示例#1
0
        public void ResupplyAndReorderReadAndWrite()
        {
            string          path    = "../../../SimplyWriteData.json";
            JsonFilePersist persist = new JsonFilePersist(path);
            CStore          store   = persist.ReadStoreData();

            List <CProduct> supply = new List <CProduct> {
                new CProduct("111", "Banana", "Produce", 0.5, 10),
                new CProduct("222", "orange", "Produce", 0.88, 10),
                new CProduct("333", "Rocket", "Transport", 1000000, 15)
            };

            store.AddProducts(supply);
            CCustomer       customer = new CCustomer("127137147", "Adam", "Savage", "4801111111");
            List <CProduct> p        = new List <CProduct> {
                new CProduct("111", "Banana", "Produce", 0.5, 1),
                new CProduct("222", "orange", "Produce", 0.88, 1)
            };
            COrder order = new COrder(store, customer, DateTime.Today, 100, p);

            customer.PlaceOrder(store, order);

            persist.WriteStoreData(store);
            foreach (var pair in store.Inventory)
            {
                Assert.Equal(15, pair.Value.Quantity);
            }
        }
        public ActionResult CheckCart()
        {
            JsonFilePersist persist = new JsonFilePersist();

            if (!TempData.ContainsKey("Cart"))
            {
                return(View(new List <DetailedProductViewModel>()));
            }
            List <CProduct> products = persist.ReadProductsTempData(TempData.Peek("Cart").ToString());

            // empty cart to start with
            if (products == null)
            {
                return(View(new List <DetailedProductViewModel>()));
            }

            var viewProducts = ViewModelMapper.MapDetailedProducts(products);

            //fixed
            double total = 0;

            foreach (var item in viewProducts)
            {
                total += item.TotalCostPerProduct;
            }
            TempData["Total"] = total.ToString();
            TempData.Keep("Total");
            return(View(viewProducts));
        }
示例#3
0
        public void SimplyReadData()
        {
            string          path    = "../../../SimplyWriteData.json";
            JsonFilePersist persist = new JsonFilePersist(path);
            CStore          store   = persist.ReadStoreData();

            foreach (var product in store.CustomerDict["123123121"].OrderHistory[0].ProductList)
            {
                Assert.Equal(4, product.Quantity);
            }
        }
        public ActionResult AddToCart(string id, BindedProductViewModel bindedProduct)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "invalid input format");
                    return(View(bindedProduct));
                }

                // handle concurrency
                CProduct foundProduct = _storeRepo.GetOneProduct(id);
                if (foundProduct == null)
                {
                    ModelState.AddModelError("", "This product has just been deleted");
                    return(View(bindedProduct));
                }
                CProduct cProduct = new CProduct(foundProduct.UniqueID, foundProduct.Name, foundProduct.Category, foundProduct.Price,
                                                 bindedProduct.Quantity);

                // use tempdata to store products in a cart
                // do not know how to return a serialized string directly, use a local text file for now
                string          path    = "../../SimplyWriteData.json";
                JsonFilePersist persist = new JsonFilePersist(path);
                string          json    = "";
                if (TempData.ContainsKey("Cart"))
                {
                    json = TempData.Peek("Cart").ToString();
                }

                List <CProduct> products = persist.ReadProductsTempData(json);
                if (products == null)
                {
                    products = new List <CProduct>();
                }
                products.Add(cProduct);
                string cart = persist.WriteProductsTempData(products);
                TempData["Cart"] = cart;
                TempData.Keep("Cart");
                // route parameter is an object
                return(RedirectToAction("Select", "Store", new StoreViewModel {
                    Storeloc = TempData.Peek("storeLoc").ToString()
                }));
            }
            catch (Exception e)
            {
                _logger.LogError(e, "error while tring to add a product");
                ModelState.AddModelError("", "failed to create a product");
                return(View(bindedProduct));
            }
        }
示例#5
0
        public void SimplyWriteData()
        {
            List <CProduct> supply = new List <CProduct> {
                new CProduct("111", "Banana", "Produce", 0.5, 10),
                new CProduct("222", "orange", "Produce", 0.88, 10)
            };
            List <CProduct> p = new List <CProduct> {
                new CProduct("111", "Banana", "Produce", 0.5, 4),
                new CProduct("222", "orange", "Produce", 0.88, 4)
            };
            CStore    store    = new CStore("Phoenix101", "606", supply);
            CCustomer customer = new CCustomer("123123121", "John", "Smith", "6021111111");
            // orders the same as the store's inventory
            COrder        order = new COrder(store, customer, DateTime.Today, 100, p);
            SimpleDisplay dis   = new SimpleDisplay();

            string          path    = "../../../SimplyWriteData.json";
            JsonFilePersist persist = new JsonFilePersist(path);

            customer.PlaceOrder(store, order);
            persist.WriteStoreData(store);
        }
        public ActionResult Delete(string id)
        {
            JsonFilePersist          persist  = new JsonFilePersist();
            List <CProduct>          products = persist.ReadProductsTempData(TempData.Peek("Cart").ToString());
            CProduct                 foundProduct;
            DetailedProductViewModel viewProduct;

            if (products == null)
            {
                return(RedirectToAction("CheckCart"));
            }
            foreach (var product in products)
            {
                if (product.UniqueID == id)
                {
                    foundProduct = product;
                    viewProduct  = ViewModelMapper.MapSingleDetailedProductWithoutTotal(foundProduct);
                    return(View(viewProduct));
                }
            }
            return(View());
        }
        public ActionResult Delete(string id, DetailedProductViewModel viewDP)
        {
            string          path     = "../../SimplyWriteData.json";
            JsonFilePersist persist  = new JsonFilePersist(path);
            List <CProduct> products = persist.ReadProductsTempData(TempData.Peek("Cart").ToString());

            if (products == null)
            {
                return(RedirectToAction("CheckCart"));
            }
            foreach (var product in products)
            {
                if (product.UniqueID == id)
                {
                    products.Remove(product);
                    break;
                }
            }
            string cart = persist.WriteProductsTempData(products);

            TempData["Cart"] = cart;
            TempData.Keep("Cart");
            return(RedirectToAction("CheckCart"));
        }
        public ActionResult Checkout()
        {
            JsonFilePersist persist  = new JsonFilePersist();
            List <CProduct> products = persist.ReadProductsTempData(TempData.Peek("Cart").ToString());

            if (products == null)
            {
                return(RedirectToAction("CheckCart"));
            }

            // orderid , store, customer, ordertime, totalcost
            string    orderID   = Guid.NewGuid().ToString().Substring(0, 10);
            string    storeLoc  = TempData.Peek("storeLoc").ToString();
            CStore    store     = _storeRepo.GetOneStore(storeLoc);
            string    email     = TempData.Peek("User").ToString();
            CCustomer cCustomer = _storeRepo.GetOneCustomerByEmail(email);
            // recalculate price in case customers change quantity in carts
            double totalCost = store.CalculateTotalPrice(products);

            COrder newOrder = new COrder(orderID, store, cCustomer, totalCost);

            // check against inventory
            var inventory = _storeRepo.GetInventoryOfOneStore(storeLoc);

            store.CleanInventory();
            store.AddProducts(inventory);

            bool isSuccessful = false;

            if (store.CalculateThreshhold(products))
            {
                // quantity limits
                newOrder.ProductList = products;
                isSuccessful         = true;
            }
            else
            {
                ModelState.AddModelError("", "Quantity set exceeds the maximum allowed");
                _logger.LogError("Quantity set exceeds the maximum allowed");
                return(RedirectToAction("CheckCart"));
            }
            if (isSuccessful)
            {
                if (!store.CheckInventory(newOrder))
                {
                    ModelState.AddModelError("", "Not enough left in the inventory");
                    _logger.LogError("Not enough left in the inventory");
                    return(RedirectToAction("CheckCart"));
                }
                else
                {
                    store.UpdateInventory(newOrder);
                    _storeRepo.CustomerPlaceOneOrder(newOrder, store, totalCost);
                }
            }

            // clean cart
            StreamWriter sw = new StreamWriter("../../SimplyWriteData.json");

            sw.WriteLine(string.Empty);
            sw.Close();
            TempData.Remove("Cart");
            TempData.Remove("Total");
            return(View());
        }