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 List <TravelViewModel> Read(TravelBindingModel model) { using (var context = new TravelAgencyDatabase()) { return(context.Travels.Where(rec => model == null || rec.Id == model.Id && model.Id.HasValue) .Select(rec => new TravelViewModel { Id = rec.Id, DateOfBuying = rec.DateOfBuying, TravelName = rec.TravelName, ClientId = rec.ClientId, Duration = rec.Duration, FinalCost = rec.FinalCost, Status = rec.Status, IsCredit = rec.IsCredit, ClientFIO = rec.Client.ClientFIO, TravelTours = context.TravelTours .Where(recCI => recCI.TravelId == rec.Id) .Select(recCI => new TravelTourViewModel { Id = recCI.Id, TravelId = recCI.TravelId, TourId = recCI.TourId, Count = recCI.Count }) .ToList() }) .ToList()); } }
public List <PaymentViewModel> Read(PaymentBindingModel model) { using (var context = new TravelAgencyDatabase()) { return(context.Payments .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new PaymentViewModel { Id = rec.Id, ClientId = rec.ClientId, DatePayment = rec.DatePayment, TravelId = rec.TravelId, Sum = rec.Sum }) .ToList()); } }
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 List <TourViewModel> Read(TourBindingModel model) { using (var context = new TravelAgencyDatabase()) { return(context.Tours .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new TourViewModel { Id = rec.Id, TourName = rec.TourName, Duration = rec.Duration, Cost = rec.Cost, TypeOfAllocation = rec.TypeOfAllocation, Country = rec.Country }) .ToList()); } }
public List <ClientViewModel> Read(ClientBindingModel model) { using (var context = new TravelAgencyDatabase()) { return(context.Clients .Where( rec => model == null || rec.Id == model.Id || rec.Login == model.Login && rec.Password == model.Password ) .Select(rec => new ClientViewModel { Id = rec.Id, Login = rec.Login, ClientFIO = rec.ClientFIO, Email = rec.Email, Password = rec.Password, PhoneNumber = rec.PhoneNumber, Block = rec.Block }) .ToList()); } }
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; } } } }