Ejemplo n.º 1
0
        public ActionResult Blog(string username)
        {
            BlogViewModel model = new BlogViewModel();
            // The blog is the main site's blog
            if (string.IsNullOrEmpty(username))
            {
                ViewBag.Title = Config.BlogConfig.Title + " - " + Config.Title;
                ViewBag.Description = Config.BlogConfig.Description;
                bool isAuth = User.IsInRole("Admin");
                var foundPosts = db.BlogPosts.Where(p => ((p.System || isAuth) && p.Published));
                model = new BlogViewModel();
                model.BlogId = Config.BlogConfig.ServerBlogId;

                User user = (User.IsInRole("Admin")) ? UserHelper.GetUser(db, User.Identity.Name) : null;
                model.UserId = (user != null) ? user.UserId : 0;
                model.User = user;
                model.Title = Config.BlogConfig.Title;
                model.Description = Config.BlogConfig.Description;
                model.HasPosts = (foundPosts != null && foundPosts.Any());

                return View(model);
            }
            else // A user specific blog
            {
                Models.Blog blog = db.Blogs.Where(p => p.User.Username == username && p.BlogId != Config.BlogConfig.ServerBlogId).FirstOrDefault();
                // find the blog specified
                if (blog != null)
                {
                    ViewBag.Title = blog.User.Username + "'s Blog - " + Config.Title;
                    if (!string.IsNullOrEmpty(blog.User.BlogSettings.Title))
                    {
                        ViewBag.Title = blog.User.BlogSettings.Title + " - " + ViewBag.Title;
                    }
                    ViewBag.Description = blog.User.BlogSettings.Description;
                    bool isAuth = User.IsInRole("Admin");
                    var foundPosts = db.BlogPosts.Where(p => (p.BlogId == blog.BlogId && !p.System) && 
                                                                                                    (p.Published || p.Blog.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
                    model = new BlogViewModel();
                    model.BlogId = blog.BlogId;
                    model.UserId = blog.UserId;
                    model.User = blog.User;
                    model.Title = blog.User.BlogSettings.Title;
                    model.Description = blog.User.BlogSettings.Description;
                    model.HasPosts = (foundPosts != null);

                    return View(model);
                }
            }
            model.Error = true;
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult CreatePost(int blogID, string title, string article)
        {
            BlogViewModel model = new BlogViewModel();
            if (ModelState.IsValid)
            {
                bool isAuth = User.IsInRole("Admin");
                var blog = db.Blogs.Where(p => (p.BlogId == blogID) && (p.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
                if (blog != null)
                {
                    if (User.IsInRole("Admin") || db.Blogs.Where(b => b.User.Username == User.Identity.Name).FirstOrDefault() != null)
                    {
                        // Validate the fields
                        if (string.IsNullOrEmpty(title))
                        {
                            model.Error = true;
                            model.ErrorMessage = "You must write something for the title";
                            return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
                        }

                        if (string.IsNullOrEmpty(article))
                        {
                            model.Error = true;
                            model.ErrorMessage = "You must write something for the article";
                            return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
                        }

                        bool system = (blogID == Config.BlogConfig.ServerBlogId);
                        if (system)
                        {
                            var user = db.Blogs.Where(b => b.User.Username == User.Identity.Name);
                            if (user != null)
                            {
                                blogID = user.First().BlogId;
                            }
                        }
                        BlogPost post = db.BlogPosts.Create();
                        post.BlogId = blogID;
                        post.Title = title;
                        post.Article = article;
                        post.System = system;
                        post.DatePosted = DateTime.Now;
                        post.DatePublished = DateTime.Now;
                        post.DateEdited = DateTime.Now;

                        db.BlogPosts.Add(post);
                        db.SaveChanges();
                        return Redirect(Url.SubRouteUrl("blog", "Blog.Post", new { username = blog.User.Username, id = post.BlogPostId }));
                    }
                    model.Error = true;
                    model.ErrorMessage = "You are not authorized to create a post for this blog";
                    return View("~/Areas/Blog/Views/Blog/Blog.cshtml", model);
                }
                model.Error = true;
                model.ErrorMessage = "Blog does not exist.";
                return View("~/Areas/Blog/Views/Blog/Blog.cshtml", model);
            }
            model.Error = true;
            model.ErrorMessage = "No post created";
            return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
        }
Ejemplo n.º 3
0
 public ActionResult NewPost(string username, int blogID)
 {
     if (string.IsNullOrEmpty(username))
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     BlogViewModel model = new BlogViewModel();
     // find the post specified
     bool isAuth = User.IsInRole("Admin");
     var blog = db.Blogs.Where(p => (p.BlogId == blogID) && (p.User.Username == User.Identity.Name || isAuth)).FirstOrDefault();
     if (blog != null)
     {
         model = new BlogViewModel(blog);
         if (blog.User.Username == Constants.SERVERUSER)
         {
             ViewBag.Title = model.Title + " - " + Config.BlogConfig.Title + " - " + Config.Title;
             ViewBag.Description = Config.BlogConfig.Description;
         }
         else
         {
             ViewBag.Title = username + "'s Blog - " + Config.Title;
             if (!string.IsNullOrEmpty(blog.User.BlogSettings.Title))
             {
                 ViewBag.Title = blog.User.BlogSettings.Title + " - " + ViewBag.Title;
             }
             ViewBag.Title = model.Title + " - " + ViewBag.Title;
             ViewBag.Description = blog.User.BlogSettings.Description;
         }
         return View("~/Areas/Blog/Views/Blog/NewPost.cshtml", model);
     }
     model.Error = true;
     model.ErrorMessage = "Blog does not exist.";
     return View("~/Areas/Blog/Views/Blog/Blog.cshtml", model);
 }