Beispiel #1
0
 public Response ArchiveBlogPostToRepo(BlogPost blogPost)
 {
     _response = new Response();
     _repo.ArchiveBlogPostDB(blogPost);
     _response.Success = true;
     return _response;
 }
        public ActionResult ArchivePost(BlogPost blogPost)
        {
            _res = new Response();
            _ops = new MVCBlogOps();

            _res = _ops.ArchiveBlogPostToRepo(blogPost);
            return RedirectToAction("Index", "Home");
        }
Beispiel #3
0
        public void ArchiveBlogPostDB(BlogPost blogPost)
        {
            using (var cn = new SqlConnection(Settings.ConnectionString))
            {
                var p = new DynamicParameters();
                p.Add("@BlogPostID", blogPost.BlogPostID);

                cn.Execute("ArchiveBlogPostByID", p, commandType: CommandType.StoredProcedure);
            }
        }
Beispiel #4
0
 public Response()
 {
     Category = new Category();
     Categories = new List<Category>();
     HashTag = new HashTag();
     HashTags = new List<HashTag>();
     User = new User();
     BlogPost = new BlogPost();
     BlogPosts = new List<BlogPost>();
     Mce = new TinyMceClass();
 }
Beispiel #5
0
        public void CreateBlogPostDB(BlogPost blogPost)
        {
            using (var cn = new SqlConnection(Settings.ConnectionString))
            {
                var p = new DynamicParameters();
                p.Add("@UserID", blogPost.User.UserID);
                p.Add("@Title", blogPost.Title);
                p.Add("@Body", blogPost.Mce.Body);
                p.Add("@CategoryID", blogPost.Category.CategoryID);
                p.Add("@PostDate", DateTime.Today);
                p.Add("@Status", blogPost.Status);

                p.Add("@BlogPostID", DbType.Int32, direction: ParameterDirection.Output);
                cn.Execute("BlogPostInsert", p, commandType: CommandType.StoredProcedure);

                var blogPostID = p.Get<int>("BlogPostID");

                _hashTags = GetAllHashTags();

                foreach (var hashTag in blogPost.HashTags) //new user hashtags
                {

                    var ht = _hashTags.FirstOrDefault(h => h.HashTagName == hashTag.HashTagName);
                    var hashTagID = 0;
                    if (ht == null)
                    {
                        var p2 = new DynamicParameters();
                        p2.Add("@HashTagName", hashTag.HashTagName);
                        p2.Add("@HashTagID", DbType.Int32, direction: ParameterDirection.Output);
                        cn.Execute("HashTagInsert", p2, commandType: CommandType.StoredProcedure);
                        hashTagID = p2.Get<int>("HashTagID");
                        _hashTags.Add(new HashTag() { HashTagID = hashTagID, HashTagName = hashTag.HashTagName });
                    }
                    else
                    {
                        hashTagID = ht.HashTagID;
                    }
                    var p4 = new DynamicParameters();

                    p4.Add("@HashTagID", hashTagID);
                    p4.Add("@BlogPostID", blogPostID);
                    cn.Execute("HashTagPostInsert", p4, commandType: CommandType.StoredProcedure);
                }

            }
        }
Beispiel #6
0
        public void CreateBlogPostDB()
        {
            var blogPost = new BlogPost()
            {
                BlogPostID = 0,
                Body = "",
                Categories = new List<Category>(),
                Category = new Category()
                {
                    CategoryID = 2,
                    CategoryName = "Daily Picks"
                },
                HashTag = new HashTag()
                {
                    HashTagID = 1,
                    HashTagName = "#savings"
                },
                HashTags = new List<HashTag>(),
                Mce = new TinyMceClass()
                {
                    Body = "Unit Testing for CreateBlogPost"
                },
                PostDate = DateTime.Today,
                Status = 2,
                tags = new List<string>()
                {
                    "#goodfood",
                    "#bestdeals"
                }
            };

            var repo = new MVCBlogRepo();

            repo.CreateBlogPostDB(blogPost);

            var cn = new SqlConnection(Settings.ConnectionString);

            var expected = cn.Query<int>("SELECT COUNT(BlogPostID) FROM BlogPosts").FirstOrDefault();

            Assert.AreEqual(expected, 9);
        }
Beispiel #7
0
 public BlogPostVM()
 {
     blogPost = new BlogPost();
     category = new Category();
 }
Beispiel #8
0
        public List<BlogPost> GetAllBlogPosts()
        {
            List<BlogPost> posts = new List<BlogPost>();

            using (var cn = new SqlConnection(Settings.ConnectionString))
            {
                SqlCommand cmd = cn.CreateCommand();
                cmd.CommandText = "GetAllBlogPostsOrderByCategory";
                cmd.CommandType = CommandType.StoredProcedure;

                cn.Open();

                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    while (dr.Read())
                    {
                        var hashTag = new HashTag();

                        hashTag.HashTagID = dr.GetInt32(7);
                        hashTag.HashTagName = dr.GetString(8);

                        var testPostID = dr.GetInt32(0);

                        var item = posts.Where(p => p.BlogPostID == testPostID).FirstOrDefault();

                        if (item == null)
                        {
                            BlogPost post = new BlogPost();
                            post.BlogPostID = testPostID;
                            post.Title = dr.GetString(1);
                            post.Body = dr.GetString(2);
                            post.PostDate = dr.GetDateTime(3);
                            post.Category.CategoryID = dr.GetInt32(4);
                            post.Status = dr.GetInt32(5);
                            post.Category.CategoryName = dr.GetString(6);
                            post.User.UserID = dr.GetString(9);
                            post.User.UserName = dr.GetString(10);

                            post.HashTags.Add(hashTag);

                            posts.Add(post);
                        }
                        else
                        {
                            item.HashTags.Add(hashTag);
                        }
                    }
                }
            }
            return posts;
        }
        public ActionResult CreatePostPost(BlogPostVM blogPostVM)
        {
            _ops = new MVCBlogOps();

                var blogPost = new BlogPost();
                var userManager =
                    new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                var user = userManager.FindById(User.Identity.GetUserId());
                if (User.IsInRole("Admin"))
                {
                    blogPostVM.blogPost.Status = 1; // 1 is Approved
                    blogPost.Status = blogPostVM.blogPost.Status;
                }
                else
                {
                    blogPostVM.blogPost.Status = 2; // 2 is Unapproved
                    blogPost.Status = blogPostVM.blogPost.Status;
                }

                blogPost.User.UserID = user.Id;
                blogPost.Title = blogPostVM.blogPost.Title;
                blogPost.Mce.Body = blogPostVM.blogPost.Mce.Body;
                blogPost.Category.CategoryID = blogPostVM.category.CategoryID;

                if (blogPostVM.tags == null)
                {
                    HashTag hashTag = new HashTag();
                    hashTag.HashTagName = "#freshfoods";
                    blogPost.HashTags.Add(hashTag);
                }
                else
                {
                    foreach (var item in blogPostVM.tags)
                    {
                        HashTag hashTag = new HashTag();
                        hashTag.HashTagName = item;
                        blogPost.HashTags.Add(hashTag);
                    }
                }

            _ops.SaveBlogPostToRepo(blogPost);

            return RedirectToAction("Index", "Home");
        }