Esempio n. 1
0
        /// <summary>
        /// Adds a post
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        public int AddPost(Post post)
        {
            using (var db = new BlogDbContext())
            {
                var postEnt = new PostEntity();

                #region common

                //postEnt.PostId = post.PostId;
                postEnt.NavId = post.Nav.NavId;
                postEnt.PostGuidId = post.PostGuidId;
                postEnt.PostType = post.PostType;
                postEnt.PostStatus = post.PostStatus;
                postEnt.UserId = post.UserId;
                postEnt.Subject = post.Subject;
                postEnt.Body = post.Body;
                postEnt.IP = post.IP;
                postEnt.DateCreated = post.DateCreated;
                postEnt.DateUpdated = post.DateUpdated;
                postEnt.ViewCount = post.ViewCount;

                #endregion

                #region tags

                if (post.Tags != null)
                {
                    // Add new labels and association of label/post
                    foreach (var tag in post.Tags)
                    {
                        // find it
                        var tagEnt = db.TagEntities.FirstOrDefault(t => t.Name.ToLower() == tag.TagName.ToLower() && t.NavId == post.Nav.NavId);

                        // if tag is new, add the tag; if publish is true, set PostCount to 1 else to 0;
                        if (tagEnt == null)
                        {
                            tagEnt = new TagEntity();
                            tagEnt.Name = tag.TagName;
                            tagEnt.Description = tag.Description;
                            tagEnt.PostCount = post.PostStatus == PostStatus.Published ? 1 : 0;
                            tagEnt.NavId = post.Nav.NavId;
                            db.TagEntities.Add(tagEnt);
                        }
                        // if tag exists and publish is true, update PostCount to inc 1 else do nothing.
                        else if (post.PostStatus == PostStatus.Published)
                        {
                            tagEnt.PostCount++;
                        }

                        // add to tag/post association
                        postEnt.TagEntities.Add(tagEnt);
                    }
                }

                #endregion

                #region type specific

                if (post.PostType == PostType.Article)
                {
                    Article article = (Article)post;
                    postEnt.Slug = article.RequireSlugValidationAtDataSource ?
                        _ValidateSlug(db, post.Nav, article.PostSlug, post.DateCreated) : article.PostSlug;
                }

                #endregion

                #region archive

                if (post.PostStatus == PostStatus.Published) // touch archive only if the post is published
                {
                    // try find ent
                    var archiveEnt = db.ArchiveEntities.FirstOrDefault(a => a.NavId == post.Nav.NavId && a.Year == post.DateCreated.Year);

                    if (archiveEnt == null) // row not exist, add it
                    {
                        archiveEnt = new ArchiveEntity();
                        archiveEnt.NavId = post.Nav.NavId;
                        archiveEnt.Year = post.DateCreated.Year;
                        switch (post.DateCreated.Month)
                        {
                            case 1: archiveEnt.M01 = 1; break;
                            case 2: archiveEnt.M02 = 1; break;
                            case 3: archiveEnt.M03 = 1; break;
                            case 4: archiveEnt.M04 = 1; break;
                            case 5: archiveEnt.M05 = 1; break;
                            case 6: archiveEnt.M06 = 1; break;
                            case 7: archiveEnt.M07 = 1; break;
                            case 8: archiveEnt.M08 = 1; break;
                            case 9: archiveEnt.M09 = 1; break;
                            case 10: archiveEnt.M10 = 1; break;
                            case 11: archiveEnt.M11 = 1; break;
                            default: archiveEnt.M12 = 1; break;
                        }
                        db.ArchiveEntities.Add(archiveEnt);
                    }
                    else // otherwise update here
                    {
                        switch (post.DateCreated.Month)
                        {
                            case 1: archiveEnt.M01++; break;
                            case 2: archiveEnt.M02++; break;
                            case 3: archiveEnt.M03++; break;
                            case 4: archiveEnt.M04++; break;
                            case 5: archiveEnt.M05++; break;
                            case 6: archiveEnt.M06++; break;
                            case 7: archiveEnt.M07++; break;
                            case 8: archiveEnt.M08++; break;
                            case 9: archiveEnt.M09++; break;
                            case 10: archiveEnt.M10++; break;
                            case 11: archiveEnt.M11++; break;
                            default: archiveEnt.M12++; break;
                        }
                    }
                }

                #endregion

                db.PostEntities.Add(postEnt);
                db.SaveChanges();

                return postEnt.PostId;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="post"></param>
        public void UpdatePost(Post post)
        {
            using (var db = new BlogDbContext())
            {
                // find the post as well as loading tags with it
                var postEnt = db.PostEntities.Include(p => p.TagEntities).FirstOrDefault(p => p.PostId == post.PostId);

                bool prevIsPublished = postEnt.PostStatus == PostStatus.Published;
                bool wasPublished = postEnt.PostStatus == PostStatus.Published;
                DateTime oldDateCreated = postEnt.DateCreated;

                #region common

                postEnt.NavId = post.Nav.NavId;
                postEnt.PostGuidId = post.PostGuidId;
                postEnt.PostType = post.PostType;
                postEnt.UserId = post.UserId;
                postEnt.Subject = post.Subject;
                postEnt.Body = post.Body;
                postEnt.IP = post.IP;
                postEnt.DateCreated = post.DateCreated;
                postEnt.DateUpdated = post.DateUpdated;
                postEnt.ViewCount = post.ViewCount;
                postEnt.PostStatus = post.PostStatus;

                #endregion

                #region tags

                if (post.Tags != null)
                {
                    // find all the tags that belong to this post
                    var existingTags = from t in db.TagEntities
                                       from x in t.PostEntities
                                       where x.PostId == post.PostId
                                       select t;

                    // for each existing tag check if it's still in the coming tags
                    foreach (var existingTag in existingTags)
                    {
                        Tag tag = post.Tags.FirstOrDefault(t => t.TagName.ToLower() == existingTag.Name.ToLower());

                        // in coming tags does not have an existing tag anymore
                        if (tag == null)
                        {
                            if (existingTag.PostCount>0) existingTag.PostCount--; // an existing tag could have 0, because it was created with a save draft
                            postEnt.TagEntities.Remove(existingTag);
                        }
                        else // in coming tags still have an existing tag
                        {
                            if (prevIsPublished && post.PostStatus==PostStatus.Draft) // if it was pub, but now save as draft
                            {
                                existingTag.PostCount--;
                            }
                            else if (!prevIsPublished && post.PostStatus == PostStatus.Published) // if it was draft, but now publish it
                            {
                                existingTag.PostCount++;
                            }
                            post.Tags.Remove(tag); // remove an incoming tag that was also an existing tag
                        }
                    }

                    foreach (var tag in post.Tags)
                    {
                        // find it
                        var tagEnt = db.TagEntities.FirstOrDefault(t => t.Name.ToLower() == tag.TagName.ToLower() && t.NavId == post.Nav.NavId);

                        // if tag is new, add the tag; if publish is true, set PostCount to 1 else to 0;
                        if (tagEnt == null)
                        {
                            tagEnt = new TagEntity();
                            tagEnt.Name = tag.TagName;
                            tagEnt.Description = tag.Description;
                            tagEnt.PostCount = post.PostStatus == PostStatus.Published ? 1 : 0;
                            tagEnt.NavId = post.Nav.NavId;
                            db.TagEntities.Add(tagEnt);
                        }
                        // if tag exists and publish is true, update PostCount to inc 1 else do nothing.
                        else if (post.PostStatus == PostStatus.Published)
                        {
                            tagEnt.PostCount++;
                        }

                        // add to tag/post association
                        postEnt.TagEntities.Add(tagEnt);
                    }
                }

                #endregion

                #region type specific

                if (post.PostType == PostType.Article)
                {
                    Article article = (Article)post;
                    postEnt.Slug = article.RequireSlugValidationAtDataSource ?
                       _ValidateSlug(db, post.Nav, article.PostSlug, post.DateCreated) : article.PostSlug;
                }

                #endregion

                #region archive

                // if it was pub or it is now pub, then we have to update archive, because user can update DateCreated or user can pub a draft;
                if (wasPublished || post.PostStatus==PostStatus.Published)
                {
                    int oldYear = oldDateCreated.Year;
                    int oldMonth = oldDateCreated.Month;

                    if (!wasPublished && post.PostStatus == PostStatus.Published)
                    {
                        #region case1: it was draft but now pub -> inc only

                        var archEnt = db.ArchiveEntities.FirstOrDefault(a => a.NavId == post.Nav.NavId && a.Year == post.DateCreated.Year);

                        if (archEnt != null) // the row exists, inc
                        {
                            switch (post.DateCreated.Month)
                            {
                                case 1: archEnt.M01++; break;
                                case 2: archEnt.M02++; break;
                                case 3: archEnt.M03++; break;
                                case 4: archEnt.M04++; break;
                                case 5: archEnt.M05++; break;
                                case 6: archEnt.M06++; break;
                                case 7: archEnt.M07++; break;
                                case 8: archEnt.M08++; break;
                                case 9: archEnt.M09++; break;
                                case 10: archEnt.M10++; break;
                                case 11: archEnt.M11++; break;
                                default: archEnt.M12++; break;
                            }
                            //db.UpdateObject(archEnt);
                        }
                        else // the row didn't exist, add and set to 1
                        {
                            archEnt = new ArchiveEntity();
                            archEnt.NavId = post.Nav.NavId;
                            archEnt.Year = post.DateCreated.Year;
                            switch (post.DateCreated.Month)
                            {
                                case 1: archEnt.M01 = 1; break;
                                case 2: archEnt.M02 = 1; break;
                                case 3: archEnt.M03 = 1; break;
                                case 4: archEnt.M04 = 1; break;
                                case 5: archEnt.M05 = 1; break;
                                case 6: archEnt.M06 = 1; break;
                                case 7: archEnt.M07 = 1; break;
                                case 8: archEnt.M08 = 1; break;
                                case 9: archEnt.M09 = 1; break;
                                case 10: archEnt.M10 = 1; break;
                                case 11: archEnt.M11 = 1; break;
                                default: archEnt.M12 = 1; break;
                            }
                            db.ArchiveEntities.Add(archEnt);
                        }

                        #endregion
                    }
                    else if (wasPublished && post.PostStatus==PostStatus.Draft)
                    {
                        #region case2: it was pub but now draft - dec only

                        var archEnt = db.ArchiveEntities.FirstOrDefault(a => a.NavId == post.Nav.NavId && a.Year == post.DateCreated.Year);

                        if (archEnt != null) // the row should exist since it was published
                        {
                            switch (oldMonth)  // dec old month
                            {
                                case 1: archEnt.M01--; break;
                                case 2: archEnt.M02--; break;
                                case 3: archEnt.M03--; break;
                                case 4: archEnt.M04--; break;
                                case 5: archEnt.M05--; break;
                                case 6: archEnt.M06--; break;
                                case 7: archEnt.M07--; break;
                                case 8: archEnt.M08--; break;
                                case 9: archEnt.M09--; break;
                                case 10: archEnt.M10--; break;
                                case 11: archEnt.M11--; break;
                                default: archEnt.M12--; break;
                            }
                        }

                        #endregion
                    }
                    else if (oldYear != post.DateCreated.Year || oldMonth != post.DateCreated.Month)
                    {
                        #region case3: it was and still is published but not on the same year and month

                        var archEntOld = db.ArchiveEntities.FirstOrDefault(a => a.NavId == post.Nav.NavId && a.Year == oldYear);
                        var archEnt = db.ArchiveEntities.FirstOrDefault(a => a.NavId == post.Nav.NavId && a.Year == post.DateCreated.Year);

                        // dec old
                        if (archEntOld != null) // should exist
                        {
                            switch (oldMonth)  // dec old month
                            {
                                case 1: archEntOld.M01--; break;
                                case 2: archEntOld.M02--; break;
                                case 3: archEntOld.M03--; break;
                                case 4: archEntOld.M04--; break;
                                case 5: archEntOld.M05--; break;
                                case 6: archEntOld.M06--; break;
                                case 7: archEntOld.M07--; break;
                                case 8: archEntOld.M08--; break;
                                case 9: archEntOld.M09--; break;
                                case 10: archEntOld.M10--; break;
                                case 11: archEntOld.M11--; break;
                                default: archEntOld.M12--; break;
                            }
                        }

                        // inc new
                        if (archEnt != null)
                        {
                            switch (post.DateCreated.Month)
                            {
                                case 1: archEnt.M01++; break;
                                case 2: archEnt.M02++; break;
                                case 3: archEnt.M03++; break;
                                case 4: archEnt.M04++; break;
                                case 5: archEnt.M05++; break;
                                case 6: archEnt.M06++; break;
                                case 7: archEnt.M07++; break;
                                case 8: archEnt.M08++; break;
                                case 9: archEnt.M09++; break;
                                case 10: archEnt.M10++; break;
                                case 11: archEnt.M11++; break;
                                default: archEnt.M12++; break;
                            }
                        }
                        else
                        {
                            archEnt = new ArchiveEntity();
                            archEnt.NavId = post.Nav.NavId;
                            archEnt.Year = post.DateCreated.Year;
                            switch (post.DateCreated.Month)
                            {
                                case 1: archEnt.M01 = 1; break;
                                case 2: archEnt.M02 = 1; break;
                                case 3: archEnt.M03 = 1; break;
                                case 4: archEnt.M04 = 1; break;
                                case 5: archEnt.M05 = 1; break;
                                case 6: archEnt.M06 = 1; break;
                                case 7: archEnt.M07 = 1; break;
                                case 8: archEnt.M08 = 1; break;
                                case 9: archEnt.M09 = 1; break;
                                case 10: archEnt.M10 = 1; break;
                                case 11: archEnt.M11 = 1; break;
                                default: archEnt.M12 = 1; break;
                            }
                            db.ArchiveEntities.Add(archEnt);
                        }

                        #endregion
                    }
                }

                #endregion

                db.SaveChanges();
            }
        }
Esempio n. 3
0
        // --------------------------------------------------------------------
        public int AddPost(Post post)
        {
            postList.Add(post);

            post.PostId = postList.Count + 1; // need to set id or GetPost will fail

            foreach (var tag in post.Tags)
            {
                tagList.Add(tag);
            }
            return post.PostId;
        }
Esempio n. 4
0
        public void UpdatePost(Post post)
        {
            if (post.PostType == PostType.Article)
            {
                Article article = (Article)post;
                var fan_post = (Article)postList.Find(p => p.PostId == post.PostId);

                fan_post.Subject = post.Subject;
                fan_post.Body = post.Body;
                fan_post.DateCreated = post.DateCreated;
                fan_post.IP = post.IP;
                fan_post.PostSlug = post.PostSlug;

                foreach (var tag in post.Tags)
                {
                    tagList.Add(tag);
                }
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Adds a post, returns its id.
        /// </summary>
        /// <param name="post"></param>
        /// <returns></returns>
        private static int _AddPost(Post post)
        {
            int postId = Provider.AddPost(post);

            // ---------------------------------------------------------------- Cache

            // Remove from cache
            SiteCache.Remove(BlogHelper.GetCacheKey_Posts(post.Nav.Slug, QueryBy.Index)); // index page
            SiteCache.Remove(BlogHelper.GetCacheKey_Tags(post.Nav.Slug)); // tag list
            SiteCache.Remove(BlogHelper.GetCacheKey_Timeline(post.Nav.Slug));  // archive list

            return postId;
        }
Esempio n. 6
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="post"></param>
        /// <param name="previousPostSlug"></param>
        /// <remarks>
        /// </remarks>
        public static void UpdatePost(Post post, string previousPostSlug)
        {
            Provider.UpdatePost(post);

            // ---------------------------------------------------------------- Cache

            // Remove from cache
            SiteCache.Remove(BlogHelper.GetCacheKey_Post(post.Nav.Slug, post.PostId));  // post
            SiteCache.Remove(BlogHelper.GetCacheKey_Posts(post.Nav.Slug, QueryBy.Index)); // index page
            SiteCache.Remove(BlogHelper.GetCacheKey_Tags(post.Nav.Slug)); // tag list
            SiteCache.Remove(BlogHelper.GetCacheKey_Timeline(post.Nav.Slug));  // archive list
        }
Esempio n. 7
0
        /// <summary>
        /// This is called when a visitor visits a blog post, it increases the view count for post view, daily veiw and total view. 
        /// </summary>
        /// <remarks>
        /// - There is a complex caching mechanism enforced here to update the count, it uses sliding right now
        /// - This method decides if to inc the count, owner will NOT inc any count.  
        /// </remarks>
        /// <seealso cref="http://msdn.microsoft.com/en-us/library/system.web.caching.cacheitemremovedcallback.aspx"/>
        public static void IncViewCount(Post post)
        {
            if (!SiteContext.Current.IsAnonymous || post == null) return; // inc only for visitors

            string ip = SiteContext.Current.IP;
            // to debug this logic, set the TimeSpan to only a few seconds, so you don't have to wait long for it to hit break point
            TimeSpan slidingExpiration = post.DateCreated.IsWithinXDays(14) ? new TimeSpan(0, 1, 0) : new TimeSpan(0, 0, 30); // need to update these value after testing
            //TimeSpan slidingExpiration = new TimeSpan(0, 0, 5); // need to update these value after testing

            #region inc post view

            string postViewKey = BlogHelper.GetCacheKey_PostViewCount(post.Nav.Slug, post.PostId, post.DateCreated);
            List<string> ipList_PostView = SiteCache.Get(postViewKey) as List<string>;
            if (ipList_PostView == null) // list not in cache, create it, add the ip and put it to cache
            {
                ipList_PostView = new List<string>();
                ipList_PostView.Add(ip);

                SiteCache.Slide(postViewKey, ipList_PostView, slidingExpiration, _UpdatePostViewCountCallback);
            }
            else // list in cache, update it
            {
                // if not in list then add it
                if (!ipList_PostView.Contains(ip))
                {
                    ipList_PostView.Add(ip);
                }

                // when you use Remove it'll trigger the callback
                if (ipList_PostView.Count >= 100)
                {
                    HttpRuntime.Cache.Remove(postViewKey);
                }
            }

            #endregion
        }