public void CreateOrUpdate(PaymentBindingModel model) { using (var context = new TravelAgencyDatabase()) { Payment element = context.Payments.FirstOrDefault(rec => rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть платеж с таким названием"); } if (model.Id.HasValue) { element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Payment(); context.Payments.Add(element); } element.TravelId = model.TravelId; element.ClientId = model.ClientId; element.Sum = model.Sum; element.DatePayment = model.DatePayment; context.SaveChanges(); } }
public void CreateOrUpdate(ClientBindingModel model) { using (var context = new TravelAgencyDatabase()) { Client element = context.Clients.FirstOrDefault(rec => rec.Login == model.Login && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть клиент с таким логином"); } if (model.Id.HasValue) { element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Client(); context.Clients.Add(element); } element.Email = model.Email; element.Login = model.Login; element.ClientFIO = model.ClientFIO; element.PhoneNumber = model.PhoneNumber; element.Block = false; element.Password = model.Password; context.SaveChanges(); } }
public void CreateOrUpdate(TourBindingModel model) { using (var context = new TravelAgencyDatabase()) { Tour element = context.Tours.FirstOrDefault(rec => rec.TourName == model.TourName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть тур с таким названием"); } if (model.Id.HasValue) { element = context.Tours.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Tour(); context.Tours.Add(element); } element.TourName = model.TourName; element.Country = model.Country; element.Duration = model.Duration; element.Cost = model.Cost; element.TypeOfAllocation = model.TypeOfAllocation; context.SaveChanges(); } }
public void Delete(TravelBindingModel model) { using (var context = new TravelAgencyDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { context.TravelTours.RemoveRange(context.TravelTours.Where(rec => rec.TravelId == model.Id)); Travel element = context.Travels.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Travels.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void Delete(PaymentBindingModel model) { using (var context = new TravelAgencyDatabase()) { Payment element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Payments.Remove(element); context.SaveChanges(); } else { throw new Exception("Элемент не найден"); } } }
public void CreateOrUpdate(TravelBindingModel model) { using (var context = new TravelAgencyDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Travel element = context.Travels.FirstOrDefault(rec => rec.TravelName == model.TravelName && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть путешествие с таким названием"); } if (model.Id.HasValue) { element = context.Travels.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Travel(); context.Travels.Add(element); } element.DateOfBuying = model.DateOfBuying; element.ClientId = model.ClientId; element.Duration = model.Duration; element.FinalCost = model.FinalCost; element.Status = model.Status; element.IsCredit = model.IsCredit; var groupTours = model.TravelTours .GroupBy(rec => rec.TourId) .Select(rec => new { TourId = rec.Key, Count = rec.Sum(r => r.Count) }); foreach (var groupTour in groupTours) { context.TravelTours.Add(new TravelTour { TravelId = element.Id, TourId = groupTour.TourId, Count = groupTour.Count }); context.SaveChanges(); transaction.Commit(); } } catch (Exception) { transaction.Rollback(); throw; } } } }