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 <ClientPresenter> FindInDb(string whatProperty, string thingToFind) { using (var DbContext = new InvoicingMachineDbContext()) { List <ClientPresenter> clientsToDisplay = new List <ClientPresenter>(); ClientPresenter currentClient; switch (whatProperty) { case "Name": var clientsFoundInDb = DbContext.Clients.Where(x => x.Name.Contains(thingToFind)).ToList(); foreach (var client in clientsFoundInDb) { currentClient = new ClientPresenter(client.ClientID, client.Name, client.Street, client.PostCode, client.City, client.NIP); clientsToDisplay.Add(currentClient); } break; case "NIP": clientsFoundInDb = DbContext.Clients.Where(x => x.NIP.Contains(thingToFind)).ToList(); foreach (var client in clientsFoundInDb) { currentClient = new ClientPresenter(client.ClientID, client.Name, client.Street, client.PostCode, client.City, client.NIP); clientsToDisplay.Add(currentClient); } break; } return(clientsToDisplay); } }
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 DeleteGoodFromDb(int goodToDeleteID) { bool goodRemoved = false; using (var DbContext = new InvoicingMachineDbContext()) { var goodToDelete = DbContext.Goods.Find(goodToDeleteID); if (goodToDelete != null) { DbContext.Goods.Remove(goodToDelete); DbContext.SaveChanges(); goodRemoved = true; } } return(goodRemoved); }
public bool DeleteClientFromDb(int clientToDeleteID) { bool clientRemoved = false; using (var DbContext = new InvoicingMachineDbContext()) { var clientToDelete = DbContext.Clients.Find(clientToDeleteID); if (clientToDelete != null) { DbContext.Clients.Remove(clientToDelete); DbContext.SaveChanges(); clientRemoved = true; } } return(clientRemoved); }
public List <ClientPresenter> ReadFromDb() { using (var DbContext = new InvoicingMachineDbContext()) { var clientsFromDb = from client in DbContext.Clients orderby client.ClientID ascending select client; List <ClientPresenter> clientsPresenter = new List <ClientPresenter>(); ClientPresenter currentClient; foreach (var client in clientsFromDb) { currentClient = new ClientPresenter(client.ClientID, client.Name, client.Street, client.PostCode, client.City, client.NIP); clientsPresenter.Add(currentClient); } return(clientsPresenter); } }
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 UpdateClientToDb(ClientPresenter clientToUpdate) { using (var Dbcontext = new InvoicingMachineDbContext()) { bool clientIsChange = false; var clientInDb = Dbcontext.Clients.Find(clientToUpdate.ID); if (clientInDb.Name != clientToUpdate.Name && !string.IsNullOrWhiteSpace(clientToUpdate.Name)) { clientInDb.Name = clientToUpdate.Name; clientIsChange = true; } if (clientInDb.Street != clientToUpdate.Street && !string.IsNullOrWhiteSpace(clientToUpdate.Street)) { clientInDb.Street = clientToUpdate.Street; clientIsChange = true; } if (clientInDb.PostCode != clientToUpdate.PostCode && !string.IsNullOrWhiteSpace(clientToUpdate.PostCode) && clientToUpdate.PostCode.Length == 6) { clientInDb.PostCode = clientToUpdate.PostCode; clientIsChange = true; } if (clientInDb.City != clientToUpdate.City && !string.IsNullOrWhiteSpace(clientToUpdate.City)) { clientInDb.City = clientToUpdate.City; clientIsChange = true; } if (clientInDb.NIP != clientToUpdate.NIP && !string.IsNullOrWhiteSpace(clientToUpdate.NIP)) { clientInDb.NIP = clientToUpdate.NIP; clientIsChange = true; } if (clientIsChange) { Dbcontext.SaveChanges(); } return(clientIsChange); } }
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); } }
public bool AddClientToDb(ClientPresenter clientToAdd) { using (var DbContext = new InvoicingMachineDbContext()) { var newClient = new ClientModel(); bool clientIsCorrect = false; if (!string.IsNullOrWhiteSpace(clientToAdd.Name)) { newClient.Name = clientToAdd.Name; clientIsCorrect = true; } else { return(false); } if (!string.IsNullOrWhiteSpace(clientToAdd.Street)) { newClient.Street = clientToAdd.Street; clientIsCorrect = true; } else { return(false); } if (!string.IsNullOrWhiteSpace(clientToAdd.PostCode) && clientToAdd.PostCode.Length == 6) { newClient.PostCode = clientToAdd.PostCode; clientIsCorrect = true; } else { return(false); } if (!string.IsNullOrWhiteSpace(clientToAdd.City)) { newClient.City = clientToAdd.City; clientIsCorrect = true; } else { return(false); } if (!string.IsNullOrWhiteSpace(clientToAdd.NIP)) { newClient.NIP = clientToAdd.NIP; clientIsCorrect = true; } else { return(false); } if (clientIsCorrect) { DbContext.Clients.Add(newClient); DbContext.SaveChanges(); } return(clientIsCorrect); } }