public void RemoveLabel(String labelGuid, String blogEntryGuid) { using (DataContext db = new DataContext(ServiceConfiguration.ConnectionString)) { //+ validate BlogEntryLINQ blogEntryLinq; Validator.EnsureBlogEntryExists(blogEntryGuid, out blogEntryLinq, db); LabelLINQ labelLinq; Validator.EnsureLabelExists(labelGuid, out labelLinq, db); //+ LabelBlogEntryLINQ labelBlogEntryLinq = db.LabelBlogEntries.SingleOrDefault(p => p.LabelId == labelLinq.LabelId && p.BlogEntryId == blogEntryLinq.BlogEntryId); if (labelBlogEntryLinq != null) { db.LabelBlogEntries.DeleteOnSubmit(labelBlogEntryLinq); db.SubmitChanges(); } } }
public void ApplyLabel(String blogEntryGuid, String labelGuid) { using (DataContext db = new DataContext(ServiceConfiguration.ConnectionString)) { //+ ensure blog exists BlogEntryLINQ blogEntryLinq; Validator.EnsureBlogEntryExists(blogEntryGuid, out blogEntryLinq, db); LabelLINQ labelLinq; Validator.EnsureLabelExists(labelGuid, out labelLinq, db); //+ if (!db.LabelBlogEntries.Any(p => p.LabelId == labelLinq.LabelId && p.BlogEntryId == blogEntryLinq.BlogEntryId)) { LabelBlogEntryLINQ labelBlogEntryLinq = new LabelBlogEntryLINQ(); labelBlogEntryLinq.BlogEntryId = blogEntryLinq.BlogEntryId; labelBlogEntryLinq.LabelId = labelLinq.LabelId; //+ db.LabelBlogEntries.InsertOnSubmit(labelBlogEntryLinq); db.SubmitChanges(); } } }
public String PostBlogEntry(String blogGuid, List <Author> authorList, String title, String content, DateTime dateTime, String blogEntryTypeGuid, List <Label> labelList, Boolean publish) { using (DataContext db = new DataContext(ServiceConfiguration.ConnectionString)) { //+ validate BlogLINQ blogLinq; Validator.EnsureBlogExists(blogGuid, out blogLinq, db); BlogEntryTypeLINQ blogEntryTypeLinq = null; if (!String.IsNullOrEmpty(blogEntryTypeGuid)) { Validator.EnsureBlogEntryTypeExists(blogEntryTypeGuid, out blogEntryTypeLinq, db); } if (authorList == null) { authorList = new List <Author>(); } List <AuthorLINQ> authorLinqList; Validator.EnsureEachAuthorExists(authorList, out authorLinqList, db); if (labelList == null) { labelList = new List <Label>(); } List <LabelLINQ> labelLinqList; Validator.EnsureEachLabelExists(labelList, out labelLinqList, db); Validator.IsNotZero(authorLinqList.Count, "At least one author is required."); //+ using (TransactionScope scope = new TransactionScope()) { BlogEntryLINQ blogEntryLinq = new BlogEntryLINQ(); blogEntryLinq.BlogId = blogLinq.BlogId; blogEntryLinq.BlogEntryTitle = title; blogEntryLinq.BlogEntryTypeId = 1; blogEntryLinq.BlogEntryText = content; if (!String.IsNullOrEmpty(blogEntryTypeGuid)) { blogEntryLinq.BlogEntryTypeId = blogEntryTypeLinq.BlogEntryTypeId; } blogEntryLinq.BlogEntryStatus = db.BlogEntryStatus.SingleOrDefault(p => p.BlogEntryStatusId == (publish ? 1 : 3)); blogEntryLinq.BlogEntryCommentAllowStatus = db.BlogEntryCommentAllowStatus.SingleOrDefault(p => p.BlogEntryCommentAllowStatusId == 1); blogEntryLinq.BlogEntryGuid = Themelia.GuidCreator.GetNewGuid(); if (dateTime.Year >= 1950) { blogEntryLinq.BlogEntryPostDateTime = dateTime; } else { blogEntryLinq.BlogEntryPostDateTime = DateTime.Now; } blogEntryLinq.BlogEntryModifyDateTime = DateTime.Now; //+ db.BlogEntries.InsertOnSubmit(blogEntryLinq); db.SubmitChanges(); //+ BlogEntryUrlMappingLINQ blogEntryUrlMappingLinq = new BlogEntryUrlMappingLINQ(); blogEntryUrlMappingLinq.BlogEntryId = blogEntryLinq.BlogEntryId; blogEntryUrlMappingLinq.BlogEntryUrlMappingName = BlogEntryHelper.BuildBlogEntryLink(blogEntryLinq.BlogEntryPostDateTime, CreateBlogEntryPostUrlMapping(title)); blogEntryUrlMappingLinq.BlogEntryUrlMappingPrimary = true; db.BlogEntryUrlMappings.InsertOnSubmit(blogEntryUrlMappingLinq); //+ db.SubmitChanges(); //+ label foreach (LabelLINQ labelLinq in labelLinqList) { LabelBlogEntryLINQ labelBlogEntryLinq = new LabelBlogEntryLINQ(); labelBlogEntryLinq.BlogEntryId = blogEntryLinq.BlogEntryId; labelBlogEntryLinq.LabelId = labelLinq.LabelId; //+ db.LabelBlogEntries.InsertOnSubmit(labelBlogEntryLinq); } db.SubmitChanges(); //+ author foreach (AuthorLINQ authorLinq in authorLinqList) { BlogEntryAuthorLINQ blogEntryAuthorLinq = new BlogEntryAuthorLINQ(); blogEntryAuthorLinq.BlogEntryId = blogEntryLinq.BlogEntryId; blogEntryAuthorLinq.AuthorId = authorLinq.AuthorId; //+ db.BlogEntryAuthors.InsertOnSubmit(blogEntryAuthorLinq); } db.SubmitChanges(); //+ scope.Complete(); //+ return(blogEntryLinq.BlogEntryGuid); } } }
public void UpdateBlogEntry(String blogEntryGuid, String title, String content, String blogEntryTypeGuid, List <Label> labelList, DateTime dateTime, Boolean publish) { using (DataContext db = new DataContext(ServiceConfiguration.ConnectionString)) { //+ ensure blog entry exists BlogEntryLINQ blogEntryLinq; Validator.EnsureBlogEntryExists(blogEntryGuid, out blogEntryLinq, db); if (!String.IsNullOrEmpty(blogEntryTypeGuid)) { BlogEntryTypeLINQ blogEntryTypeLinq; Validator.EnsureBlogEntryTypeExists(blogEntryTypeGuid, out blogEntryTypeLinq, db); } if (labelList == null) { labelList = new List <Label>(); } List <LabelLINQ> labelLinqList; Validator.EnsureEachLabelExists(labelList, out labelLinqList, db); using (TransactionScope scope = new TransactionScope()) { if (dateTime.Year >= 1950) { blogEntryLinq.BlogEntryPostDateTime = dateTime; } //+ has this title's mapping been used by a different blog entry? if (!String.IsNullOrEmpty(title)) { String mapping = BlogEntryHelper.BuildBlogEntryLink(blogEntryLinq.BlogEntryPostDateTime, CreateBlogEntryPostUrlMapping(title)); BlogEntryUrlMappingLINQ blogEntryUrlMappingLINQ = db.BlogEntryUrlMappings.Where(p => p.BlogEntryUrlMappingName == mapping && p.BlogEntryId != blogEntryLinq.BlogEntryId).FirstOrDefault(); if (blogEntryUrlMappingLINQ != null) { throw new ArgumentException("This title's mapping has already been used and, therefore, would create a conflicting link. Please change the title"); } blogEntryUrlMappingLINQ = db.BlogEntryUrlMappings.Where(p => p.BlogEntryUrlMappingName == mapping && p.BlogEntryId == blogEntryLinq.BlogEntryId).FirstOrDefault(); //+ if this is a new title completely, create a new mapping to allow access by //+ the old and the new links if (blogEntryUrlMappingLINQ == null) { foreach (BlogEntryUrlMappingLINQ beum in db.BlogEntryUrlMappings.Where(p => p.BlogEntryId == blogEntryLinq.BlogEntryId)) { beum.BlogEntryUrlMappingPrimary = false; db.SubmitChanges(); } BlogEntryUrlMappingLINQ blogEntryUrlMappingLinq = new BlogEntryUrlMappingLINQ(); blogEntryUrlMappingLinq.BlogEntryId = blogEntryLinq.BlogEntryId; blogEntryUrlMappingLinq.BlogEntryUrlMappingName = CreateBlogEntryPostUrlMapping(title); blogEntryUrlMappingLinq.BlogEntryUrlMappingPrimary = true; db.BlogEntryUrlMappings.InsertOnSubmit(blogEntryUrlMappingLinq); db.SubmitChanges(); } //+ blogEntryLinq.BlogEntryTitle = title; } if (!String.IsNullOrEmpty(blogEntryTypeGuid)) { blogEntryLinq.BlogEntryTypeId = blogEntryLinq.BlogEntryTypeId; } if (!String.IsNullOrEmpty(content)) { blogEntryLinq.BlogEntryText = content; } blogEntryLinq.BlogEntryStatusId = publish ? 1 : 3; //+ label foreach (LabelLINQ labelLinq in labelLinqList) { if (!blogEntryLinq.LabelBlogEntries.Any(p => p.LabelId == labelLinq.LabelId)) { LabelBlogEntryLINQ labelBlogEntryLinq = new LabelBlogEntryLINQ(); labelBlogEntryLinq.BlogEntryId = blogEntryLinq.BlogEntryId; labelBlogEntryLinq.LabelId = labelLinq.LabelId; //+ db.LabelBlogEntries.InsertOnSubmit(labelBlogEntryLinq); } } foreach (LabelBlogEntryLINQ labelBlogEntryLINQ in blogEntryLinq.LabelBlogEntries) { if (!labelLinqList.Any(p => p.LabelId == labelBlogEntryLINQ.LabelId)) { db.LabelBlogEntries.DeleteOnSubmit(labelBlogEntryLINQ); } } //+ db.SubmitChanges(); scope.Complete(); } } }