Пример #1
0
        public async Task <Result <GetProductReviewModel> > SaveReviewAsync(Guid productId, SendProductReviewModel model)
        {
            if (!CustomerAuthorizationService.CustomerIdIsAuthorized(productId))
            {
                return(Result <GetProductReviewModel> .Forbidden());
            }

            if (await ProductRepository.GetFirstOrDefaultAsync(predicate: p => p.Id == productId) == null)
            {
                return(Result <GetProductReviewModel> .BadRequest($"Product with Id {productId} does not exist."));
            }

            if (await CustomerRepository.GetFirstOrDefaultAsync(predicate: c => c.Id == model.CustomerId) == null)
            {
                return(Result <GetProductReviewModel> .BadRequest($"Customer with Id {model.CustomerId} does not exist."));
            }

            if (await ProductReviewRepository.GetFirstOrDefaultAsync(predicate: fp => fp.CustomerId == model.CustomerId && fp.ProductId == productId) != null)
            {
                return(Result <GetProductReviewModel> .BadRequest($"Customer with id {model.CustomerId} already has a review for product with id {productId}."));
            }

            var review = Mapper.Map <ProductReview>(model);

            review.ProductId = productId;

            await ProductReviewRepository.InsertAsync(review);

            await UnitOfWork.SaveChangesAsync();

            return(Result <GetProductReviewModel> .Success(Mapper.Map <GetProductReviewModel>(review)));
        }
Пример #2
0
        public async Task <Result> DeleteFavoriteProduct(Guid customerId, DeleteFavoriteProductModel model)
        {
            if (!CustomerAuthorizationService.CustomerIdIsAuthorized(customerId))
            {
                return(Result.Forbidden());
            }

            if (await ProductRepository.GetFirstOrDefaultAsync(predicate: c => c.Id == customerId) == null)
            {
                return(Result.NotFound());
            }

            var relationship = await
                               FavoriteProductRepository.GetFirstOrDefaultAsync(
                predicate : fp => fp.ProductId == model.ProductId && fp.CustomerId == customerId);

            if (relationship == null)
            {
                return(Result.NotFound());
            }

            FavoriteProductRepository.Delete(relationship);

            await UnitOfWork.SaveChangesAsync();

            return(Result.Success());
        }