public CrResult <UserDto> Edit(UserCrDto userCrDto, User user) { var crResult = new CrResult <UserDto>(); if (user != null) { bool nicknameChanged = userCrDto.Nickname != user.nickname; bool passwordChanged = String.IsNullOrEmpty(userCrDto.Password) && !new CustomUserNameValidator().IsPasswordMatch(userCrDto.Password, user); CheckRegisterData(userCrDto, crResult, nicknameChanged, passwordChanged); if (crResult.ActionResult == ActionResult.Success) { Copy(userCrDto, user, nicknameChanged, passwordChanged); try { _context.SaveChanges(); crResult.CreatedObject = Converter.ConvertToUserDto(user); } catch (DbUpdateException) { crResult.ActionResult = ActionResult.DatabaseError; } } } else { crResult.AddError(new Error(CheckStatus.IdDoesNotExist, nameof(user.user_id))); } return(crResult); }
private void CheckRegisterData(UserCrDto userCrDto, Result r, bool checkNickname, bool checkPawssword) { if (checkNickname) { CheckNickname(userCrDto, r); } if (checkPawssword) { CheckPassword(userCrDto, r); } CheckEmail(userCrDto, r); CheckPhone(userCrDto, r); CheckStringData(userCrDto, r); }
private void CheckPassword(UserCrDto userCrDto, Result r) { var checkStatus = Helper.CheckParam(userCrDto.Password, _maxPasswordLength, false); if (checkStatus != CheckStatus.Success) { r.AddError(new Error(checkStatus, nameof(userCrDto.Password))); return; } if (!ValidatePassword(userCrDto.Password)) { r.AddError(new Error(CheckStatus.ArgumentDoesNotMatchFormat, nameof(userCrDto.Password))); } }
private void Copy(UserCrDto userCrDto, User user, bool copyNickname, bool copyPassword) { user.city_id = userCrDto.CityId; user.university_id = userCrDto.UniversityId; user.description = userCrDto.Description; user.email = userCrDto.Email; user.group = userCrDto.Group; user.name = userCrDto.Name; user.phone = userCrDto.Phone; user.surname = userCrDto.Surname; if (copyNickname) { user.nickname = userCrDto.Nickname; } if (copyPassword) { var encryption = new HashEncryption(); encryption.HashPassword(user); } }
private void CheckPhone(UserCrDto userCrDto, Result r) { var paramName = nameof(userCrDto.Phone); var checkStatus = Helper.CheckParam(userCrDto.Phone, _phoneLength, true); if (checkStatus != CheckStatus.Success) { r.AddError(new Error(checkStatus, paramName)); } else { if (string.IsNullOrEmpty(userCrDto.Phone)) { return; } var match = Regex.IsMatch(userCrDto.Phone, @"\(?\d{3}\)?-? *\d{3}-? *-?\d{4}"); if (!match) { r.AddError(new Error(CheckStatus.ArgumentDoesNotMatchFormat, paramName)); } } }
private void CheckStringData(UserCrDto userCrDto, Result r) { var checkStatus = Helper.CheckParam(userCrDto.Description, _descriptionLength, true); if (checkStatus != CheckStatus.Success) { r.AddError(new Error(checkStatus, nameof(userCrDto.Description))); } checkStatus = Helper.CheckParam(userCrDto.Group, _groupLength, true); if (checkStatus != CheckStatus.Success) { r.AddError(new Error(checkStatus, nameof(userCrDto.Group))); } checkStatus = Helper.CheckParam(userCrDto.Name, _nameLength, true); if (checkStatus != CheckStatus.Success) { r.AddError(new Error(checkStatus, nameof(userCrDto.Name))); } checkStatus = Helper.CheckParam(userCrDto.Surname, _surnameLength, true); if (checkStatus != CheckStatus.Success) { r.AddError(new Error(checkStatus, nameof(userCrDto.Surname))); } if (userCrDto.CityId != null && !new DataBaseInformationManager().IsCityExist((long)userCrDto.CityId)) { r.AddError(new Error(CheckStatus.IdDoesNotExist, nameof(userCrDto.CityId))); } if (userCrDto.UniversityId != null && !new DataBaseInformationManager().IsUnivercityExist((long)userCrDto.UniversityId)) { r.AddError(new Error(CheckStatus.IdDoesNotExist, nameof(userCrDto.UniversityId))); } }
public CrResult <UserDto> Create(UserCrDto userCrDto) { var crResult = new CrResult <UserDto>(); CheckRegisterData(userCrDto, crResult, true, true); if (crResult.ActionResult == ActionResult.Success) { var user = Converter.ConvertToUser(userCrDto); var encryption = new HashEncryption(); encryption.HashPassword(user); try { var createdUser = _context.User.Add(user); _context.SaveChanges(); crResult.CreatedObject = Converter.ConvertToUserDto(createdUser); } catch (DbUpdateException) { crResult.ActionResult = ActionResult.DatabaseError; } } return(crResult); }