public async Task UpdateAsync(UpdateAdminUserDTO dto) { using (AdminUserContext ctx = new AdminUserContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); var phoneAdminUser = await bs.GetAll().SingleOrDefaultAsync(e => e.PhoneNum == dto.PhoneNum); if (phoneAdminUser != null) { if (phoneAdminUser.Id != dto.Id) { throw new Exception("电话号码已存在"); } } AdminUserEntity entity = await bs.GetAll().SingleOrDefaultAsync(e => e.Id == dto.Id); if (entity == null) { throw new ArgumentException($"管理员{dto.Id}不存在"); } entity.Age = dto.Age; entity.Gender = dto.Gender; entity.Name = dto.Name; entity.PhoneNum = dto.PhoneNum; await ctx.SaveChangesAsync(); } }
public async Task <IActionResult> Post([FromBody] UpdateAdminUserModel model) { var idEntity = await AdminUserSvc.GetByIdAsync(model.Id); if (idEntity == null) { return(new JsonResult(new APIResult <long> { ErrorMsg = "管理员不存在" }) { StatusCode = 400 }); } var phoneEntity = await AdminUserSvc.GetByPhoneNumAsync(model.PhoneNum); if (phoneEntity != null) { if (phoneEntity.Id != idEntity.Id) { return(new JsonResult(new APIResult <long> { ErrorMsg = "电话号码已存在" }) { StatusCode = 400 }); } } UpdateAdminUserDTO dto = new UpdateAdminUserDTO(); dto.Age = model.Age; dto.Id = model.Id; dto.Gender = model.Gender; dto.Name = model.Name; dto.PhoneNum = model.PhoneNum; await AdminUserSvc.UpdateAsync(dto); return(Ok()); }