public async Task <ActionResult <ProductDto> > GetProduct(int id)
        {
            var spec = new ProductWithPhotosSpecification(id);

            var product = await _productRepo.GetEntityWithSpecAsync(spec);

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

            return(_mapper.Map <Product, ProductDto>(product));
        }
        public async Task <ActionResult <PhotoDto> > AddPhoto([FromForm] IFormFile file,
                                                              [FromRoute] int productId)
        {
            var product = await _productRepo.GetByIdAsync(productId);

            var result = await _photoService.AddPhotoAsync(file);

            if (result.Error != null)
            {
                return(BadRequest(result.Error.Message));
            }

            var photo = new Photo
            {
                Url       = result.SecureUrl.AbsoluteUri,
                PublicId  = result.PublicId,
                Product   = product,
                ProductId = product.Id
            };

            var spec = new ProductWithPhotosSpecification(productId);

            Product productWithPhotos = await _productRepo.GetEntityWithSpecAsync(spec);

            if (productWithPhotos.Photos.Count == 0)
            {
                photo.IsMain = true;
            }

            product.Photos.Add(photo);

            if (await _productRepo.SaveAllAsync())
            {
                return(CreatedAtRoute("GetProduct", new { id = productId },
                                      _mapper.Map <PhotoDto>(photo)));
            }

            return(BadRequest("failed to add photo"));
        }
        public async Task <Product> GetProductWithPhotos(int id)
        {
            var spec = new ProductWithPhotosSpecification(id);

            return(await _productRepo.GetEntityWithSpecAsync(spec));
        }
Beispiel #4
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int deliveryMethodId, string basketId,
                                                   string shippingAddress)
        {
            // get basket from repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

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

            foreach (var item in basket.ProductItems)
            {
                var spec        = new ProductWithPhotosSpecification(item.Id);
                var productItem = await _unitOfWork.Repository <Product>().GetEntityWithSpecAsync(spec);

                var itemOrdered = new ItemOrdered(productItem.Id, productItem.Name,
                                                  productItem.Photos.FirstOrDefault(x => x.IsMain).Url);
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }
            foreach (var item in basket.PartItems)
            {
                var spec     = new PartsSpecification(item.Id);
                var partItem = await _unitOfWork.Repository <Part>().GetEntityWithSpecAsync(spec);

                var itemOrdered = new ItemOrdered(partItem.Id, partItem.Name,
                                                  partItem.Photos.FirstOrDefault(x => x.IsMain).Url);
                var orderItem = new OrderItem(itemOrdered, partItem.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 orderSpec      = new OrderByPaymentIntentIdSpecification(basket.PaymentIntentId);
            var exisitingOrder = await _unitOfWork.Repository <Order>().GetEntityWithSpecAsync(orderSpec);

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

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

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

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

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



            // return order
            return(order);
        }
Beispiel #5
0
        public async Task <Order> CreateOrderAsync(string buyerEmail, int delieveryMethodId, string basketId, Address shippingAddress)
        {
            // get basket from repo
            var basket = await _basketRepo.GetBasketAsync(basketId);

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

            foreach (var item in basket.Items)
            {
                var spec        = new ProductWithPhotosSpecification(item.Id);
                var productItem = await _unitOfWork.Repository <Product>().GetEntityWithSpec(spec);

                var itemOrdered = new ProductItemOrdered(productItem.Id, productItem.Name, productItem.Photos.SingleOrDefault(x => Convert.ToInt32(x.IsMain) == 1)?.PictureUrl);
                mailOrderContent.Append($"<tr style=\"border-collapse:collapse\"><td width=\"80%\" style=\"padding:0;Margin:0\"><h4 style=\"Margin:0;line-height:120%;mso-line-height-rule:exactly;font-family:'open sans', 'helvetica neue', helvetica, arial, sans-serif\">{productItem.Name}</h4></td><td width=\"20%\" style=\"padding:0;Margin:0\"><h4 style=\"Margin:0;float:left;line-height:120%;mso-line-height-rule:exactly;font-family:'open sans', 'helvetica neue', helvetica, arial, sans-serif\">{item.Price} KM</h4></td><td width=\"20%\" style=\"padding:0;Margin:0\"><h4 style=\"Margin:0;line-height:120%;mso-line-height-rule:exactly;font-family:'open sans', 'helvetica neue', helvetica, arial, sans-serif\">{item.Quantity}</h4></td></tr> ");
                var orderItem = new OrderItem(itemOrdered, productItem.Price, item.Quantity);
                items.Add(orderItem);
            }

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

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

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

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

            _unitOfWork.Repository <Order>().Add(order);
            StringBuilder mailShippingInfo =
                new StringBuilder($"<p>{shippingAddress.FirstName} {shippingAddress.LastName}</p><p>{shippingAddress.Street}</p><p>{shippingAddress.City}</p><p>{shippingAddress.ZipCode}</p><p>{shippingAddress.State}</p>");

            /*if (existingOrder != null)
             * {
             *  _unitOfWork.Repository<Order>().Delete(existingOrder);
             *  await _paymentService.CreateOrUpdatePaymentIntent(basket.Id);
             * }*/
            // Update Stock
            foreach (var item in basket.Items)
            {
                var productItem = await _unitOfWork.Repository <Product>().GetByIdAsync(item.Id);

                productItem.Stock -= item.Quantity;
                _unitOfWork.Repository <Product>().Update(productItem);
            }
            // TODO: save to db
            var result = await _unitOfWork.Complete();

            if (result <= 0)
            {
                return(null);
            }
            else
            {
                // send mail
                await _mailService.SendOrderMailAsync(order.Id, mailOrderContent.ToString(), subtotal, mailShippingInfo.ToString());

                // return order
                return(order);
            }
        }