//PATCH : api/users/1/profiles public async Task <IActionResult> PartialUpdate( [FromRoute] Guid userId, JsonPatchDocument <UserProfileForUpdateDto> patchDocument) { var userProfileResponse = await this.businessLogic.GetUserProfile(userId); if (!userProfileResponse.IsSuccessful) { return(BadRequest(new { message = userProfileResponse.ErrorMessage })); } if (userProfileResponse.Result == null) { var userProfileDto = new UserProfileForUpdateDto(); patchDocument.ApplyTo(userProfileDto, ModelState); if (!TryValidateModel(userProfileDto)) { return(ValidationProblem(ModelState)); } var userProfileForCreationDto = this.businessLogic.MapToUserProfileForCreation(userProfileDto); if (!TryValidateModel(userProfileForCreationDto)) { return(ValidationProblem(ModelState)); } var createClientResponse = await this.businessLogic.AddNewUserProfile(userId, userProfileForCreationDto); if (!createClientResponse.IsSuccessful) { return(BadRequest(new { message = createClientResponse.ErrorMessage })); } return(CreatedAtRoute("GetUserProfile", new { userId }, createClientResponse.Result)); } UserProfileForUpdateDto userProfileToPatch = this.businessLogic.MapToUserProfileForUpdateDto(userProfileResponse.Result); patchDocument.ApplyTo(userProfileToPatch, ModelState); if (!TryValidateModel(userProfileToPatch)) { return(ValidationProblem(ModelState)); } var response = await this.businessLogic.UpdateUserProfile(userProfileResponse.Result, userProfileToPatch); if (!response.IsSuccessful) { return(BadRequest(new { message = response.ErrorMessage })); } return(NoContent()); }
public async Task <IActionResult> GetUserProfile([FromBody] UserProfileForUpdateDto profileForUpdate, [FromRoute] string Username) { var existingProfile = await _dbContext.Users.FirstOrDefaultAsync((user) => user.Username == Username); existingProfile.FirstName = profileForUpdate.FirstName; existingProfile.LastName = profileForUpdate.LastName; existingProfile.ProfilePic = profileForUpdate.ProfilePic; existingProfile.Status = profileForUpdate.Status; existingProfile.DOB = profileForUpdate.DOB; existingProfile.Intro = profileForUpdate.Intro; _dbContext.Users.Update(existingProfile); await _dbContext.SaveChangesAsync(); return(Ok(existingProfile)); }
public async Task <GenericResponse> UpdateUserProfile(UserProfile userProfile, UserProfileForUpdateDto userProfileToPatch) { try { this.mapper.Map(userProfileToPatch, userProfile); this.dataService.UserProfiles.Update(userProfile); await this.dataService.CompleteAsync(); return(GenericResponseBuilder.Success()); } catch (Exception ex) { //ToDo Log Error return(GenericResponseBuilder.NoSuccess($"{ex.Message} InnerException: {ex.InnerException.Message}")); } }
public UserProfileForCreationDto MapToUserProfileForCreation(UserProfileForUpdateDto userProfileDto) { return(this.mapper.Map <UserProfileForCreationDto>(userProfileDto)); }