Example #1
0
        public IActionResult Index(string msg = null, string scsMsg = null)
        {
            if (UserService.CurrentUser == null || !UserService.CurrentUser.IsAuthenticate)
            {
                return(RedirectToAction("Index", "Login", new { msg = "Giriş yapmalısınız." }));
            }

            ViewData["ErrorMsg"]    = msg;
            ViewData["SuccessInfo"] = scsMsg;
            var model = _cartProductService.GetCartProducts(_cartService.GetUserCart().Id);

            CartViewModel cvm = new CartViewModel();

            if (model == null || model.LongCount <CartProduct>() == 0)
            {
                return(RedirectToAction("Index", "Home", new { msg = "Sepetinizde hiç ürün yok." }));
            }
            else
            {
                cvm.ProductAmountsOnCart = new List <int>();
                cvm.Products             = new List <Product>();
                cvm.TotalPrice           = 0;

                foreach (var item in model)
                {
                    Product p = _productService.Get(x => x.Id == item.ProductId);
                    cvm.TotalPrice += p.Price * item.OnCartAmount;
                    cvm.ProductAmountsOnCart.Add(item.OnCartAmount);
                    cvm.Products.Add(p);
                }
            }

            return(View(cvm));
        }
Example #2
0
        public ActionResult <IEnumerable <ProductForCheckout> > GetCartProducts(int clientId)
        {
            var client = clientService.GetClientById(clientId);

            if (client == null)
            {
                return(NotFound());
            }
            var cartProducts = cartProductService.GetCartProducts(client.Id);

            var categories = new List <CategoryForCartDto>();
            List <ProductForCheckout> allProducts = new List <ProductForCheckout>();

            foreach (var prod in cartProducts)
            {
                var product         = productService.GetProductById(prod.ProductId);
                var productCategory = categoryService.GetCategoryById(product.CategoryId);

                if (!categories.CategoryExists(productCategory))
                {
                    categories.Add(new CategoryForCartDto {
                        Id = productCategory.Id, Name = productCategory.Name, NbProducts = 1
                    });
                }
                else
                {
                    categories.Where(c => c.Id == productCategory.Id).FirstOrDefault().NbProducts++;
                }

                allProducts.Add(new ProductForCheckout
                {
                    Id          = product.Id,
                    Name        = product.Name,
                    ImageBase64 = product.ProductImages.FirstOrDefault().ImageBase64,
                    Amount      = prod.Amount,
                    //The following code line wont work if the amount can't be converted to int
                    TotalProductPrice = Math.Floor(1000 * (product.Price * Convert.ToInt32(prod.Amount))) / 1000,
                    Category          = productCategory.Name,
                    CategoryId        = productCategory.Id
                });
            }
            ClientForCartDto clientForCart = _mapper.Map <ClientForCartDto>(client);
            CartDto          cartDto       = new CartDto
            {
                Categories = categories,
                Products   = allProducts,
                Client     = clientForCart
            };

            return(Ok(cartDto));
        }
Example #3
0
        public ActionResult <Order> AddNewOrder(OrderForCreationDto orderDto)
        {
            var client = clientService.GetClientById(orderDto.ClientId);

            if (client == null)
            {
                return(NotFound());
            }

            //Calculate delivery price
            double deliveryPrice;

            if (orderDto.Distance >= 0 && orderDto.Distance <= 10)
            {
                deliveryPrice = 2;
            }
            else
            {
                if (orderDto.Distance > 10 && orderDto.Distance <= 25)
                {
                    deliveryPrice = 5;
                }
                else
                {
                    deliveryPrice = 10;
                }
            }

            var cartProducts = cartProductService.GetCartProducts(orderDto.ClientId);

            double totalPrice = 0;

            foreach (var prod in cartProducts)
            {
                var product = productService.GetProductById(prod.ProductId);
                if (product != null)
                {
                    //The following code line wont work if the amount can't be converted to int
                    totalPrice += Math.Floor(1000 * (product.Price * Convert.ToInt32(prod.Amount))) / 1000;
                }
            }

            var order = orderService.AddOrder(new Order
            {
                IdClient      = client.Id,
                DeliveryPrice = deliveryPrice,
                OrderPrice    = totalPrice,
                OrderTime     = DateTime.Now,
                Status        = EnumOrderStatus.Pending,
                WithBill      = orderDto.WithBill
            });

            foreach (var prod in cartProducts)
            {
                //Add each product that was in the cart to the table ProductOrder
                var product = productService.GetProductById(prod.ProductId);
                if (product != null)
                {
                    var orderProduct = productOrderService.AddProduct(new ProductOrder
                    {
                        IdProduct = product.Id,
                        Amount    = prod.Amount,
                        IdOrder   = order.Id
                    });
                }
            }

            //Empty the client cart
            cartProductService.RemoveAllProducts(client.Id);

            //Send email to admins
            var admin = adminService.GetAdminByEmail("*****@*****.**");

            if (admin != null)
            {
                var msg = "<span>Bonjour <strong>" + admin.FirstName + " " + admin.LastName + "</strong>,</span>" +
                          "<br />" +
                          "<p>Une nouvelle commande a été enregistrée.</p>" +
                          "<p>Connectez-vous à l'application pour avoir plus de détails en cliquant <a href='https://localhost:44352/Order/PendingOrders'>ici</a></p>";

                emailSenderService.SendEmail(admin.Email, "Nouvelle commande", msg);
            }

            return(Ok(order));
        }