public bool Add(User user, UserCredentials userCredentials) { if (userCredentials.Password == null || userCredentials.Email == null) { throw new IncorrectDataException("Data not correct"); } if (_userCredentialsRepository.CheckByEmail(userCredentials.Email)) { throw new IncorrectDataException("Email booked"); } if (userCredentials.Role == null) { userCredentials.Role = "User"; } userCredentials.RegistrationDate = DateTime.Now; userCredentials.Password = SaltedHashGenerator.GenerateHash(userCredentials.Password, userCredentials.Email); try { _userRepository.Add(user, userCredentials); return(true); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public void Add(Comment comment) { try { _commentRepository.Add(comment); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public bool Delete(int id) { try { _userCredentialsRepository.Delete(id); return(true); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public void AddToComment(string address, int id) { Photo photo = new Photo { Address = $"{_file}{address}", CommentId = id }; try { _photoRepository.Add(photo); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public bool EditPassword(int id, string newPassword, string oldPassword) { try { UserCredentials userCredentials = _userCredentialsRepository.GetById(id); string oldP = SaltedHashGenerator.GenerateHash(oldPassword, userCredentials.Email); if (oldP != userCredentials.Password) { throw new IncorrectDataException("Passwords do not match"); } _userCredentialsRepository.EditPassword(id, newPassword); return(true); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public void Delete(int idComment, int idUser) { var user = _userCredentialsRepository.GetById(idUser); var comment = GetById(idComment); if (user.Role != "Admin" || user.Id != comment.UserId) { throw new IncorrectDataException("has no access to edit"); } try { _commentRepository.Delete(idComment); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public void Edit(int idComment, int idAdmin, string newTextComment) { if (_userCredentialsRepository.GetById(idAdmin).Role != "Admin") { throw new IncorrectDataException("has no access to edit"); } try { Comment comment = GetById(idComment); comment.Text = newTextComment; comment.AdminId = idAdmin; comment.LastModifiedDate = DateTime.Now; _commentRepository.Edit(comment); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }
public bool Edit(User user, int id) { var oldData = _userRepository.GetById(id); if (oldData == null) { throw new IncorrectDataException("Data not correct"); } oldData.LastName = CompareStringData(oldData.LastName, user.LastName); oldData.FirstName = CompareStringData(oldData.FirstName, user.FirstName); oldData.NickName = CompareStringData(oldData.NickName, user.NickName); oldData.Сountry = CompareStringData(oldData.Сountry, user.Сountry); try { _userRepository.Edit(user); return(true); } catch (DbEntityValidationException ex) { throw new IncorrectDataException(DbEntityValidationExceptioErrorMessages.ErrorMessages(ex)); } }