public IQueryable <SeqPost> PostsForTag(SeqTag tag, string sortOrder, int pageSize, int pageNum) { pageSize = (pageSize > MAX_PAGE_SIZE) ? MAX_PAGE_SIZE : pageSize; IQueryable <SeqPost> posts; switch (sortOrder) { case "desc": posts = tag.SeqPosts .OrderByDescending(p => p.CreateDate) .Skip((pageNum - 1) * pageSize) .Take(pageSize) .AsQueryable <SeqPost>(); break; default: posts = tag.SeqPosts .OrderBy(p => p.CreateDate) .Skip((pageNum - 1) * pageSize) .Take(pageSize) .AsQueryable <SeqPost>(); break; } return(posts); }
private TagHomeVModel GetPostsForTag(int tagId, string bid, int pageNum, string order) { if (bid == null) { bid = blogId; } SeqTag tag = tagRep.GetTag(tagId, bid); //if (tag == null) return null; IQueryable <SeqPost> tagPosts = tagRep.PostsForTag( tag, order, pageSize, pageNum); TagHomeVModel thvm = new TagHomeVModel(tagId); thvm.Tag = tag.Name; thvm.BlogId = bid; thvm.RecentPosts = VModelFactory.BlogPosts(tagPosts); thvm.Controller = "Tag"; thvm.CurrentPage = pageNum; thvm.PageSize = pageSize; thvm.HasMorePages = thvm.RecentPosts.Count() > 0; //thvm.News = VModelFactory.BlogPosts(catRep.RecentNews(5)); //thvm.Books = VModelFactory.Books(bookRep.AllBooks()); return(thvm); }
/// <summary> /// Get a tag given a name. /// </summary> /// <param name="tagName">The name of the tag to find.</param> /// <returns>The SeqTag that matches the tagName. In the unlikely event that /// there is more than one match this method returns null.</returns> public SeqTag GetTag(string tagName, string blogId) { try { SeqTag theTag = db.SeqTags.Single(t => t.Name == tagName && t.BlogId == blogId); return(theTag); } catch (InvalidOperationException) { return(null); } }
///// <summary> ///// Updates SeqBooks table with new or removed posts. A book is a ///// collection of posts without regard to chronology of the posts. A book ///// can be navigated with different views than that used for regular ///// posts. Currently, the book title much exactly match a category for ///// new posts to be addable to the book through WLW2011. Note that the ///// book is retrieved through the SeqBook.UriContext in order to provide ///// shorter URLs and avoid special characters which get encoded ugly. ///// </summary> ///// <param name="sp"></param> ///// <param name="oldCategory"></param> ///// <param name="newCategory"></param> //private void UpdateBooks(SeqPost sp, SeqCategory oldCategory=null, // SeqCategory newCategory=null) { // /* Change of categories so remove post from old category/book */ // if ((oldCategory!=null && newCategory!=null) && // (oldCategory.Name!= newCategory.Name)) { // SeqBook oldBook = booksTable.SingleOrDefault( // b => b.Title ==oldCategory.Name); // if (oldBook != null) { // oldBook.SeqPosts.Remove(sp); // oldBook.PageCount = oldBook.SeqPosts.Count(); // booksTable.Context.Refresh(RefreshMode.KeepCurrentValues, // oldBook); // } // } // /* Add post to existing book if post is in a category designated book. */ // bool catIsBook = booksTable.Any(b => b.Title == sp.SeqCategory.Name); // if (catIsBook) { // SeqBook book = booksTable.Single(b => b.Title == sp.SeqCategory.Name); // book.SeqPosts.Add(sp); // book.PageCount=book.SeqPosts.Count(); // booksTable.Context.Refresh(RefreshMode.KeepCurrentValues, book); // } //} private void UpdateTags(string tags, SeqPost sp) { /* WLW2011 will send an empty string if no tags. */ if (tags == null || tags=="") return; string[] allTags = tags.Split(','); /* Simple cleanse */ for (int i = 0; i < allTags.Length; i++) { allTags[i] = allTags[i].Trim() .TrimEnd(FORBIDDEN_CHARS) .TrimStart(FORBIDDEN_CHARS); } List<SeqTag> postTags = sp.SeqTags.ToList<SeqTag>(); /* Tallys are recomputed for any tag removed and if the tally is zero then the tag itself is removed. */ foreach (SeqTag st in postTags) { /* If none of the submitted tags match the current tag then we know it has been removed since WLW2011 sends the complete list of tags for every transaction. */ if (!allTags.Any(at => at==st.Name)) { sp.SeqTags.Remove(st); //Delete tag ref from post st.SeqPosts.Remove(sp); //Delete post ref from tag st.Tally = st.SeqPosts.Count(); if (st.Tally == 0) db.SeqTags.DeleteObject(st); //Sweep db of empties } } for (int i=0; i<allTags.Length; i++) { SeqTag dbTag; /* Slow...has to make allTags.Length queries to the db. */ string name = allTags[i]; //Array elements not supported by EF4?! if (!db.SeqTags.Any(st => st.Name == name)) { dbTag = new SeqTag(); dbTag.BlogId = sp.BlogId; dbTag.Name = allTags[i]; dbTag.LastUpdated = DateTime.Now.ToUniversalTime(); sp.SeqTags.Add(dbTag); dbTag.SeqPosts.Add(sp); } /* Re-use existing tag from SeqTags table */ else { dbTag = db.SeqTags.Single( t => t.Name == name && t.BlogId==sp.BlogId); dbTag.LastUpdated = DateTime.Now.ToUniversalTime(); dbTag.SeqPosts.Add(sp); sp.SeqTags.Add(dbTag); } //end else dbTag.Tally = dbTag.SeqPosts.Count(); } //end for }