public List <TravelViewModel> GetFilteredList(TravelBindingModel model)
        {
            if (model == null)
            {
                return(null);
            }

            using (var context = new TourFirmDatabase())
            {
                return(context.Travels
                       .Include(rec => rec.TravelTours)
                       .ThenInclude(rec => rec.Tour)
                       .Include(rec => rec.TravelExcursions)
                       .ThenInclude(rec => rec.Excursion)
                       .Where(rec => (model.DateFrom.HasValue && model.DateTo.HasValue && rec.DateStart.Date >= model.DateFrom.Value.Date &&
                                      rec.DateEnd.Date <= model.DateTo.Value.Date && rec.TouristID == model.TouristID) ||
                              (!model.DateFrom.HasValue && !model.DateTo.HasValue && rec.TouristID == model.TouristID))
                       .ToList().
                       Select(rec => new TravelViewModel
                {
                    ID = rec.ID,
                    Name = rec.Name,
                    DateStart = rec.DateStart,
                    DateEnd = rec.DateEnd,
                    TouristID = rec.TouristID,
                    TravelTours = rec.TravelTours.ToDictionary(recTT => recTT.TourID, recTT => (recTT.Tour?.Name)),
                    TravelExcursions = rec.TravelExcursions.ToDictionary(recTE => recTE.ExcursionID, recTE => (recTE.Excursion?.Name))
                }).ToList());
            }
        }
        public TravelViewModel GetElement(TravelBindingModel model)
        {
            if (model == null)
            {
                return(null);
            }

            using (var context = new TourFirmDatabase())
            {
                Travel travel = context.Travels
                                .Include(rec => rec.TravelTours)
                                .ThenInclude(rec => rec.Tour)
                                .Include(rec => rec.TravelExcursions)
                                .ThenInclude(rec => rec.Excursion)
                                .FirstOrDefault(rec => rec.Name == model.Name || rec.ID == model.ID);
                return(travel != null ? new TravelViewModel
                {
                    ID = travel.ID,
                    Name = travel.Name,
                    DateStart = travel.DateStart,
                    DateEnd = travel.DateEnd,
                    TouristID = travel.TouristID,
                    TravelTours = travel.TravelTours.ToDictionary(recTT => recTT.TourID, recTT => (recTT.Tour?.Name)),
                    TravelExcursions = travel.TravelExcursions.ToDictionary(recTE => recTE.ExcursionID, recTE => (recTE.Excursion?.Name))
                } : null);
            }
        }
        public void Update(TravelBindingModel model)
        {
            using (var context = new TourFirmDatabase())
            {
                using (var transaction = context.Database.BeginTransaction())
                {
                    try
                    {
                        Travel element = context.Travels.FirstOrDefault(rec => rec.ID == model.ID);

                        if (element == null)
                        {
                            throw new Exception("Элемент не найден");
                        }

                        CreateModel(model, element, context);
                        transaction.Commit();
                    }
                    catch
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
 public ReportTravelGuidesViewModel GetTravelGuides(TravelBindingModel model)
 {
     using (var context = new TourFirmDatabase())
     {
         var guides = from travel in context.Travels
                      where travel.ID == model.ID
                      join travelExcursion in context.TravelExcursions
                      on travel.ID equals travelExcursion.TravelID
                      join guideExcursion in context.GuideExcursions
                      on travelExcursion.ExcursionID equals guideExcursion.ExcursionID
                      join guide in context.Guides
                      on guideExcursion.GuideID equals guide.ID
                      select new GuideViewModel
         {
             Name               = guide.Name,
             Surname            = guide.Surname,
             PhoneNumber        = guide.PhoneNumber,
             WorkPlace          = guide.WorkPlace,
             MainLanguage       = guide.MainLanguage,
             AdditionalLanguage = guide.AdditionalLanguage
         };
         return(new ReportTravelGuidesViewModel
         {
             TravelName = model.Name,
             Guides = guides.ToList()
         });
     }
 }
 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());
     }
 }
Beispiel #7
0
        public void Delete(TravelBindingModel model)
        {
            var element = _travelStorage.GetElement(new TravelBindingModel {
                ID = model.ID
            });

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            _travelStorage.Delete(model);
        }
Beispiel #8
0
        public List <TravelViewModel> Read(TravelBindingModel model)
        {
            if (model == null)
            {
                return(_travelStorage.GetFullList());
            }

            if (model.ID.HasValue)
            {
                return(new List <TravelViewModel> {
                    _travelStorage.GetElement(model)
                });
            }
            return(_travelStorage.GetFilteredList(model));
        }
        public void Delete(TravelBindingModel model)
        {
            using (var context = new TourFirmDatabase())
            {
                Travel element = context.Travels.FirstOrDefault(rec => rec.ID == model.ID);

                if (element != null)
                {
                    context.Travels.Remove(element);
                    context.SaveChanges();
                }
                else
                {
                    throw new Exception("Элемент не найден");
                }
            }
        }
Beispiel #10
0
 public void Insert(TravelBindingModel model)
 {
     using (var context = new TourFirmDatabase())
     {
         using (var transaction = context.Database.BeginTransaction())
         {
             try
             {
                 CreateModel(model, new Travel(), context);
                 transaction.Commit();
             }
             catch
             {
                 transaction.Rollback();
                 throw;
             }
         }
     }
 }
Beispiel #11
0
        public void CreateOrUpdate(TravelBindingModel model)
        {
            var element = _travelStorage.GetElement(new TravelBindingModel
            {
                Name = model.Name
            });

            if (element != null && element.ID != model.ID)
            {
                throw new Exception("Уже есть путешествие с таким названием");
            }

            if (model.ID.HasValue)
            {
                _travelStorage.Update(model);
            }
            else
            {
                _travelStorage.Insert(model);
            }
        }
Beispiel #12
0
        private Travel CreateModel(TravelBindingModel model, Travel travel, TourFirmDatabase context)
        {
            travel.Name      = model.Name;
            travel.DateStart = model.DateStart;
            travel.DateEnd   = model.DateEnd;
            travel.TouristID = model.TouristID;

            if (travel.ID == 0)
            {
                context.Travels.Add(travel);
                context.SaveChanges();
            }

            if (model.ID.HasValue)
            {
                List <TravelTour> travelTours = context.TravelTours
                                                .Where(rec => rec.TravelID == model.ID.Value).ToList();
                context.TravelTours.RemoveRange(travelTours
                                                .Where(rec => !model.TravelTours.ContainsKey(rec.TourID)).ToList());

                List <TravelExcursion> travelExcursions = context.TravelExcursions
                                                          .Where(rec => rec.TravelID == model.ID.Value).ToList();
                context.TravelExcursions.RemoveRange(travelExcursions
                                                     .Where(rec => !model.TravelExcursions.ContainsKey(rec.ExcursionID)).ToList());

                context.SaveChanges();

                // Убираем повторы
                foreach (var travelTour in travelTours)
                {
                    if (model.TravelTours.ContainsKey(travelTour.TourID))
                    {
                        model.TravelTours.Remove(travelTour.TourID);
                    }
                }

                foreach (var travelExcursion in travelExcursions)
                {
                    if (model.TravelExcursions.ContainsKey(travelExcursion.ExcursionID))
                    {
                        model.TravelExcursions.Remove(travelExcursion.ExcursionID);
                    }
                }
                context.SaveChanges();
            }

            foreach (var tt in model.TravelTours)
            {
                context.TravelTours.Add(new TravelTour
                {
                    TravelID = travel.ID,
                    TourID   = tt.Key,
                });
                context.SaveChanges();
            }

            foreach (var te in model.TravelExcursions)
            {
                context.TravelExcursions.Add(new TravelExcursion
                {
                    TravelID    = travel.ID,
                    ExcursionID = te.Key,
                });
                context.SaveChanges();
            }
            return(travel);
        }
 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;
             }
         }
     }
 }