Пример #1
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="galleryID"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public int AddImage(BlogImage blogImage)
        {
            using (var db = new BlogDbContext())
            {
                var ent = new BlogImageEntity();
                ent.NavId = blogImage.Nav.NavId;
                ent.UserId = blogImage.UserId;
                ent.PostGuidId = blogImage.PostGuidId;
                ent.Caption = blogImage.Caption;
                ent.Description = blogImage.Description;

                db.BlogImageEntities.Add(ent);
                db.SaveChanges();

                return ent.ImageId;
            }
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tag"></param>
        public void UpdateTag(Tag tag)
        {
            using (var db = new BlogDbContext())
            {
                // check if a tag with this name exists in this nav
                var ent = db.TagEntities.FirstOrDefault(n => n.Name.ToLower() == tag.TagName.ToLower() && n.NavId == tag.Nav.NavId);
                if (ent != null)
                    throw new SiteException("Tag exists.");

                // find the current tag
                ent = db.TagEntities.FirstOrDefault(n => n.TagId == tag.TagId);
                if (ent == null)
                    return;

                // update it
                ent.Name = tag.TagName;
                ent.Description = tag.Description;

                // save it
                db.SaveChanges();
            }
        }
Пример #3
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();
            }
        }
Пример #4
0
        public void IncreasePostView(int postId, int count)
        {
            using (var db = new BlogDbContext())
            {
                var post = db.PostEntities.FirstOrDefault(p => p.PostId == postId);
                if (post == null) return;

                post.ViewCount += count;
                db.SaveChanges();
            }
        }
Пример #5
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;
            }
        }
Пример #6
0
        public void DeleteTag(int tagID)
        {
            using (var db = new BlogDbContext())
            {
                // find it
                var ent = db.TagEntities.FirstOrDefault(t => t.TagId == tagID);

                if (ent == null) return;// throw new Exception();

                db.TagEntities.Remove(ent); // delete cascade will take care of the tags
                db.SaveChanges();
            }
        }
Пример #7
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="postID"></param>
        /// <returns></returns>
        public string DeletePost(int postID)
        {
            using (var db = new BlogDbContext())
            {
                // Find the post
                var ent = db.PostEntities.FirstOrDefault(p => p.PostId == postID);

                if (ent == null) throw new SiteException();

                #region tags

                // update tags only for published post
                if (ent.PostStatus == PostStatus.Published)
                {
                    // find all the tags that belong to this post
                    var existingTags = from t in db.TagEntities
                                       from x in t.PostEntities
                                       where x.PostId == postID
                                       select t;

                    foreach (var tag in existingTags)
                    {
                        // when you delete a post that was a draft since creation,
                        // its tags will have 0, then you don't want to dec on 0.
                        if (tag.PostCount > 0)
                            tag.PostCount--;
                    }
                }

                #endregion

                #region archive

                // update archive only if the post is published
                if (ent.PostStatus == PostStatus.Published)
                {
                    var archEnt = db.ArchiveEntities.FirstOrDefault(a => a.NavId == ent.NavId && a.Year == ent.DateCreated.Year);
                    if (archEnt != null)
                    {
                        switch (ent.DateCreated.Month)  // dec 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

                db.PostEntities.Remove(ent); // delete cascade will take care of the tags
                db.SaveChanges();

                return ent.Slug;
            }
        }
Пример #8
0
        /// <summary>
        /// Adds a tag.
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        /// <exception cref="BlogException">Throw if already exists</exception>
        public int AddTag(Tag tag)
        {
            using (var db = new BlogDbContext())
            {
                if (db.TagEntities.FirstOrDefault(n => n.Name.ToLower() == tag.TagName.ToLower()) != null)
                    throw new SiteException("Msg_Tag_Exists");

                var ent = new TagEntity();
                ent.Name = tag.TagName;
                ent.Description = tag.Description;
                ent.NavId = tag.Nav.NavId;

                db.TagEntities.Add(ent);
                db.SaveChanges();

                return ent.TagId;
            }
        }