public bool Add(CreateOrUpdateUserDto createOrUpdateUserDto) { var user = Mapper.Map <User>(createOrUpdateUserDto); this._db.Users.Add(user); return(this._db.SaveChanges() > 0); }
public async Task <bool> Add(CreateOrUpdateUserDto createOrUpdateUserDto) { try { var user = Mapper.Map <User>(createOrUpdateUserDto); this._db.Users.Add(user); return(await this._db.SaveChangesAsync() > 0); } catch (DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; } }
public bool Delete(CreateOrUpdateUserDto deleteUser) { var user = Mapper.Map <User>(deleteUser); this._db.Users.Remove(user); return(this._db.SaveChanges() > 0); }
public async Task <ActionResult> Edit([Bind(Include = "Id,UserName,PassWord,Email,UserState,RoleType,AddTime")] CreateOrUpdateUserDto user) { if (ModelState.IsValid) { this._userService.Update(user); } return(View(user)); }
private void ValidateCreateOrUpDateUser(CreateOrUpdateUserDto dto) { if ((dto.Id == null && dto.Password.IsNullOrWhiteSpace()) || dto.UserName.IsNullOrWhiteSpace() || dto.FullName.IsNullOrWhiteSpace() || dto.Email.IsNullOrWhiteSpace() ) { throw new BusinessException("Invalid parameter!", ErrorCode.INVALID_PARAMETER); } }
public async Task <BaseResponse <DetailUserResultDto> > UpdateUser([FromBody] CreateOrUpdateUserDto dto) { if (dto == null) { throw new BusinessException("Invalid parameter!", ErrorCode.INVALID_PARAMETER); } var response = new BaseResponse <DetailUserResultDto> { Data = await _userService.CreateOrUpdateUser(dto), Status = true }; return(await Task.FromResult(response)); }
public static Data.Entity.Account.Account ToAccount(this CreateOrUpdateUserDto dto) { if (dto == null) { return(null); } var entity = new Data.Entity.Account.Account(); entity.CopyPropertiesFrom(dto); entity.Id = Guid.NewGuid(); return(entity); }
public static User ToUser(this CreateOrUpdateUserDto dto) { if (dto == null) { return(null); } var entity = new User(); entity.CopyPropertiesFrom(dto); entity.Id = (Guid)(dto.Id == null ? Guid.NewGuid() : dto.Id); entity.ProvinceId = dto.Province == null ? null : (int?)int.Parse(dto.Province.Key); entity.DistrictId = dto.District == null ? null : (int?)int.Parse(dto.District.Key); entity.CommuneId = dto.Commune == null ? null : (int?)int.Parse(dto.Commune.Key); return(entity); }
public async Task <JsonResult> createPost(string username, string gender, string role, string major) { CreateOrUpdateUserDto addusr = new CreateOrUpdateUserDto(); if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(gender) || string.IsNullOrEmpty(role) || string.IsNullOrEmpty(major)) { return(Json(new { code = 1, msg = "用户名,性别,角色,专业不能为空" })); } else { // //Add user to db. // addusr.UserName = username; addusr.Gender = gender; //addusr.RoleType = ; addusr.Major = major; await this._userService.Add(addusr); return(Json(new { code = 0 })); } }
public async Task <DetailUserResultDto> CreateOrUpdateUser(CreateOrUpdateUserDto dto) { ValidateCreateOrUpDateUser(dto); var checkAccount = new CheckAccountDto { UserName = dto.Id == null ? dto.UserName : null, Email = dto.Email }; await CheckAccount(checkAccount); var now = DateTime.UtcNow; using (IDbTransaction trans = this.DatabaseConnectService.Connection.BeginTransaction()) { try { if (dto.Id == null) { var user = dto.ToUser(); user.CreatedAt = now; user.CreatedBy = _sessionService.UserId; await this.DatabaseConnectService.Connection.InsertAsync <User>(user, x => x.AttachToTransaction(trans)); var account = dto.ToAccount(); account.CreatedAt = now; account.CreatedBy = _sessionService.UserId; account.UserId = user.Id; await this.DatabaseConnectService.Connection.InsertAsync <Data.Entity.Account.Account>(account, x => x.AttachToTransaction(trans)); if (dto.Group != null) { var userGroup = dto.Group.ToUserGroup(); userGroup.UserId = user.Id; userGroup.CreatedAt = now; await this.DatabaseConnectService.Connection.InsertAsync <UserGroup>(userGroup, x => x.AttachToTransaction(trans)); } dto.Id = user.Id; } else { var user = (await this.DatabaseConnectService.Connection.FindAsync <User>(x => x .AttachToTransaction(trans) .Include <UserGroup>(join => join.LeftOuterJoin()) .Where($"bys_user.id = @UserId") .WithParameters(new { UserId = dto.Id }))).FirstOrDefault(); var userGroupAlive = user.UserGroups.Where(x => x.Status == EntityStatus.Alive.ToString()).FirstOrDefault(); var userInsert = dto.ToUser(); userInsert.ModifiedAt = now; userInsert.UpdateBy = _sessionService.UserId; userInsert.CreatedAt = user.CreatedAt; userInsert.CreatedBy = user.CreatedBy; await this.DatabaseConnectService.Connection.UpdateAsync <User>(userInsert, x => x.AttachToTransaction(trans)); if (dto.Group != null) { var userGroupInsert = dto.Group.ToUserGroup(); userGroupInsert.UserId = user.Id; userGroupInsert.CreatedAt = now; await this.DatabaseConnectService.Connection.InsertAsync <UserGroup>(userGroupInsert, x => x.AttachToTransaction(trans)); if (userGroupAlive != null) { userGroupAlive.Status = EntityStatus.Delete.ToString(); await this.DatabaseConnectService.Connection.UpdateAsync <UserGroup>(userGroupAlive, x => x.AttachToTransaction(trans)); } } } trans.Commit(); _redisCache.Remove(CacheConst.AllUser); return(await GetDetailUserById((Guid)dto.Id)); } catch (Exception e) { trans.Rollback(); throw new BusinessException(e.Message, ErrorCode.INTERNAL_SERVER_ERROR); } } }