public async Task <UserResult> UpdatePasswordAsync(UserPasswordUpdateRequest model) { _validationStrategyContext.SetStrategy(new UserPasswordUpdateValidationStratergy()); bool canUpdate = _validationStrategyContext.Validate(model); if (!canUpdate) { throw new UnauthorizedAccessException(); } var errors = _validationStrategyContext.Errors; if (errors != null || errors.Any()) { throw new ArgumentException(_validationStrategyContext.Errors.FirstOrDefault().Message); } var user = await _userRepository.FirstOrDefaultAsync(x => x.Id == model.UserId); if (user == null) { return(new UserResult()); } user.PasswordHash = model.NewPassword; await _userRepository.UpdateAsync(user); return(new UserResult() { Id = user.Id }); }
public CommonResult ValidateImageUrl(ImageValidationModel criterias) { _validationStrategyContext.SetStrategy(new ImageUrlValidationStrategy()); if (criterias == null || string.IsNullOrEmpty(criterias.Url)) { return(CommonResult.Failed(new CommonError())); } bool canUpdate = _validationStrategyContext.Validate(criterias.Url); if (!canUpdate) { _validationStrategyContext.SetStrategy(new Base64ImageValidationStrategy()); canUpdate = _validationStrategyContext.Validate(criterias.Url); } if (canUpdate) { return(CommonResult.Success()); } return(CommonResult.Failed(new CommonError())); }
public async Task <UpdateItemRequest> UpdateInfoItemAsync(UpdateItemRequest request) { if (request.PropertyName == null) { throw new ArgumentException(nameof(request.PropertyName)); } if (request.Key == null) { throw new ArgumentException(nameof(request.Key)); } var key = (long)request.Key; var userInfo = _userInfoRepository.FirstOrDefault(x => x.Id == key); if (userInfo == null) { throw new ArgumentException(nameof(userInfo)); } _validationStrategyContext.SetStrategy(new UserInfoItemUpdationValidationStratergy(_validationStrategyContext)); bool canUpdate = _validationStrategyContext.Validate(request); if (!canUpdate) { throw new ArgumentException(request.PropertyName); } if (userInfo.User != null) { userInfo.User.UpdatedDate = DateTime.UtcNow; userInfo.User.UpdatedById = userInfo.Id; } await _dataConnection.UpdateByNameAsync(userInfo, request.Value, request.PropertyName, true); return(request); }
public async Task <UserPhotoUpdateRequest> UpdateUserPhotoAsync(UserPhotoUpdateRequest request, long userId) { if (request == null) { throw new ArgumentNullException(nameof(request)); } var userInfo = _userInfoRepository.FirstOrDefault(x => x.Id == userId); if (userInfo == null) { throw new ArgumentException(nameof(userInfo)); } _validationStrategyContext.SetStrategy(new Base64ImageValidationStrategy()); bool canUpdate = _validationStrategyContext.Validate(request.PhotoUrl); if (!canUpdate) { throw new ArgumentException(request.PhotoUrl); } if (request.UserPhotoTypeId == (int)UserPictureType.Avatar) { _validationStrategyContext.SetStrategy(new AvatarValidationStrategy()); canUpdate = _validationStrategyContext.Validate(request); } else if (request.UserPhotoTypeId == (int)UserPictureType.Cover) { _validationStrategyContext.SetStrategy(new UserCoverValidationStrategy()); canUpdate = _validationStrategyContext.Validate(request); } if (!canUpdate && request.UserPhotoTypeId == (int)UserPictureType.Avatar) { throw new PhotoSizeInvalidException($"{nameof(UserPictureType.Avatar)}Should larger than 100px X 100px"); } else if (!canUpdate) { throw new PhotoSizeInvalidException($"{nameof(UserPictureType.Cover)}Should larger than 1000px X 300px"); } int maxSize = request.UserPhotoTypeId == (int)UserPictureType.Avatar ? 600 : 1000; var newImage = ImageUtil .Crop(request.PhotoUrl, request.XAxis, request.YAxis, request.Width, request.Height, request.Scale, maxSize); var userPhotoType = (byte)request.UserPhotoTypeId; var userPhoto = _userPhotoRepository .Get(x => x.UserId == userId && x.TypeId == userPhotoType) .FirstOrDefault(); request.UserPhotoCode = Guid.NewGuid().ToString(); if (userPhoto == null) { userPhoto = new UserPhoto() { CreatedById = userId, CreatedDate = DateTime.UtcNow, ImageData = newImage, TypeId = (byte)request.UserPhotoTypeId, UserId = userId, Name = request.FileName, Code = request.UserPhotoCode, }; await _userPhotoRepository.AddAsync(userPhoto); } else { userPhoto.ImageData = newImage; userPhoto.Name = request.FileName; userPhoto.Code = request.UserPhotoCode; await _userPhotoRepository.UpdateAsync(userPhoto); } request.PhotoUrl = userPhoto.Code; return(request); }