Example #1
0
        public VendingMachineProduct Create(VendingMachineProduct request)
        {
            var defect = _repository.Add(request);

            _repository.Save();

            return(defect);
        }
 public static VendingMachineProductDTO ToDTO(this VendingMachineProduct ObjectToConvert)
 {
     return(new VendingMachineProductDTO()
     {
         Cost = ObjectToConvert.Cost,
         StockOnHand = ObjectToConvert.StockOnHand,
         VendingMachine = ObjectToConvert.VendingMachine.ToDTO(),
         Compartment = ObjectToConvert.Compartment.ToDTO(),
         Product = ObjectToConvert.Product.ToDTO()
     });
 }
Example #3
0
        public void TestInventory_ShouldReturnCorrectItemString()
        {
            Inventory inventory = new Inventory();
            Dictionary <string, VendingMachineProduct> actualOutput   = new Dictionary <string, VendingMachineProduct>();
            Dictionary <string, VendingMachineProduct> expectedReturn = new Dictionary <string, VendingMachineProduct>()
            {
                { "A1", new Chip("Potato Crisps", "3.05") },
                { "A2", new Chip("Stackers", "1.45") },
                { "A3", new Chip("Grain Waves", "2.75") },
                { "A4", new Chip("Cloud Popcorn", "3.65") },
                { "B1", new Candy("Moonpie", "1.80") },
                { "B2", new Candy("Cowtales", "1.50") },
                { "B3", new Candy("Wonka Bar", "1.50") },
                { "B4", new Candy("Crunchie", "1.75") },
                { "C1", new Drink("Cola", "1.25") },
                { "C2", new Drink("Dr. Salt", "1.50") },
                { "C3", new Drink("Mountain Melter", "1.50") },
                { "C4", new Drink("Heavy", "1.50") },
                { "D1", new Gum("U-Chews", "0.85") },
                { "D2", new Gum("Little League Chew", "0.95") },
                { "D3", new Gum("Chiclets", "0.75") },
                { "D4", new Gum("Triplemint", "0.75") }
            };

            inventory.StockMachine(actualOutput);

            foreach (KeyValuePair <string, VendingMachineProduct> kv in expectedReturn)
            {
                string expectedKey = kv.Key;

                // Tests if key exists
                bool hasKey = actualOutput.TryGetValue(expectedKey, out VendingMachineProduct actualValue);
                Assert.IsTrue(hasKey);

                // Tests if actual object value is not null
                Assert.IsNotNull(actualValue);

                // Tests if objects are the same, with the same properties
                VendingMachineProduct expectedValue = kv.Value;
                expectedValue.Should().BeEquivalentTo(actualValue);
            }
        }
Example #4
0
        public void InitializeDatabase(VendingMachineDbContext context)
        {
            context.Database.Delete();
            context.Database.Create();

            //initial data

            var tea = new Product
            {
                ProductName = "Tea"
            };
            var espresso = new Product
            {
                ProductName = "Espresso"
            };
            var juice = new Product
            {
                ProductName = "Juice"
            };
            var chickenSoup = new Product
            {
                ProductName = "Chicken Soup"
            };

            context.Products.Add(tea);
            context.Products.Add(espresso);
            context.Products.Add(juice);
            context.Products.Add(chickenSoup);

            var teaVending = new VendingMachineProduct
            {
                Price   = (decimal)1.30,
                Portion = 10,
                Product = tea
            };
            var espressoVending = new VendingMachineProduct
            {
                Price   = (decimal)1.80,
                Portion = 20,
                Product = espresso
            };
            var juiceVending = new VendingMachineProduct
            {
                Price   = (decimal)1.80,
                Portion = 20,
                Product = juice
            };
            var chickenSoupVending = new VendingMachineProduct
            {
                Price   = (decimal)1.80,
                Portion = 15,
                Product = chickenSoup
            };

            context.VendingMachineProducts.Add(teaVending);
            context.VendingMachineProducts.Add(espressoVending);
            context.VendingMachineProducts.Add(juiceVending);
            context.VendingMachineProducts.Add(chickenSoupVending);

            var tenCent = new Currency
            {
                CoinName = "10 Cent",
                Value    = (decimal)0.10
            };

            var twentyCent = new Currency
            {
                CoinName = "20 Cent",
                Value    = (decimal)0.20
            };

            var fiftyCent = new Currency
            {
                CoinName = "50 Cent",
                Value    = (decimal)0.50
            };

            var oneEuro = new Currency
            {
                CoinName = "1 Euro",
                Value    = 1
            };

            context.Currencies.Add(tenCent);
            context.Currencies.Add(twentyCent);
            context.Currencies.Add(fiftyCent);
            context.Currencies.Add(oneEuro);

            var tenCentVending = new VendingMachineCoin
            {
                Currency = tenCent,
                Pieces   = 100
            };

            var twentyCentVending = new VendingMachineCoin
            {
                Currency = twentyCent,
                Pieces   = 100
            };

            var fiftyCentVending = new VendingMachineCoin
            {
                Currency = fiftyCent,
                Pieces   = 100
            };

            var oneEuroVending = new VendingMachineCoin
            {
                Currency = oneEuro,
                Pieces   = 100
            };

            context.VendingMachineCoins.Add(tenCentVending);
            context.VendingMachineCoins.Add(twentyCentVending);
            context.VendingMachineCoins.Add(fiftyCentVending);
            context.VendingMachineCoins.Add(oneEuroVending);

            context.SaveChanges();
        }
        public IActionResult Create([FromBody] VendingMachineProduct request)
        {
            var actionResult = new CustomActionResult
            {
                Successful = true,
                Message    = "Product was successfull added for Vending Machine!"
            };
            var userId = CurrentLoggedUserId;

            try
            {
                request.SetAudit(userId);
                var vendingMachineProduct = _vendingMachineProductService.Create(request);
            }
            catch
            {
                actionResult.Successful = false;
                actionResult.Message    = "Product was unsuccessfull added for Vending Machine, please try again!";

                return(Ok(actionResult));
            }

            if (CurrentLoggedUserRole == Role.FieldWorker)
            {
                var product             = _productService.GetById(request.ProductId);
                var fieldWorkerProducts = _fieldWorkerProductService.GetByCriteria(new FieldWorkerProductSearchRequest
                {
                    FieldWorkerId = CurrentLoggedUserId,
                    ProductId     = request.ProductId
                });
                var fieldWorkerProduct = fieldWorkerProducts.FirstOrDefault();
                try
                {
                    product.Quantity -= request.Quantity;
                    product.SetAudit(userId);
                    _productService.Update(product);
                }
                catch
                {
                    actionResult.Successful = false;
                    actionResult.Message    = "Product was successfully added for Vending Machine, but Quantity for product was not updated, contact your superior!";

                    return(Ok(actionResult));
                }

                if (fieldWorkerProduct == null)
                {
                    actionResult.Successful = false;
                    actionResult.Message    = "Product was successfully added for Vending Machine, but field worker should not possess these product, contact your superior!";

                    return(Ok(actionResult));
                }
                else
                {
                    try
                    {
                        fieldWorkerProduct.Quantity -= request.Quantity;
                        fieldWorkerProduct.SetAudit(userId);
                        _fieldWorkerProductService.Update(fieldWorkerProduct);
                    }
                    catch
                    {
                        actionResult.Successful = false;
                        actionResult.Message    = "Product was successfully added for Vending Machine, but Quantity for field worker product was not updated, contact your superior!";

                        return(Ok(actionResult));
                    }
                }
            }

            return(Ok(actionResult));
        }