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); }
public async Task <UserPhotoUpdateRequest> UpdateUserPhotoAsync(UserPhotoUpdateRequest request, long userId) { return(await _userPhotoRepository.UpdateUserPhotoAsync(request, userId)); }