public List <GoodPresenter> FindInDb(string whatProperty, string thingToFind)
        {
            using (var DbContext = new InvoicingMachineDbContext())
            {
                List <GoodPresenter> goodsToDisplay = new List <GoodPresenter>();
                GoodPresenter        currentGood;
                switch (whatProperty)
                {
                case "Name":
                    var goodsFoundInDb = DbContext.Goods.Where(x => x.Name.Contains(thingToFind)).ToList();
                    foreach (var good in goodsFoundInDb)
                    {
                        currentGood = new GoodPresenter(good.GoodID, good.GoodKey, good.Name, good.Unit, good.VAT, good.NetPrice, good.GrossPrice);
                        goodsToDisplay.Add(currentGood);
                    }
                    break;

                case "Key":
                    goodsFoundInDb = DbContext.Goods.Where(x => x.GoodKey.Contains(thingToFind)).ToList();
                    foreach (var good in goodsFoundInDb)
                    {
                        currentGood = new GoodPresenter(good.GoodID, good.GoodKey, good.Name, good.Unit, good.VAT, good.NetPrice, good.GrossPrice);
                        goodsToDisplay.Add(currentGood);
                    }
                    break;
                }
                return(goodsToDisplay);
            }
        }
 public List <GoodPresenter> ReadFromDb()
 {
     using (var DbContext = new InvoicingMachineDbContext())
     {
         var goodsFromDb = DbContext.Goods.OrderBy(g => g.GoodID);
         List <GoodPresenter> goodsPresenter = new List <GoodPresenter>();
         GoodPresenter        currentGood;
         foreach (var good in goodsFromDb)
         {
             currentGood = new GoodPresenter(good.GoodID, good.GoodKey, good.Name, good.Unit, good.VAT, good.NetPrice, good.GrossPrice);
             goodsPresenter.Add(currentGood);
         }
         return(goodsPresenter);
     }
 }
 public bool UpdateGoodToDb(GoodPresenter goodToUpdate)
 {
     using (var Dbcontext = new InvoicingMachineDbContext())
     {
         bool goodIsChange = false;
         var  goodInDb     = Dbcontext.Goods.Find(goodToUpdate.ID);
         if (goodInDb.GoodKey != goodToUpdate.GoodKey && !string.IsNullOrWhiteSpace(goodToUpdate.GoodKey) && goodToUpdate.GoodKey.Length <= 20)
         {
             goodInDb.GoodKey = goodToUpdate.GoodKey;
             goodIsChange     = true;
         }
         if (goodInDb.Name != goodToUpdate.Name && !string.IsNullOrWhiteSpace(goodToUpdate.Name))
         {
             goodInDb.Name = goodToUpdate.Name;
             goodIsChange  = true;
         }
         if (goodInDb.Unit != goodToUpdate.Unit && !string.IsNullOrWhiteSpace(goodToUpdate.Unit))
         {
             goodInDb.Unit = goodToUpdate.Unit;
             goodIsChange  = true;
         }
         if (goodInDb.VAT != goodToUpdate.VAT)
         {
             goodInDb.VAT = goodToUpdate.VAT;
             goodIsChange = true;
         }
         if (goodInDb.NetPrice != goodToUpdate.NetPrice && !float.IsNaN(goodToUpdate.NetPrice))
         {
             goodInDb.NetPrice = goodToUpdate.NetPrice;
             goodIsChange      = true;
         }
         if (goodInDb.GrossPrice != goodToUpdate.GrossPrice && !float.IsNaN(goodToUpdate.GrossPrice))
         {
             goodInDb.GrossPrice = goodToUpdate.GrossPrice;
             goodIsChange        = true;
         }
         if (goodIsChange)
         {
             Dbcontext.SaveChanges();
         }
         return(goodIsChange);
     }
 }
        public bool AddGoodToDb(GoodPresenter goodToAdd)
        {
            using (var DbContext = new InvoicingMachineDbContext())
            {
                var  newGood       = new GoodModel();
                bool goodIsCorrect = false;
                if (!string.IsNullOrWhiteSpace(goodToAdd.GoodKey) && goodToAdd.GoodKey.Length <= 20)
                {
                    newGood.GoodKey = goodToAdd.GoodKey;
                    goodIsCorrect   = true;
                }
                else
                {
                    return(false);
                }

                if (!string.IsNullOrWhiteSpace(goodToAdd.Name))
                {
                    newGood.Name  = goodToAdd.Name;
                    goodIsCorrect = true;
                }
                else
                {
                    return(false);
                }

                if (!string.IsNullOrWhiteSpace(goodToAdd.Unit))
                {
                    newGood.Unit  = goodToAdd.Unit;
                    goodIsCorrect = true;
                }
                else
                {
                    return(false);
                }

                if (float.IsNaN(goodToAdd.VAT))
                {
                    newGood.VAT   = goodToAdd.VAT;
                    goodIsCorrect = true;
                }
                else
                {
                    return(false);
                }

                if (!float.IsNaN(goodToAdd.NetPrice))
                {
                    newGood.NetPrice = goodToAdd.NetPrice;
                    goodIsCorrect    = true;
                }
                else
                {
                    return(false);
                }

                if (!float.IsNaN(goodToAdd.GrossPrice))
                {
                    newGood.GrossPrice = goodToAdd.GrossPrice;
                    goodIsCorrect      = true;
                }
                else
                {
                    return(false);
                }

                if (goodIsCorrect)
                {
                    DbContext.Goods.Add(newGood);
                    DbContext.SaveChanges();
                }
                return(goodIsCorrect);
            }
        }