Exemple #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;
            }
        }
Exemple #2
0
        /// <summary>
        /// When adding/updating a post, in case slug already exists, return the slug attached with "-int".
        /// </summary>
        private string _ValidateSlug(BlogDbContext db, Nav nav, string postSlug, DateTime postDate)
        {
            int i = 0;
            string t = postSlug;

            while (db.PostEntities.FirstOrDefault(p => p.NavId == nav.NavId
                   && p.Slug.ToLower() == t.ToLower()
                   && p.DateCreated.Year == postDate.Year
                   && p.DateCreated.Month == postDate.Month
                   && p.DateCreated.Day == postDate.Day) != null)
            {
                i++;
                t = string.Format("{0}-{1}", postSlug, i.ToString());
            }

            return t;
        }
Exemple #3
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();
            }
        }
Exemple #4
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();
            }
        }
Exemple #5
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();
            }
        }
Exemple #6
0
        public List<Tag> GetTags(Nav nav)
        {
            using (var db = new BlogDbContext())
            {
                var tags = new List<Tag>();

                foreach (var t in db.TagEntities.Where(t=>t.NavId == nav.NavId))
                {
                    var tag = new Tag(nav)
                    {
                        TagId = t.TagId,
                        TagName = WebHelper.UrlDecode(t.Name),
                        Description = t.Description,
                        PostCount = t.PostCount,
                    };
                    tags.Add(tag);
                }

                return tags;
            }
        }
Exemple #7
0
 public Tag GetTag(Nav nav, int tagId)
 {
     using (var db = new BlogDbContext())
     {
         TagEntity ent = db.TagEntities.FirstOrDefault(t => t.TagId == tagId);
         if (ent == null) throw new SiteException("Tag not found");
         return new Tag(nav)
         {
             TagId = ent.TagId,
             TagName = WebHelper.UrlDecode(ent.Name),
             Description = ent.Description,
             PostCount = ent.PostCount,
         };
     }
 }
Exemple #8
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;
            }
        }
Exemple #9
0
        /// <summary>
        /// Returns a list of posts for a particular nav that also satisfy other query
        /// critirions.
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PostList GetPosts(PostQuery query)
        {
            List<Post> posts = new List<Post>();
            List<PostEntity> postEnts = null;
            int totalPosts = 0;
            //int skip = query.PageIndex * query.PageSize;
            int skip = (query.PageIndex - 1) * query.PageSize;
            int take = query.PageSize;

              using (var db = new BlogDbContext())
            {
                if (query.QueryBy == QueryBy.Index) // index
                {
                    totalPosts = db.PostEntities.Count(p => p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Published);
                    postEnts = (from p in db.PostEntities.Include(p => p.TagEntities)
                                where p.NavId == query.Nav.NavId &&
                                      p.PostStatus == PostStatus.Published
                                orderby p.DateCreated descending
                                select p)
                                     .Skip(skip).Take(take).ToList();
                }
                else if (query.QueryBy == QueryBy.Drafts) // drafts
                {
                    totalPosts = db.PostEntities.Count(p => p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Draft);
                    postEnts = (from p in db.PostEntities.Include(p => p.TagEntities)
                                where p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Draft
                                orderby p.DateCreated descending
                                select p)
                                     .Skip(skip).Take(take).ToList();
                }
                else if (query.QueryBy == QueryBy.Article)
                {
                    totalPosts = db.PostEntities.Count(p => p.PostType == (short)PostType.Article && p.NavId == query.Nav.NavId && p.PostStatus == PostStatus.Published);
                    postEnts = (from p in db.PostEntities.Include(p => p.TagEntities)
                                where p.PostType == PostType.Article &&
                                      p.PostStatus == PostStatus.Published &&
                                      p.NavId == query.Nav.NavId
                                orderby p.DateCreated descending
                                select p)
                                     .Skip(skip).Take(take).ToList();
                }
                else if (query.QueryBy == QueryBy.Tag)
                {
                    TagEntity tagEnt = db.TagEntities.FirstOrDefault(t => t.Name.ToLower() == query.TagName.ToLower() && t.NavId == query.Nav.NavId);
                    if (tagEnt != null)
                    {
                        // get the total published posts count from the tag
                        totalPosts = (from p in db.PostEntities
                                      from t in p.TagEntities
                                      where p.NavId==query.Nav.NavId && t.TagId == tagEnt.TagId &&
                                            p.PostStatus == PostStatus.Published
                                      select p).Count();

                        //http://stackoverflow.com/questions/15556134/entity-framework-many-to-many-works-but-include-does-not/15577053?noredirect=1#15577053
                        postEnts = (from p in db.PostEntities.Include(p => p.TagEntities)
                                    where p.TagEntities.Any(t=>t.TagId ==tagEnt.TagId) &&
                                          p.NavId == query.Nav.NavId &&
                                          p.PostStatus == PostStatus.Published
                                    orderby p.DateCreated descending
                                    select p).Skip(skip).Take(take).ToList();
                    }
                }
                else if (query.QueryBy == QueryBy.Archive)
                {
                    if (query.Month.HasValue && query.Month >0)
                    {
                        // get the total posts for the year and month
                        totalPosts = (from p in db.PostEntities
                                      where p.NavId == query.Nav.NavId &&
                                            p.DateCreated.Year == query.Year && p.DateCreated.Month == query.Month.Value &&
                                            p.PostStatus == PostStatus.Published
                                      select p).Count();

                        postEnts = (from p in db.PostEntities.Include(p => p.TagEntities)
                                    where p.NavId == query.Nav.NavId &&
                                          p.DateCreated.Year == query.Year && p.DateCreated.Month == query.Month.Value &&
                                          p.PostStatus == PostStatus.Published
                                    orderby p.DateCreated descending
                                    select p)
                                .Skip(skip).Take(take).ToList();
                    }
                    else
                    {
                        // get the total posts for the year only
                        totalPosts = (from p in db.PostEntities
                                      where p.NavId == query.Nav.NavId &&
                                            p.DateCreated.Year == query.Year &&
                                            p.PostStatus == PostStatus.Published
                                      select p).Count();

                        postEnts = (from p in db.PostEntities.Include(p => p.TagEntities)
                                    where p.NavId == query.Nav.NavId &&
                                          p.DateCreated.Year == query.Year &&
                                          p.PostStatus == PostStatus.Published
                                    orderby p.DateCreated descending
                                    select p)
                                .Skip(skip).Take(take).ToList();
                    }
                }

                foreach (var postEnt in postEnts)
                {
                    User user = db.Users.FirstOrDefault(u => u.UserId == postEnt.UserId);
                    posts.Add(BlogMapper.MapPost(postEnt, query.Nav, user));
                }

                PostList postList = new PostList(posts, totalPosts);
                return postList;
            }
        }
Exemple #10
0
        /// <summary>
        /// Returns <see cref="Post"/> for edit.
        /// </summary>
        /// <param name="nav"></param>
        /// <param name="postID"></param>
        /// <param name="currentUserId"></param>
        /// <returns></returns>
        /// <exception cref="Fan.SiteException">If user is not the author of the post.</exception>
        public Post GetPostForEdit(Nav nav, int postID, int currentUserId)
        {
            using (var db = new BlogDbContext())
            {
                var postEnt = db.PostEntities.Include(p => p.TagEntities).FirstOrDefault(p => p.PostId == postID && p.UserId == currentUserId);

                if (postEnt == null) throw new SiteException("Post_Not_Found");

                User user = db.Users.FirstOrDefault(u => u.UserId == postEnt.UserId);
                return BlogMapper.MapPost(postEnt, nav, user);
            }
        }
Exemple #11
0
        // -------------------------------------------------------------------- Post
        /// <summary>
        /// Returns
        /// "Drafts": 10
        /// 
        /// </summary>
        /// <param name="nav"></param>
        /// <returns></returns>
        public Dictionary<PostType, int> GetPostCountsByType(Nav nav)
        {
            var stats = new Dictionary<PostType, int>();
            using (var db = new BlogDbContext())
            {
                var result = db.PostEntities.Where(p=>p.NavId == nav.NavId).GroupBy(c => c.PostType).Where(grp => grp.Count() > 1);
                    //from b in db.PostEntities
                    //          group b by b.PostType into grp
                    //          //where grp.Count() > 1
                    //          select grp.Key;

                foreach (var item in result)
                {
                    var test = item.Key;
                    var test2 = item.Count();
                }
            }

            return stats;
        }
Exemple #12
0
        /// <summary>
        /// NOT USED, 
        /// </summary>
        /// <param name="nav"></param>
        /// <param name="postSlug"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="day"></param>
        /// <param name="postType"></param>
        /// <returns></returns>
        public Post GetPost(Nav nav, string postSlug, int year, int month, int day, PostType postType)
        {
            using (var db = new BlogDbContext())
            {
                var postEnt = db.PostEntities.Include(p => p.TagEntities)
                    .FirstOrDefault(p => p.NavId == nav.NavId &&
                        p.PostStatus == PostStatus.Published &&
                        p.Slug.ToLower() == postSlug.ToLower() &&
                        p.PostType == postType &&
                        p.DateCreated.Year == year &&
                        p.DateCreated.Month == month &&
                        p.DateCreated.Day == day);
                if (postEnt == null) throw new SiteException("Post_Not_Found");

                User user = db.Users.FirstOrDefault(u => u.UserId == postEnt.UserId);
                return BlogMapper.MapPost(postEnt, nav, user);
            }
        }
Exemple #13
0
        /// <summary>
        /// Returns a list of <see cref="Archive"/> objects.
        /// </summary>
        /// <param name="nav"></param>
        /// <returns></returns>
        /// <remarks>
        /// The list will be like the following: <br />
        /// <list type="bullet">
        /// <item>
        /// <description>2013 12 100</description>
        /// </item>
        /// <item>
        /// <description>2013 11 100</description>
        /// </item>
        /// <item>
        /// <description>...</description>
        /// </item>
        /// </list>
        /// </remarks>
        public List<Archive> GetArchives(Nav nav)
        {
            using (var db = new BlogDbContext())
            {
                var archiveList = new List<Archive>();

                List<ArchiveEntity> archEnts = db.ArchiveEntities.Where(a => a.NavId == nav.NavId).OrderBy(a=>a.Year).ToList();
                if (archEnts == null)
                {
                    return archiveList;
                }

                return BlogMapper.MapArchive(archEnts, nav);
            }
        }
Exemple #14
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();
            }
        }
Exemple #15
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;
            }
        }
Exemple #16
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;
            }
        }