public async Task <IActionResult> ChangeProfile([FromForm] ChangeProfileDTO model) { try { var data = await _userService.ChangeProfileAsync(model); return(Ok(data)); } catch (DbUpdateConcurrencyException) { return(BadRequest(new { Message = "The record you attempted to edit was modified by another user after you got the original value" })); } catch (UnauthorizedAccessException) { return(Unauthorized()); } catch (AlreadyExistsException e) { return(BadRequest(new { e.Message })); } catch (Exception e) { return(BadRequest(new { e.Message })); } }
public void ChangeProfile_ExistingIDSameEMail_UpdatedProfileHasValidData(int userID, string eMail) { var testDatabaseContext = DbContextFactory.Create(); var timeProviderMock = new Mock <ITimeProvider>(); timeProviderMock.Setup(p => p.Now()).Returns(new DateTime(2016, 1, 1)); var changeProfileDTO = new ChangeProfileDTO { EMail = eMail, City = "Test city", About = "Something about me", Footer = "Ultra-rare funny footer" }; var profileService = new ProfileService(testDatabaseContext, timeProviderMock.Object); profileService.ChangeProfile(userID, changeProfileDTO); var updatedProfile = profileService.GetProfileByUserID(userID); Assert.Equal(changeProfileDTO.EMail, updatedProfile.EMail); Assert.Equal(changeProfileDTO.City, updatedProfile.City); Assert.Equal(changeProfileDTO.About, updatedProfile.About); Assert.Equal(changeProfileDTO.Footer, updatedProfile.Footer); }
public static User UpdateUser(this User user, ChangeProfileDTO model) { if (model.Email != null) { user.Email = model.Email; } if (model.FirstName != null) { user.FirstName = model.FirstName; } if (model.LastName != null) { user.LastName = model.LastName; } return(user); }
/// <inheritdoc /> public void ChangeProfile(int id, ChangeProfileDTO newProfileData) { if (!ProfileExists(id)) { throw new UserProfileNotFoundException(); } var currentProfile = _databaseContext.Users.First(user => user.ID == id); if (currentProfile.EMail != newProfileData.EMail && EMailExists(newProfileData.EMail)) { throw new EMailAlreadyExistsException(); } currentProfile = Mapper.Map(newProfileData, currentProfile); _databaseContext.SaveChanges(); }
public async Task <UserReturnDTO> ChangeProfileAsync(ChangeProfileDTO model) { var idClaim = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value; if (!long.TryParse(idClaim, out var ownerId)) { throw new UnauthorizedAccessException(); } if (model.AvatarFile.Length / 1024 > 2048) { throw new FileTooBigException("File must be less than 2mb"); } var oldUser = await _userRepository.GetByIdAsync(ownerId); if (model.AvatarFile != null && model.AvatarFile.Length != 0) { var avatarUrl = await _uploadService.UploadFileAsync(model.AvatarFile, UploadType.Avatar); oldUser.AratarUrl = avatarUrl; } if (!string.IsNullOrWhiteSpace(model.Email)) { var emailExist = await(await _userRepository.GetAllAsync(user => user.Email.ToUpper().Equals(model.Email.ToUpper()) && user.Id != ownerId)) .AnyAsync(); if (emailExist) { throw new AlreadyExistsException("This email already exist"); } } oldUser.UpdateUser(model); await _userRepository.UpdateAsync(oldUser); return(oldUser.UserToUserReturnDTO()); }
public async Task <ChangeProfileDTO> UpdateProfileAsync(ChangeProfileDTO profileDTO) { var user = await _userRepository.GetByIdAsync(profileDTO.Id); if (user == null) { throw new HttpStatusCodeException(HttpStatusCode.NotFound, $"Wrong user Id"); } user.FirstName = profileDTO.FirstName; user.LastName = profileDTO.LastName; user.Birthdate = Convert.ToDateTime(profileDTO.DateOfBirth).Date; user.City = profileDTO.City; user.Country = profileDTO.Country; user.Email = profileDTO.Email; user.PhoneNumber = profileDTO.PhoneNumber; await _userRepository.UpdateProfileAsync(user); _theaterScheduleUnitOfWork.Save(); return(profileDTO); }
public void ChangeProfile_NotExistingID_ThrowsUserProfileNotFoundException() { var testDatabaseContext = DbContextFactory.Create(); var timeProviderMock = new Mock <ITimeProvider>(); timeProviderMock.Setup(p => p.Now()).Returns(new DateTime(2016, 1, 1)); var changeProfileDTO = new ChangeProfileDTO { EMail = "*****@*****.**", City = "Test city", About = "Something about me", Footer = "Ultra-rare funny footer" }; var profileService = new ProfileService(testDatabaseContext, timeProviderMock.Object); var exception = Record.Exception(() => profileService.GetProfileByUserID(1000)); Assert.IsType <UserProfileNotFoundException>(exception); }
public void ChangeProfile_ExistingIDExistingEMail_ThrowsEMailAlreadyExistsException(int userID, string eMail) { var testDatabaseContext = DbContextFactory.Create(); var timeProviderMock = new Mock <ITimeProvider>(); timeProviderMock.Setup(p => p.Now()).Returns(new DateTime(2016, 1, 1)); var changeProfileDTO = new ChangeProfileDTO { EMail = eMail, City = "Test city", About = "Something about me", Footer = "Ultra-rare funny footer" }; var profileService = new ProfileService(testDatabaseContext, timeProviderMock.Object); var exception = Record.Exception(() => profileService.ChangeProfile(userID, changeProfileDTO)); Assert.IsType <EMailAlreadyExistsException>(exception); }