コード例 #1
0
        public ActionResult <IEnumerable <CartProduct> > DeleteAllProductsFromCart([FromBody] CartClientDto cartClient)
        {
            var client = clientService.GetClientById(cartClient.ClientId);

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

            var deletedProducts = cartProductService.RemoveAllProducts(client.Id);

            return(Ok(deletedProducts));
        }
コード例 #2
0
        public async Task <IActionResult> DeleteClient(int id)
        {
            var client = clientService.GetClientById(id);

            if (client == null)
            {
                return(View("NotFound"));
            }
            var clientOrders = orderService.GetClientOrders(id);

            foreach (var ord in clientOrders)
            {
                var order = orderService.GetOrderById(ord.Id);
                order.IdClient = 0;

                orderService.EditOrder(order);
            }

            var favorites = favoritesService.GetFavoriteProducts(id);

            foreach (var fav in favorites)
            {
                favoritesService.RemoveProductFromFavorites(fav);
            }

            var ratings = ratingService.GetClientRatings(id);

            foreach (var rating in ratings)
            {
                ratingService.DeleteRating(rating);
            }

            cartProductService.RemoveAllProducts(id);

            clientService.DeleteClient(id);
            locationService.DeleteLocation(client.Location.Id);

            var user = await _userManager.FindByIdAsync(client.IdentityId);

            if (user != null)
            {
                await _userManager.DeleteAsync(user);
            }


            TempData["Message"] = "Client supprimé avec succès !";

            return(RedirectToAction("AllClients"));
        }
コード例 #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));
        }