Пример #1
0
        public async Task <ActionResult> DeleteProductPhoto(int productId, int photoId)
        {
            var spec    = new ProductsWithTypesAndBrandsAndPhotosSpecification(productId);
            var product = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

            var photo = product.Photos.SingleOrDefault(x => x.Id == photoId);

            if (photo == null)
            {
                return(BadRequest(new ApiResponse(404, "Product photo not found")));
            }

            if (photo.IsMain)
            {
                return(BadRequest(new ApiResponse(400, "You can not delete the main photo")));
            }

            _photoService.DeletePhoto(photo.PublicId);

            product.Photos.Remove(photo);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem deleting photo product")));
            }

            return(Ok());
        }
Пример #2
0
        public async Task <ActionResult> DeleteProduct(int id)
        {
            var spec    = new ProductsWithTypesAndBrandsAndPhotosSpecification(id);
            var product = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

            if (product == null)
            {
                return(BadRequest(new ApiResponse(404, "Product not found")));
            }

            foreach (var photo in product.Photos)
            {
                if (photo.PublicId != null)
                {
                    _photoService.DeletePhoto(photo.PublicId);
                }
            }

            _unitOfWork.Repository <Product>().Delete(product);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem deleting product")));
            }
            return(Ok());
        }
Пример #3
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId,
                                                   Address shippingAddress)
        {
            // get basket from the repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

            // get items from the product repo
            var items = new List <OrderItem>();

            foreach (var item in basket.Items)
            {
                // var productItem = await _unitOfWork.Repository<Product>().GetByIdAsync(item.Id);
                var photoSpec   = new ProductsWithTypesAndBrandsAndPhotosSpecification(item.Id);
                var productItem = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(photoSpec);

                var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name,
                                                         productItem.Photos.FirstOrDefault(x => x.IsMain)?.Url);
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }

            //  get delivery method from repo
            var deliveryMethod = await _unitOfWork.Repository <DeliveryMethod>().GetByIdAsync(deliveryMethodId);

            // calc subtotal
            var subtotal = items.Sum(item => item.Price * item.Quantity);

            // check to see if order exists
            var spec          = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var existingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpecAsync(spec);

            if (existingOrder != null)
            {
                _unitOfWork.Repository <Order>().Delete(existingOrder);
                await _paymentService.CreateOrUpdatePaymentIntent(basket.PaymentIntentId);
            }

            // create order
            var order = new Order(items, buyerEmail, shippingAddress, deliveryMethod, subtotal, basket.PaymentIntentId);

            _unitOfWork.Repository <Order>().Add(order);

            // save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }

            // return order
            return(order);
        }
Пример #4
0
        public async Task <ActionResult <ProductToReturnDto> > GetProduct(int id)
        {
            var spec = new ProductsWithTypesAndBrandsAndPhotosSpecification(id);

            var product = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

            if (product == null)
            {
                return(NotFound(new ApiResponse(404)));
            }

            return(_mapper.Map <Product, ProductToReturnDto>(product));
        }
Пример #5
0
        public async Task <ActionResult <Pagination <ProductToReturnDto> > > GetProducts(
            [FromQuery] ProductSpecParams productParams)
        {
            var spec = new ProductsWithTypesAndBrandsAndPhotosSpecification(productParams);

            var countSpec = new ProductWithFiltersForCountSpecification(productParams);

            var totalItems = await _unitOfWork.Repository <Product>().CountAsync(countSpec);

            var products = await _unitOfWork.Repository <Product>().ListAsync(spec);

            var data = _mapper.Map <List <Product>, List <ProductToReturnDto> >(products.ToList());

            return(Ok(new Pagination <ProductToReturnDto>(productParams.PageIndex, productParams.PageSize, totalItems,
                                                          data)));
        }
Пример #6
0
        public async Task <ActionResult <ProductToReturnDto> > AddProductPhoto(int productId,
                                                                               [FromForm] ProductPhotoForCreationDto productPhotoForCreationDto)
        {
            var spec    = new ProductsWithTypesAndBrandsAndPhotosSpecification(productId);
            var product = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

            if (product == null)
            {
                return(BadRequest(new ApiResponse(404, "Product not found")));
            }

            var uploadResult = _photoService.AddPhoto(productPhotoForCreationDto.Photo);

            var photo = new Photo
            {
                ProductId = productId,
                Url       = uploadResult.Url,
                PublicId  = uploadResult.PublicId,
            };

            if (!product.Photos.Any(x => x.IsMain))
            {
                photo.IsMain = true;
            }


            // product.Photos.Add(photo);
            _unitOfWork.Repository <Photo>().Add(photo);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem adding photo product")));
            }

            return(_mapper.Map <Product, ProductToReturnDto>(product));
        }
Пример #7
0
        public async Task <ActionResult <ProductToReturnDto> > SetMainPhoto(int productId, int photoId)
        {
            var spec    = new ProductsWithTypesAndBrandsAndPhotosSpecification(productId);
            var product = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

            if (product.Photos.All(x => x.Id != photoId))
            {
                return(BadRequest(new ApiResponse(404, "Product photo not found")));
            }

            var photo = product.Photos.FirstOrDefault(x => x.Id == photoId);

            if (photo.IsMain)
            {
                return(BadRequest("This is already the main photo"));
            }

            var currentMainPhoto = product.Photos.FirstOrDefault(x => x.IsMain);

            if (currentMainPhoto != null)
            {
                currentMainPhoto.IsMain = false;
            }

            photo.IsMain = true;

            _unitOfWork.Repository <Product>().Update(product);

            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(BadRequest(new ApiResponse(400, "Problem adding photo product")));
            }

            return(_mapper.Map <Product, ProductToReturnDto>(product));
        }