public ReportTourExcursionsViewModel GetTourExcursion(TourBindingModel model) { using (var context = new TourFirmDatabase()) { var excusrions = from tour in context.Tours where tour.ID == model.ID join tourguide in context.TourGuides on tour.ID equals tourguide.TourID join guide in context.Guides on tourguide.GuideID equals guide.ID join guideexcurion in context.GuideExcursions on guide.ID equals guideexcurion.GuideID join excursion in context.Excursions on guideexcurion.ExcursionID equals excursion.ID select new ExcursionViewModel { Name = excursion.Name, Price = excursion.Price, Duration = excursion.Duration }; return(new ReportTourExcursionsViewModel { TourName = model.Name, Excursions = excusrions.ToList() }); } }
public void Update(TourBindingModel model) { using (var context = new TourFirmDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { var element = context.Tours.FirstOrDefault(rec => rec.ID == model.ID); if (element == null) { throw new Exception("Элемент не найден"); } CreateModel(model, element, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
public void Insert(TourBindingModel model) { using (var context = new TourFirmDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Tour tour = new Tour { Name = model.Name, Price = model.Price, Country = model.Country, HaltID = model.HaltID, OperatorID = model.OperatorID }; context.Tours.Add(tour); context.SaveChanges(); CreateModel(model, tour, context); context.SaveChanges(); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } }
private Tour CreateModel(TourBindingModel model, Tour tour, TourFirmDatabase context) { tour.Name = model.Name; tour.Country = model.Country; tour.Price = model.Price; tour.HaltID = model.HaltID; tour.OperatorID = model.OperatorID; if (model.ID.HasValue) { var tourGuides = context.TourGuides.Where(rec => rec.TourID == model.ID.Value).ToList(); // удалили те, которых нет в модели context.TourGuides.RemoveRange(tourGuides.ToList()); context.SaveChanges(); } // добавили новые foreach (var tg in model.TourGuides) { context.TourGuides.Add(new TourGuide { TourID = tour.ID, GuideID = tg.Key, }); context.SaveChanges(); } return(tour); }
public TourViewModel GetElement(TourBindingModel model) { if (model == null) { return(null); } using (var context = new TourFirmDatabase()) { var tour = context.Tours .Include(rec => rec.TourGuides) .ThenInclude(rec => rec.Guide) .FirstOrDefault(rec => rec.ID == model.ID); return(tour != null ? new TourViewModel { ID = tour.ID, Name = tour.Name, Country = tour.Country, Price = tour.Price, OperatorID = tour.OperatorID, HaltID = tour.HaltID, TourGuides = tour.TourGuides .ToDictionary(recTG => recTG.GuideID, recTG => (recTG.Guide?.Surname)) } : null); } }
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(TourBindingModel model) { var element = _tourStorage.GetElement(new TourBindingModel { ID = model.ID }); if (element == null) { throw new Exception("Элемент не найден"); } _tourStorage.Delete(model); }
public List <TourViewModel> Read(TourBindingModel model) { if (model == null) { return(_tourStorage.GetFullList()); } if (model.ID.HasValue) { return(new List <TourViewModel> { _tourStorage.GetElement(model) }); } return(_tourStorage.GetFilteredList(model)); }
public void Delete(TourBindingModel model) { using (var context = new TourFirmDatabase()) { Tour element = context.Tours.FirstOrDefault(rec => rec.ID == model.ID); if (element != null) { context.Tours.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 void CreateOrUpdate(TourBindingModel model) { var element = _tourStorage.GetElement(new TourBindingModel { Name = model.Name }); if (element != null && element.ID != model.ID) { throw new Exception("Данный тур уже зарегистрирован"); } if (model.ID.HasValue) { _tourStorage.Update(model); } else { _tourStorage.Insert(model); } }
public List <TourViewModel> GetFilteredList(TourBindingModel model) { using (var context = new TourFirmDatabase()) { return(context.Tours .Include(rec => rec.TourGuides) .ThenInclude(rec => rec.Guide) .Where(rec => (rec.OperatorID == model.OperatorID)) .ToList() .Select(rec => new TourViewModel { ID = rec.ID, Name = rec.Name, Country = rec.Country, Price = rec.Price, OperatorID = rec.OperatorID, HaltID = rec.HaltID, TourGuides = rec.TourGuides .ToDictionary(recTG => recTG.GuideID, recTG => (recTG.Guide?.Surname)) }) .ToList()); } }