public async Task <Unit> Handle(Command request, CancellationToken cancellationToken) { var user = await context.Users.Include(p => p.UserPhotos).SingleOrDefaultAsync(x => x.UserName == userAccessor.GetCurrentUsername()); if (user == null) { throw new RestException(HttpStatusCode.BadRequest, new{ user = "******" }); } var photo = user.UserPhotos.FirstOrDefault(x => x.Id == request.PhotoId); if (photo == null) { throw new RestException(HttpStatusCode.BadRequest, new{ Photos = "Photo not found.!!!" }); } var deletePhoto = await photosAccessor.DeletePhoto(photo.ImageUrl); if (!deletePhoto) { throw new RestException(HttpStatusCode.BadRequest, new{ DeletePhotoPhysical = "Photo cant delete from Path..." }); } context.UserPhotos.Remove(photo); var success = await context.SaveChangesAsync() > 0; if (success) { return(Unit.Value); } throw new Exception("Delete Photo Problems.."); }
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 = "Photo not found!" }); } if (photo.IsMain) { throw new RestException(HttpStatusCode.BadRequest, new { Photo = "You can't delete the 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("Problems saving changes!"); }