public FeedContainerVM PopulateFeedContainer(FeedContainerInputModel searchModel = null) { var userId = 1; //harcoded userId for demo purposes FeedContainerVM feedContainerVM = new FeedContainerVM(); feedRepository = new FeedRepository(new FeedContext()); subscriptionRepository = new SubscriptionRepository(new FeedContext()); temporalFeedRepository = new TemporalFeedRepository(new FeedContext()); feedContainerVM.feeds = ConvertToSubscription().Where(x => x.isSubscribed == true); feedContainerVM.hasSubscriptions = hasSubscriptions(); List <FeedItemEntity> downloadFeeds = new List <FeedItemEntity>(); //TODO: Search parameter if (searchModel == null) { var syncedFeeds = temporalFeedRepository.FindBy(x => x.userId == userId).ToList(); var lastSyncedItem = syncedFeeds.FirstOrDefault(); TimeSpan elapsedTime = TimeSpan.MinValue; if (lastSyncedItem != null) { elapsedTime = DateTime.Now - lastSyncedItem.lastSynced; } //Syncronize every 8 hours if (elapsedTime.Hours >= 8 || lastSyncedItem == null) { return(SyncroniceFeeds()); } else { feedContainerVM.feedItems = syncedFeeds.Select(x => new FeedItemEntity { Id = x.Id, description = x.description != null ? x.description : "", IsActive = x.IsActive, link = x.link != null ? x.link : "", author = x.author != null ? x.author : "", imageUrl = x.imageUrl != null ? x.imageUrl : "", pubDate = x.pubDate != null ? x.pubDate : "", title = x.title != null ? x.title : "" }).ToList(); return(feedContainerVM); } } else { var syncedFeeds = temporalFeedRepository.FindBy(x => x.userId == userId).ToList(); IEnumerable <FeedItemEntity> filteredFeeds; if (searchModel.selectedFeedId > 0) { filteredFeeds = syncedFeeds.Where(x => x.feedId == searchModel.selectedFeedId && (x.title.ToLower().Contains(searchModel.searchKey != null ? searchModel.searchKey.ToLower() : string.Empty) || x.description.ToLower().Contains(searchModel.searchKey != null ? searchModel.searchKey.ToLower() : string.Empty))).Select(x => new FeedItemEntity { Id = x.Id, description = x.description != null ? x.description : "", IsActive = x.IsActive, link = x.link != null ? x.link : "", author = x.author != null ? x.author : "", imageUrl = x.imageUrl != null ? x.imageUrl : "", pubDate = x.pubDate != null ? x.pubDate : "", title = x.title != null ? x.title : "" }).ToList(); } else { filteredFeeds = syncedFeeds.Where(x => x.title.ToLower().Contains(searchModel.searchKey != null ? searchModel.searchKey.ToLower() : string.Empty) || x.description.ToLower().Contains(searchModel.searchKey != null ? searchModel.searchKey.ToLower() : string.Empty)).Select(x => new FeedItemEntity { Id = x.Id, description = x.description != null ? x.description : "", IsActive = x.IsActive, link = x.link != null ? x.link : "", author = x.author != null ? x.author : "", imageUrl = x.imageUrl != null ? x.imageUrl : "", pubDate = x.pubDate != null ? x.pubDate : "", title = x.title != null ? x.title : "" }).ToList(); } feedContainerVM.feedItems = filteredFeeds; } return(feedContainerVM); }
public ActionResult FilterFeeds(FeedContainerInputModel model) { feed = new Feed(); return(View("~/Views/Home/FeedContainer.cshtml", feed.PopulateFeedContainer(model))); }