public IQueryable <SeqPost> RecentNews(int num) { SeqCategory cat = db.SeqCategories.SingleOrDefault(c => c.Name == NEWS); if (cat == null) { return(null); } return(cat.SeqPosts.AsQueryable()); }
/// <summary> /// Update the categories table with every new or modified post. /// </summary> /// <param name="catName"> /// The category name submitted with the post. /// </param> /// <param name="sp">The post to be persisted.</param> private void UpdateCategories(string catName, SeqPost sp) { /* A post might not send any category, an existing category, or a new category. In the case of null or empty category we file the post in a category called "None". */ SeqCategory oldCategory = sp.SeqCategory; SeqCategory theCategory = GetUncategorizedCategory(); if (catName.Length > 0) { theCategory = db.SeqCategories.SingleOrDefault( d => d.Name == catName && d.BlogId==sp.BlogId); /* New category was submitted with post */ if (theCategory == null) { theCategory = new SeqCategory(); theCategory.BlogId = sp.BlogId; theCategory.Name = catName; theCategory.LastUpdated = DateTime.Now.ToUniversalTime(); theCategory.Tally = 0; sp.SeqCategory = theCategory; } /* Found old category */ else { theCategory.LastUpdated = DateTime.Now.ToUniversalTime(); sp.SeqCategory = theCategory; } } /* No post category submitted. File under "None" */ else { /* "None" is not in the database. */ if (theCategory == null) { theCategory = new SeqCategory(); theCategory.BlogId = sp.BlogId; theCategory.Name = UNCATEGORIZED; theCategory.LastUpdated = DateTime.Now.ToUniversalTime(); theCategory.Tally = 0; sp.SeqCategory = theCategory; } /* "None" category exists */ else { theCategory.LastUpdated = DateTime.Now.ToUniversalTime(); sp.SeqCategory = theCategory; } } /* Switching categories - decrement the old category's tally */ if (oldCategory!=null && (oldCategory.Name != theCategory.Name)) { oldCategory.Tally--; oldCategory.SeqPosts.Remove(sp); } theCategory.Tally = theCategory.SeqPosts.Count(); }