private void AttachCountry(TourDataModel tourDataModel) { var country = _dbContext.Countries .FirstOrDefault(c => c.UrlName == tourDataModel.Country.UrlName && c.Category == tourDataModel.Country.Category); tourDataModel.Country = country; }
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); }
public TourDataModel CreateTour(TourDataModel tourDataModel) { AttachCountry(tourDataModel); var result = _dbContext.Tours.Add(tourDataModel).Entity; _dbContext.SaveChanges(); return(result); }
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); } }
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(); } }
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); } } }
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(); }
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); } } } }
private void UpdateImages(TourDataModel tourDataModel, ICollection <TourImage> tourImages) { AddImages(tourDataModel, tourImages); DeleteImages(tourDataModel, tourImages); }