public async Task <ApplicationUserOut> Create(ApplicationUserOut user) { Validate(user); user.Id = Guid.NewGuid().ToString(); return((await repository.Upsert(user.AsDbModel())).AsOutModel()); }
public static User AsDbModel(this ApplicationUserOut user) => new User { Id = user.Id, Email = user.Email, Username = user.Username, ExternalId = user.ExternalId, GroupIdsList = user.GroupIds.JoinNullable() };
private void Validate(ApplicationUserOut user) { // TODO: Add validation }
public async Task <ApplicationUserOut> Update(ApplicationUserOut user) => (await repository.Upsert(user.AsDbModel())).AsOutModel();
public IActionResult GetPagedList(int pageNumber = 1, int pageSize = 20, string sort = "userName", string filterColumnName = "", string filterValue = "") { try { //Filter Fields for My Lines = Line Name, Line Type and Line Location (Related Entity) PagedList <ApplicationUser> myEntities; if (filterColumnName?.ToLower() == "selectall") { myEntities = _repository.GetAllWithIncludes(new List <string>() { "ApplicationUserRoles.ApplicationRole" }, x => (x.FirstName.Contains(filterValue) || x.LastName.Contains(filterValue) || x.CompanyName.Contains(filterValue)), pageNumber, pageSize, sort, "", ""); } //else if (filterColumnName == "lineLocation.lineLocationName") //{ // myEntities = _repository.GetAllWithIncludes(new List<string>() { "LineLocation" }, x => // x.Remain == Remain && x.IsSavedLine == IsSavedLine && x.LineLocation.LineLocationName.Contains(filterValue), // pageNumber, pageSize, sort, "", ""); //} //else if (applicationUserId == null) //{ // myEntities = _repository.GetAllWithIncludes(new List<string>() { "LineLocation" }, x => // x.Remain == Remain && x.IsSavedLine == IsSavedLine, pageNumber, pageSize, sort, filterColumnName, filterValue); //} else { myEntities = _repository.GetAllWithIncludes(new List <string>() { "ApplicationUserRoles.ApplicationRole" }, x => x == x, pageNumber, pageSize, sort, filterColumnName, filterValue); } //Map ApplicationUser info to out DTO var myApplicationUsersOutList = new List <ApplicationUserOut>(); foreach (var ApplicationUser in myEntities.ListItems) { var myApplicationUserOut = new ApplicationUserOut(); myApplicationUserOut = _mapper.Map <ApplicationUserOut>(ApplicationUser); //Roles //foreach (var role in ApplicationUser.ApplicationUserRoles) //{ // myApplicationUserOut.ApplicationRoles.Add(new ApplicationRoleOut() { RoleId = role.RoleId, RoleName = role.ApplicationRole.Name }); //} myApplicationUsersOutList.Add(myApplicationUserOut); } ////var myEntitiesOut = _mapper.Map<IEnumerable<ApplicationUserOut>>(myEntities.ListItems); var myPagedListOut = new PagedList <ApplicationUserOut>(myApplicationUsersOutList, myEntities.TotalCount, myEntities.CurrentPage, myEntities.PageSize); //var myPagedListOut = new PagedList<ApplicationUser>(myEntities.ListItems, myEntities.TotalCount, myEntities.CurrentPage, myEntities.PageSize); return(Ok(myPagedListOut)); } catch (Exception ex) { if (ex.InnerException != null) { return(BadRequest(ex.InnerException.Message)); } return(BadRequest(ex.Message)); } }
public async Task <IActionResult> Create([FromBody] ApplicationUserIn EntityIn) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var MyNewEntity = new ApplicationUser(); _mapper.Map(EntityIn, MyNewEntity); MyNewEntity.ApplicationUserRoles = null; //Set email the same as username MyNewEntity.Email = MyNewEntity.UserName; MyNewEntity.DateCreated = DateTime.Now; MyNewEntity.DateLastModified = DateTime.Now; var myResult = await _userManager.CreateAsync(MyNewEntity); if (!myResult.Succeeded) { string myErrors = ""; foreach (var error in myResult.Errors) { myErrors = myErrors + error.Description; } return(BadRequest(myErrors)); } //_context.ApplicationUser.Add(MyNewEntity); //await _context.SaveChangesAsync(); //Add in Roles chosen in the UI foreach (var role in EntityIn.ApplicationUserRoles) { _context.ApplicationUserRole.Add(new ApplicationUserRole() { UserId = MyNewEntity.Id, RoleId = role.ApplicationRole.Id }); } await _context.SaveChangesAsync(); //Get User and Roles to send back for the list var myNewUser = _context.ApplicationUser .Include(x => x.ApplicationUserRoles) .ThenInclude(x => x.ApplicationRole) .FirstOrDefault(x => x.Id == MyNewEntity.Id); var myApplicationUserOut = new ApplicationUserOut(); myApplicationUserOut = _mapper.Map <ApplicationUserOut>(myNewUser); return(Ok(myApplicationUserOut)); } catch (Exception ex) { throw ex; } }