Пример #1
0
        //Controleur de transformation du PANIER en COMMANDE
        public JsonResult AjouterCommande(string produitsIDs)
        {
            JsonResult result = new JsonResult();

            result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (!string.IsNullOrEmpty(produitsIDs))
            {
                //récupération de la quantité de chaque produits ajoutés

                var quantitesProduits = produitsIDs.Split('-').Select(x => int.Parse(x)).ToList();

                //récupération des produits sélectionnés
                var produitsAchetes = produitsService.GetProduits(quantitesProduits.Distinct().ToList());

                //Instanciation et remplissage de la commande
                Commande nouvelleCommande = new Commande();
                nouvelleCommande.UserID       = User.Identity.GetUserId();
                nouvelleCommande.DateCommande = DateTime.Now;
                nouvelleCommande.Statut       = "En attente de traitement";
                nouvelleCommande.MontantTotal = produitsAchetes.Sum(x => x.Prix * quantitesProduits.Where(produitsID => produitsID == x.ProduitID).Count());

                nouvelleCommande.ProduitsCommandes = new List <ProduitsCommande>();
                nouvelleCommande.ProduitsCommandes.AddRange(produitsAchetes.Select(x => new ProduitsCommande()
                {
                    ProduitID = x.ProduitID, Quantite = quantitesProduits.Where(produitID => produitID == x.ProduitID).Count()
                }));
                //Sauvegarde de la commande
                var changements = shopService.SaveCommande(nouvelleCommande);

                result.Data = new { Success = true, Rows = changements };
            }
            else
            {
                result.Data = new { Success = false };
            }

            return(result);
        }