/// <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(); } }
/// <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; } }