MVC - Entity Framework using Connection Extension (Insert,update,selet,Delete) :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using FluentValidation;
using API.Database;
using API.Models;
namespace API.Controllers
{
[RoutePrefix("users/{user_id:long}/profile")]
public class UserprofileController : ApiController
{
// GET: userprofile
#region DTO & Validation
public class UserprofileRequest
{
public long user_id { get; set; }
public string profile_certificate { get; set; }
public string profile_profileUrl { get; set; }
public IEnumerable<UserprofileRefrences> profileRefrencesRequest { get; set; }
}
public class UserprofileResponseJSON
{
public long id { get; set; }
public long user_id { get; set; }
public string jobDetails { get; set; }
public string Amount { get; set; }
public int amount_type { get; set; }
}
public class UserprofileStatus
{
public long user_id { get; set; }
public bool Isprofile_status { get; set; }
}
public class UserprofileResponseStatusJSON
{
public long user_id { get; set; }
public int Isprofile_status { get; set; }
}
public class UserprofileRefrencesRefrences:IDataModel
{
public long id { get; set; }
public long user_id { get; set; }
public string jobDetails { get; set; }
public string Amount { get; set; }
public int amount_type { get; set; }
}
#endregion
#region Routes
[Route]
public dynamic Get(long user_id)
{
var profileResponse = new List<UserprofileResponseJSON>();
http://vijubook.blogspot.in/2014/12/mvc-entity-framework-using-connection.html
http://vijubook.blogspot.in/2014/12/mvc-entity-framework-using-connection.html
// FetchBy
var _profileRefrences = Db.FetchBy<UserprofileRefrences>x=>x.user_id==user_id);
foreach (var profile in _profileRefrences)
{
var _flReferences = Db.Get<UserprofileRefrences>(profile.id);
var _userprofile = new UserprofileResponseJSON()
{
id=_flReferences.id,
user_id=user_id,
jobDetails=_flReferences.jobDetails,
Amount=_flReferences.Amount,
amount_type = _flReferences.amount_type.GetHashCode()
};
profileResponse.Add(_userprofile);
}
return profileResponse;
}
[Route]
public dynamic post(long user_id, UserprofileRequest request)
{
var response = new UserprofileResponseJSON();
try
// GetBy
var _profile = Db.GetBy<Profile>(x => x.user_id == user_id);
if (_profile != null)
{
_profile.profile_profileUrl = request.profile_profileUrl;
_profile.profile_certificate = request.profile_certificate;
}
else
{
return _profile;
}
var _profile = Db.Update<Profile>(_profile);
var _flReferences = request.profileRefrencesRequest.ToList();
foreach (var _flRequest in _flReferences)
{
if (_flRequest.jobDetails != null && _flRequest.jobDetails != "" && _flRequest.Amount != null && _flRequest.Amount != "")
{
var flR_ID = Db.GetBy<UserprofileRefrences>(x => x.id == _flRequest.id);
if (flR_ID == null)
{
var _fl = new UserprofileRefrences
{
user_id = user_id,
jobDetails = _flRequest.jobDetails,
Amount = _flRequest.Amount,
amount_type=_flRequest.amount_type
//Save
Db.Save<UserprofileRefrences>(_fl);
}
else
{
flR_ID.jobDetails = _flRequest.jobDetails;
flR_ID.Amount = _flRequest.Amount;
flR_ID.amount_type = _flRequest.amount_type;
http://vijubook.blogspot.in/2014/12/mvc-entity-framework-using-connection.html
http://vijubook.blogspot.in/2014/12/mvc-entity-framework-using-connection.html
//Update
Db.Update<UserprofileRefrences>(flR_ID);
}
}
else
{
}
}
}
catch
{
}
return response;
}
[Route("{Isprofile_status:int}")]
public dynamic put(long user_id, int Isprofile_status)
{
// var response = new List<UserprofileResponseStatusJSON>();
var _userprofile = new UserprofileResponseStatusJSON();
try
{
var _profile = Db.GetBy<Profile>(x => x.user_id == user_id);
if (_profile != null)
{
_profile.Isprofile_status = Isprofile_status;
}
else
{
return _profile;
}
var _profile = Db.Update<Profile>(_profile);
_userprofile = new UserprofileResponseStatusJSON()
{
user_id = user_id,
Isprofile_status = _profile.Isprofile_status,
};
// response.Add(_userprofile);
}
catch (Exception ex)
{
}
return _userprofile;
}
[Route("{id:long}")]
public dynamic Delete(long user_id, long id)
{
try
{
var _userprofileReferences = Db.GetBy<UserprofileRefrences>(x => x.id == id && x.user_id == user_id);
if (_userprofileReferences == null || _userprofileReferences.user_id != user_id)
return Request.CreateErrorResponse(HttpStatusCode.NotFound, string.Format("No profile References found for given id:{0}", id));
http://vijubook.blogspot.in/2014/12/mvc-entity-framework-using-connection.html
//Delete
http://vijubook.blogspot.in/2014/12/mvc-entity-framework-using-connection.html
//Delete
Db.Delete<UserprofileRefrences>(_userprofileReferences);
return Request.CreateResponse(HttpStatusCode.OK);
}
catch(Exception ex) {
return Request.CreateErrorResponse(ex, HttpStatusCode.BadRequest, "Failed userfreeaReferences code");
}
}
#endregion
}
}
No comments:
Post a Comment