コード例 #1
0
ファイル: Delete.cs プロジェクト: Wallruzz9114/kurama
            public async Task <Unit> Handle(Command command, CancellationToken cancellationToken)
            {
                var appUser = await _databaseContext.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                var photo = appUser.Photos.FirstOrDefault(x => x.Id == command.Id);

                if (photo == null)
                {
                    throw new RESTException(HttpStatusCode.NotFound, new { Photo = "Not found" });
                }

                if (photo.IsMain)
                {
                    throw new RESTException(HttpStatusCode.BadRequest, new { Photo = "You can't delete your main photo" });
                }

                var deleteAttemptResult = _imageAccessor.DeleteImage(photo.Id);

                if (deleteAttemptResult == null)
                {
                    throw new Exception("Error: couldn't delete image");
                }

                appUser.Photos.Remove(photo);

                var userSuccessfullyUpdated = await _databaseContext.SaveChangesAsync() > 0;

                if (userSuccessfullyUpdated)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem while deleting image");
            }
コード例 #2
0
ファイル: Delete.cs プロジェクト: danielignatov/Reactivities
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.Include(x => x.Images).SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUsername());

                var image = user.Images.FirstOrDefault(x => x.Id == request.Id);

                if (image == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, "Image not found");
                }

                if (image.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest, "You cannot delete your main image");
                }

                var result = _imageAccessor.DeleteImage(request.Id);

                if (result == ImageDeleteResult.Error)
                {
                    throw new Exception("Problem deleting the image");
                }

                user.Images.Remove(image);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
コード例 #3
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var product = await _context.Products.SingleOrDefaultAsync(x => x.Id == request.IdProduct);

                if (product == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Product = "Product Not Found" });
                }

                var image = product.Images.FirstOrDefault(x => x.Id == request.Id);

                if (image == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { Image = "Image Not Found" });
                }

                if (image.IsMain)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { Image = "You cannot delete your main image" });
                }

                var result = _imageAccessor.DeleteImage(image.IdCloudinary);

                if (result == null)
                {
                    throw new Exception("Problem deleting the image");
                }

                product.Images.Remove(image);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }