public async Task <HttpResponseMessage> Groups(GroupsDataModel model) { Thread.CurrentThread.CurrentCulture = culture; List <GroupsQueryModel> resultSeachModel = null; GroupsByIdModel resultSeachByIdModel = null; Grupos proxy = new Grupos(); try { if (!await Authentication.isAdmin(User, Request)) { Authentication auth = new Authentication(); if (!await auth.AccesRights(User.Identity.GetUserId(), "groups", model.type)) { return(Request.CreateResponse(System.Net.HttpStatusCode.Unauthorized)); } auth = null; } using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { using (SqlConnection connection1 = new SqlConnection(connetionString)) { await connection1.OpenAsync(); if (model.type == 1) { resultSeachModel = new List <GroupsQueryModel>(); await proxy.SearchQuery(model.search, resultSeachModel, connection1); } else if (model.type == 2) { resultSeachByIdModel = new GroupsByIdModel(); await proxy.ById(model.byId, connection1, resultSeachByIdModel); } else if (model.type == 3) { await proxy.New(connection1, model.update); } else if (model.type == 4) { await proxy.Update(connection1, model.update); } else if (model.type == 5) { await proxy.UpdateIsActive(connection1, model.isActive); } } scope.Complete(); } } catch (TransactionAbortedException ex) { ErrorModel _errors = new ErrorModel(); _errors.message = ex.Message; return(Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors)); } catch (Exception ex) { ErrorModel _errors = new ErrorModel(); _errors.message = ex.Message; return(Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors)); } if (model.type == 1) { return(Request.CreateResponse(System.Net.HttpStatusCode.OK, resultSeachModel)); } else if (model.type == 2) { return(Request.CreateResponse(System.Net.HttpStatusCode.OK, resultSeachByIdModel)); } return(Request.CreateResponse(System.Net.HttpStatusCode.OK)); }
public async Task <HttpResponseMessage> Users(UsersDataModel model) { Thread.CurrentThread.CurrentCulture = culture; List <UsersQueryModel> resultSeachModel = null; UsersByIdModel resultSeachByIdModel = null; Users proxy = new Users(); try { bool isAdmin = false; if (!await Authentication.isAdmin(User, Request)) { Authentication auth = new Authentication(); if (!await auth.AccesRights(User.Identity.GetUserId(), "users", model.type)) { return(Request.CreateResponse(System.Net.HttpStatusCode.Unauthorized)); } auth = null; } else { isAdmin = true; } string idAdminrole = ""; if (isAdmin && (model.type == 3 || model.type == 4)) { var rm = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(new ApplicationDbContext())); idAdminrole = rm.FindByName("Administrador").Id; } if (model.type == 3) { return(Request.CreateResponse(System.Net.HttpStatusCode.OK, await proxy.New(model.update, Request, connetionString, isAdmin, idAdminrole))); } else if (model.type == 5) { await proxy.UpdateIsActive(model.isActive, Request); } else if (model.type == 6) { await proxy.ChangePassword(model.changePassword, Request); } else { using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { using (SqlConnection connection1 = new SqlConnection(connetionString)) { await connection1.OpenAsync(); if (model.type == 1) { resultSeachModel = new List <UsersQueryModel>(); await proxy.SearchQuery(model.search, resultSeachModel, connection1, isAdmin); } else if (model.type == 2) { resultSeachByIdModel = new UsersByIdModel(); await proxy.ById(model.byId, connection1, resultSeachByIdModel, isAdmin); } else if (model.type == 4) { await proxy.Update(model.update, connection1, isAdmin, idAdminrole); } } scope.Complete(); } } } catch (TransactionAbortedException ex) { ErrorModel _errors = new ErrorModel(); _errors.message = ex.Message; return(Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors)); } catch (Exception ex) { ErrorModel _errors = new ErrorModel(); _errors.message = ex.Message; return(Request.CreateResponse(System.Net.HttpStatusCode.InternalServerError, _errors)); } if (model.type == 1) { return(Request.CreateResponse(System.Net.HttpStatusCode.OK, resultSeachModel)); } else if (model.type == 2) { return(Request.CreateResponse(System.Net.HttpStatusCode.OK, resultSeachByIdModel)); } return(Request.CreateResponse(System.Net.HttpStatusCode.OK)); }