public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var user = await _context.Users.SingleOrDefaultAsync( x => x.UserName == _userAccessor.GetCurrentUsername()); var photo = user.Photos.FirstOrDefault(x => x.Id == request.Id); if (photo == null) { throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not Found" }); } if (photo.IsMain) { throw new RestException(HttpStatusCode.BadRequest, new { Photos = "you can't delete your main photo" }); } var result = _photoAccessor.DeletePhoto(photo.Id); if (result == null) { throw new Exception("Problem deleting the photo"); } user.Photos.Remove(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Problem saving Changes"); }
public async Task <Photo> Handle(Command request, CancellationToken cancellationToken) { //handler logic var photoUploadResult = _photoAccessor.AddPhoto(request.File); var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername()); var photo = new Photo { Url = photoUploadResult.Url, Id = photoUploadResult.PublicId }; if (!user.Photos.Any(x => x.isMain)) { photo.isMain = true; } user.Photos.Add(photo); var success = await _context.SaveChangesAsync() > 0; if (success) { return(photo); } throw new Exception("Problem saving changes"); }