public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { await context.Images.LoadAsync(cancellationToken : cancellationToken); var user = await context.Users.SingleOrDefaultAsync(x => x.UserName == currentUserAccessor.GetCurrentUsername(), cancellationToken : cancellationToken); if (user.Photo == null) { throw new RestException(HttpStatusCode.NotFound, new { Photo = "Photo doesn't exist" }); } if (user.Photo.Id != request.Id) { throw new RestException(HttpStatusCode.NotFound, new { Photo = "Photo doesn't exist" }); } var photo = user.Photo; var result = await photoAccessor.DeletePhotoAsync(photo); if (!result) { throw new Exception("Problem deleting photo"); } return(Unit.Value); }
public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { DataModel.Photo photo = await _unitOfWork.PhotoRepo.FindFirstAsync(request.PhotoId, cancellationToken); if (photo == null) { throw new CustomException(HttpStatusCode.NotFound, new { PhotoId = "Invalid photo id" }); } await _photoAccessor.DeletePhotoAsync(photo.CloudFileName, cancellationToken); _unitOfWork.PhotoRepo.Delete(photo); int cnt = await _unitOfWork.SaveAsync(cancellationToken); if (cnt > 0) { return(Unit.Value); } throw new Exception("Problem saving changes to database"); }