public static bool FeaturedAdPromotionExpiry(int adId)
 {
     using (var CurrentDbContext = new ApplicationDbContext())
     {
         using (var _CurrentDbContext = CurrentDbContext.Database.BeginTransaction())
         {
             var ad = CurrentDbContext.ClassifiedDB.Include("AdPromotion").FirstOrDefault(x => x.Id == adId);
             if (ad != null)
             {
                 if (ad.AdPromotion.FeaturedAd != null && ad.AdPromotion.FeaturedAd.Status)
                 {
                     if (ad.AdPromotion.FeaturedAd.StartDate.Value.AddDays(ad.AdPromotion.FeaturedAd.Duration).Ticks - ad.AdPromotion.FeaturedAd.EndDate.Value.Ticks <= 0)
                     {
                         ad.AdPromotion.FeaturedAd.Status   = false;
                         ad.AdPromotion.FeaturedAd.Duration = 0;
                         CurrentDbContext.SaveChanges();
                         _CurrentDbContext.Commit();
                         // Update lucene
                         LuceneSearch.AddUpdateLuceneIndex(ad);
                         return(true);
                     }
                     else
                     {
                         BackgroundJob.Schedule(() => FeaturedAdPromotionExpiry(adId), ad.AdPromotion.FeaturedAd.EndDate.Value.Subtract(ad.AdPromotion.FeaturedAd.StartDate.Value));
                         return(false);
                     }
                 }
             }
             return(false);
         }
     }
 }
 /// <summary>
 /// Removes a classified ad with the specified adid
 /// </summary>
 /// <param name="adId"></param>
 /// <returns></returns>
 public static bool AdRemoval(int adId)
 {
     using (var CurrentDbContext = new ApplicationDbContext())
     {
         using (var _CurrentDbContext = CurrentDbContext.Database.BeginTransaction())
         {
             try
             {
                 var ad = CurrentDbContext.ClassifiedDB
                          .Include("Poster")
                          .Include("Country")
                          .Include("Region")
                          .Include("SubCategory")
                          .Include("Category")
                          .Include("AdPromotion")
                          .Include("Photos")
                          .Include("AdInfo")
                          .Include("Reports")
                          .Include("AdViews")
                          .SingleOrDefault(x => x.Id == adId);
                 if (ad == null)
                 {
                     return(false);
                 }
                 else if (ad.ExpiryTimeStamp.Value.Subtract(DateTime.Now).Ticks > 0)
                 {
                     ScheduleRemoval(ad.Id, DateTime.Now, ad.ExpiryTimeStamp.Value);
                     return(false);
                 }
                 //
                 var photolist = ad.Photos.ToList();
                 // remove category
                 if (ad.Category != null)
                 {
                     if (ad.Status == 0)
                     {
                         ad.Category.TotalClassifiedAdsCount--;
                     }
                     CurrentDbContext.SaveChanges();
                 }
                 // remove subcategory
                 if (ad.SubCategory != null)
                 {
                     if (ad.Status == 0)
                     {
                         ad.SubCategory.ClassifiedAdsCount--;
                     }
                     CurrentDbContext.SaveChanges();
                 }
                 // remove photos
                 if (ad.Photos != null)
                 {
                     CurrentDbContext.AdPhotosDB.RemoveRange(ad.Photos);
                     CurrentDbContext.SaveChanges();
                 }
                 // remove info
                 if (ad.AdInfo != null)
                 {
                     CurrentDbContext.InfosDB.RemoveRange(ad.AdInfo);
                     CurrentDbContext.SaveChanges();
                 }
                 // remove reports
                 if (ad.Reports != null)
                 {
                     CurrentDbContext.ReportDB.RemoveRange(ad.Reports);
                     ad.Reports = null;
                     CurrentDbContext.SaveChanges();
                 }
                 // remove ad promotions
                 if (ad.AdPromotion != null)
                 {
                     //ap.ClassifiedAd = null;
                     CurrentDbContext.AdPromotionDB.Remove(ad.AdPromotion);
                     CurrentDbContext.SaveChanges();
                 }
                 // remove ad
                 CurrentDbContext.ClassifiedDB.Remove(ad);
                 CurrentDbContext.SaveChanges();
                 // remove ad views
                 if (ad.AdViews != null)
                 {
                     CurrentDbContext.AdViewsDB.Remove(ad.AdViews);
                     CurrentDbContext.SaveChanges();
                 }
                 // Remove from favourites
                 if (CurrentDbContext.FavouirtedDb.Any(x => x.ClassifiedAdId == ad.Id))
                 {
                     var rem = CurrentDbContext.FavouirtedDb.Where(x => x.ClassifiedAdId == ad.Id);
                     CurrentDbContext.FavouirtedDb.RemoveRange(rem);
                     CurrentDbContext.SaveChanges();
                 }
                 _CurrentDbContext.Commit();
                 // Remove all images from filestore
                 ad.Photos = photolist;
                 LuceneSearch.DeletePhotosFromLuceneIndex(ad.Id, ad.StringId, photolist);
                 // remove from lucene index
                 LuceneSearch.ClearLuceneIndexRecord(ad.Id, photolist);
                 return(true);
             }
             catch (Exception)
             {
                 return(false);
             }
         }
     }
 }