public IActionResult Update(int id, UserUpdateRequestDTO userDTO) { if (id != userDTO.Id || !ModelState.IsValid) { return(BadRequest(ModelState)); } var user = Mapper.Mapper.Map <User>(userDTO); if (_userService.Get(user.Id) == null) { return(NotFound()); } try { if (_userService.Update(user)) { return(NoContent()); } else { return(NotFound()); } } catch { return(NotFound()); } }
public IActionResult Put(int id, [FromBody] UserUpdateRequestDTO dto) { try { _updateUser.Execute(id, dto); return(NoContent()); } catch (EntityNotFoundException e) { return(NotFound(e.Message)); } catch (EntityExistsException e) { return(Conflict(e.Message)); }catch (Exception) { return(StatusCode(500, "Server error")); } }
public async Task <ActionResult <IResponseObject> > PatchUser(long id, UserUpdateRequestDTO userUpdateDTO) { if (!IsSelfOrAdmin(id)) { return(Forbidden()); } User user = await _userRepository.GetById(id); if (user == null) { return(UserNotFound()); } if (!_passwordHasher.Verify(userUpdateDTO.Password, user.PasswordHash)) { return(BadRequest(UserUpdateErrorTitle, UserUpdatePasswordErrorDetail)); } if (!string.IsNullOrEmpty(userUpdateDTO.Username)) { user.Username = userUpdateDTO.Username; } return(await TryPerformUserUpdate(user)); }
public void Execute(int search, UserUpdateRequestDTO request) { var role = new Role(); var username = AiContext.Users .Where(u => u.Username == request.Username) .Where(u => u.IsDeleted == 0) .Where(u => u.Id != search) .FirstOrDefault(); var user = AiContext.Users .Where(u => u.Id == search) .Where(u => u.IsDeleted == 0) .FirstOrDefault(); if (user == null) { throw new EntityNotFoundException("User"); } if (username != null) { throw new EntityExistsException("User with that username"); } int roleId = request.RoleId.HasValue ? request.RoleId.GetValueOrDefault() : 8; role = AiContext.Roles .Where(x => x.Id == roleId) .Where(x => x.IsDeleted == 0) .FirstOrDefault(); if (role == null) { throw new EntityNotFoundException("Role"); } user.FirstName = request.FirstName; user.LastName = request.LastName; user.Username = request.Username; user.RoleId = roleId; user.ModifiedAt = DateTime.Now; AiContext.SaveChanges(); }