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 <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 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 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);
            }
        }