예제 #1
0
        private void AttachCountry(TourDataModel tourDataModel)
        {
            var country = _dbContext.Countries
                          .FirstOrDefault(c => c.UrlName == tourDataModel.Country.UrlName && c.Category == tourDataModel.Country.Category);

            tourDataModel.Country = country;
        }
예제 #2
0
        public bool CheckTourUnique(TourDataModel tourDataModel)
        {
            AttachCountry(tourDataModel);
            var result = _dbContext.Tours.AsNoTracking().FirstOrDefault(c => c.TourId != tourDataModel.TourId &&
                                                                        c.UrlName == tourDataModel.UrlName && c.Country.UrlName == tourDataModel.Country.UrlName) == null;

            return(result);
        }
예제 #3
0
        public TourDataModel CreateTour(TourDataModel tourDataModel)
        {
            AttachCountry(tourDataModel);
            var result = _dbContext.Tours.Add(tourDataModel).Entity;

            _dbContext.SaveChanges();

            return(result);
        }
예제 #4
0
        private void AddImages(TourDataModel tourDataModel, ICollection <TourImage> tourImages)
        {
            var newImageCollection = tourImages
                                     .Where(i => tourDataModel.TourImages.All(db => db.ImageId != i.ImageId))
                                     .ToList();

            foreach (var newImage in newImageCollection)
            {
                tourDataModel.TourImages.Add(newImage);
            }
        }
예제 #5
0
        public void DeleteTour(TourDataModel tourDataModel)
        {
            var tour = _dbContext.Tours
                       .Include(t => t.TourImages)
                       .FirstOrDefault(t => t.TourId == tourDataModel.TourId);

            if (tour != null)
            {
                RemoveTourImages(tour);
                _dbContext.Tours.Remove(tour);

                _dbContext.SaveChanges();
            }
        }
예제 #6
0
        private void DeleteImages(TourDataModel tourDataModel, ICollection <TourImage> tourImages)
        {
            var tourImageForDeleteCollection = tourDataModel.TourImages.Where(db => tourImages.All(i => i.ImageId != db.ImageId)).ToList();

            foreach (var tourImageForDelete in tourImageForDeleteCollection)
            {
                tourDataModel.TourImages.Remove(tourImageForDelete);
                var image = _dbContext.Images.FirstOrDefault(i => i.ImageId == tourImageForDelete.ImageId);
                if (image != null)
                {
                    _dbContext.Images.Remove(image);
                }
            }
        }
예제 #7
0
        public void UpdateTour(TourDataModel tourDataModel)
        {
            var tourImages = tourDataModel.TourImages;

            tourDataModel.TourImages = new HashSet <TourImage>();
            tourDataModel.CountryId  = tourDataModel.Country.CountryId;

            var entry = _dbContext.Entry(tourDataModel);

            entry.State = EntityState.Modified;
            entry.Collection(c => c.TourImages).Load();

            UpdateImages(tourDataModel, tourImages);

            _dbContext.SaveChanges();
        }
예제 #8
0
        private void RemoveTourImages(TourDataModel tourDataModel)
        {
            var tourImageCollection = tourDataModel.TourImages?.ToList();

            if (tourImageCollection != null && tourImageCollection.Any())
            {
                foreach (var tourImage in tourImageCollection)
                {
                    var image = tourImage.Image;
                    if (image != null)
                    {
                        _dbContext.Images.Remove(image);
                    }
                }
            }
        }
예제 #9
0
 private void UpdateImages(TourDataModel tourDataModel, ICollection <TourImage> tourImages)
 {
     AddImages(tourDataModel, tourImages);
     DeleteImages(tourDataModel, tourImages);
 }