//The function adds a delivery man to the data base or updates it in case that the delivery man is already exist public void Add_DeliveryMan(DeliveryMan dm) { using (var ctx = new DeliveryContext()) { try { if (ctx.DeliveryMans.Find(dm.Id) != null) { var old = ctx.DeliveryMans.Find(dm.Id); updateAddress(dm.Address); old.FirstName = dm.FirstName; old.LastName = dm.LastName; old.PhoneNumber = dm.PhoneNumber; old.Email = dm.Email; ctx.SaveChanges(); } else { ctx.Addresses.Add(dm.Address); ctx.DeliveryMans.Add(dm); ctx.SaveChanges(); } } catch { MessageBox.Show("Unable to connect to DataBase"); } } }
//The function adds an adult to the data base or updates it in case that the adult is already exist public void Add_Adult(Adult adult) { using (var ctx = new DeliveryContext()) { try { if (ctx.Adults.Find(adult.Id) != null) { var old = ctx.Adults.Find(adult.Id); updateAddress(adult.Address); old.FirstName = adult.FirstName; old.LastName = adult.LastName; old.PhoneNumber = adult.PhoneNumber; old.Medicine = adult.Medicine; old.Food = adult.Food; ctx.SaveChanges(); } else { ctx.Addresses.Add(adult.Address); ctx.Adults.Add(adult); ctx.SaveChanges(); } } catch { MessageBox.Show("Unable to connect to DataBase"); } } }
//The function adds a delivery to the data base or updates it in case that the delivery is already exist public void Add_Delivery(Delivery delivery) { using (var ctx = new DeliveryContext()) { try { var deliveryMan = ctx.DeliveryMans.Find(delivery.MyDeliveryMan.Id); var adult = ctx.Adults.Find(delivery.MyAdult.Id); delivery.MyAdult = null; delivery.MyDeliveryMan = null; ctx.Deliveries.Add(delivery); ctx.SaveChanges(); var deliveryToUpdate = ctx.Deliveries.Find(delivery.Code); deliveryToUpdate.MyAdult = adult; deliveryToUpdate.MyDeliveryMan = deliveryMan; ctx.SaveChanges(); } catch { MessageBox.Show("Unable to connect to DataBase"); } } }
//The function receives a delivery and defines it as "Done" public void IsTaskDone(List <Delivery> deliveries) { using (var context = new DeliveryContext()) { try { foreach (var delivery in deliveries) { var old = context.Deliveries.Find(delivery.Code); old.IsDone = delivery.IsDone; } context.SaveChanges(); } catch { MessageBox.Show("Unable to connect to DataBase"); } } }
//The function update an address by another parameter of address private void updateAddress(Address address) { using (var ctx = new DeliveryContext()) try { if (ctx.Addresses.Find(address.AddressID) != null) { var old = ctx.Addresses.Find(address.AddressID); old.Street = address.Street; old.BuildingNumber = address.BuildingNumber; old.City = address.City; ctx.SaveChanges(); } } catch { MessageBox.Show("Unable to connect to DataBase"); } }