Esempio n. 1
0
 public void AddHashTag(Hashtag newHash)
 {
     using (var ctx = new TechTruffleShuffleEntities())
     {
         ctx.Hashtag.Add(newHash);
         ctx.SaveChanges();
     }
 }
Esempio n. 2
0
 public void CreateStaticPage(StaticPage staticPage)
 {
     using (var ctx = new TechTruffleShuffleEntities())
     {
         ctx.StaticPage.Add(staticPage);
         ctx.SaveChanges();
     }
 }
Esempio n. 3
0
 public void DeleteBlogPostDraft(int postId)
 {
     using (var ctx = new TechTruffleShuffleEntities())
     {
         var deleteThis = ctx.BlogPost.SingleOrDefault(i => i.BlogPostId == postId);
         ctx.BlogPost.Remove(deleteThis);
         ctx.SaveChanges();
     };
 }
Esempio n. 4
0
        public void CreateNewBlogPost(BlogPost newPost)
        {
            using (var ctx = new TechTruffleShuffleEntities())
            {
                var thisUser   = ctx.Users.SingleOrDefault(u => u.UserName == newPost.User.UserName);
                var thisStatus = ctx.BlogStatus.SingleOrDefault(s => s.BlogStatusId == newPost.BlogStatusId);
                newPost.User = thisUser;

                foreach (var hash in newPost.Hashtags.Where(i => i.HashtagId > 0))
                {
                    ctx.Hashtag.Attach(hash);
                }

                ctx.BlogStatus.Attach(newPost.BlogStatus);
                ctx.BlogPost.Add(newPost);
                ctx.SaveChanges();
            }
        }
Esempio n. 5
0
        public void EditBlogPost(BlogPost updatedBlogPost)
        {
            using (var ctx = new TechTruffleShuffleEntities())
            {
                updatedBlogPost.User = ctx.Users.SingleOrDefault(u => u.Id == updatedBlogPost.UserId);

                updatedBlogPost.BlogCategoryId = updatedBlogPost.BlogCategory.BlogCategoryId;
                updatedBlogPost.BlogStatusId   = updatedBlogPost.BlogStatus.BlogStatusId;



                var allTagsOnBlogPost = updatedBlogPost.Hashtags;
                updatedBlogPost.Hashtags = new List <Hashtag>();
                ctx.BlogPost.Attach(updatedBlogPost);
                ctx.Entry(updatedBlogPost).State = System.Data.Entity.EntityState.Modified;
                ctx.Entry(updatedBlogPost).Collection(i => i.Hashtags).Load();

                var current = updatedBlogPost.Hashtags.ToList();

                updatedBlogPost.Hashtags.Clear();

                foreach (var hash in allTagsOnBlogPost)
                {
                    var currentHashTag = current.SingleOrDefault(i => i.HashtagId == hash.HashtagId);
                    if (currentHashTag == null)
                    {
                        ctx.Hashtag.Attach(hash);
                        updatedBlogPost.Hashtags.Add(hash);
                    }
                    else
                    {
                        updatedBlogPost.Hashtags.Add(currentHashTag);
                    }
                }

                ctx.BlogStatus.Attach(updatedBlogPost.BlogStatus);
                ctx.SaveChanges();
            }
        }