public ActionResult Write(WritePostViewModel post) { if (ModelState.IsValid) { this.Storage.Posts.Create(CurrentAccount, post.Content); this.Storage.SaveChanges(); return(this.RedirectToAction("Index", "Home")); } // throw new NotImplementedException(CurrentAccount.Name + " " + post.Content); return(this.View()); }
public IActionResult Edit(int id) { var thisPost = _db.Posts .Where(p => p.PostId == id) .FirstOrDefault(); var userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; WritePostViewModel editPost = new WritePostViewModel(thisPost); return(View(editPost)); }
// GET: Posts public ActionResult Write() { var CurrentlyLoggedInUser = (User)Session["User"]; if (CurrentlyLoggedInUser != null) { WritePostViewModel model = new WritePostViewModel(); return(View(model)); } else { return(RedirectToAction("Login", "Accounts")); } }
public IActionResult Edit(WritePostViewModel editPost) { Post savePost = WritePostViewModel.WritePostConvert(editPost); //check to see if the user making the POST request is the same as the owner of the post being edited string userId = this.User.FindFirst(ClaimTypes.NameIdentifier)?.Value; string originalPostUserId = TempData["AccountId"].ToString(); if (userId == originalPostUserId) { //Save changes to the actual post _db.Entry(savePost).State = EntityState.Modified; _db.SaveChanges(); return(RedirectToAction("Index")); } else { return(RedirectToAction("Index", "Home")); } }
public ActionResult Write(WritePostViewModel model) { var CurrentlyLoggedInUser = (User)Session["User"]; if (ModelState.IsValid) { Post newPost = new Post(); newPost.Title = model.Title; newPost.Summary = model.Summary; newPost.Description = model.Description; newPost.Image = db.Images.Where(x => x.ID == model.ImageID).FirstOrDefault(); newPost.PublishedTime = DateTime.Now; newPost.Author = db.Users.Where(u => u.ID == CurrentlyLoggedInUser.ID).FirstOrDefault(); db.Posts.Add(newPost); db.SaveChanges(); return(RedirectToAction("Details", new { postID = newPost.ID, postURL = newPost.URL })); } return(View(model)); }
public ActionResult Edit(WritePostViewModel model, string button) { using (ForumRespository db = new ForumRespository()) { Forum_Post Post = db.GetPostByID(model.id); if (Post == null) return NotFoundView("Post"); var Editor = GetCurrentUser(db); if (!db.CheckCategoryPermissions(Post.Forum_Thread.Forum_Category, Editor, P => (P.AllowEditOwnPost && Post.PosterID == Editor.UserID && Post.PosterID != (int)BuildInUser.Guest) || P.AllowEditAllPosts)) return AuthenticationHelper.AccessDeniedView(model); if (Post.Forum_Thread.Locked) return AuthenticationHelper.AccessDeniedView(model); if (IsHttpPost) { if (String.Equals(button, "preview", StringComparison.InvariantCultureIgnoreCase)) { model.ShowPost = true; model.PostHtml = PostParser.Parse(model.PostText); ModelState.Clear(); } else { if (!AntiForgeryTokenValid) { ModelState.AddModelError("AntiForgery", "The antiforgery token was invalid."); } else if (ModelState.IsValid) { Post.TimeStamp = DateTime.Now; Post.PostText = model.PostText; Post.Forum_Thread.LastPostTime = Post.TimeStamp; if (Post == Post.Forum_Thread.Forum_Posts[0] && !String.IsNullOrEmpty(Post.Forum_Thread.Title)) { Post.Forum_Thread.Title = model.ThreadTitle; } // Save to database db.Save(); int PostIndex = Post.Forum_Thread.Forum_Posts.IndexOf(Post); int EditedPostPage = PostIndex / POSTS_PER_PAGE + 1; int EditedPostNumber = PostIndex % POSTS_PER_PAGE + 1; return RedirectToAction("ViewThread", new { id = Post.ThreadID, page = EditedPostPage }).AddFragment(String.Format("Post_{0}", EditedPostNumber)); } } } else { model.PostText = Post.PostText; ModelState.Clear(); } if (Post == Post.Forum_Thread.Forum_Posts[0]) model.EditTitle = true; model.ThreadID = Post.ThreadID; model.Title = "Edit Post"; model.ThreadTitle = Post.Forum_Thread.Title; model.AddNavigation(Post.Forum_Thread); model.AddNavigation("Edit Post"); return View("WritePost", model); } }
public ActionResult Reply(WritePostViewModel model, string button, int QuoteId = 0) { using (ForumRespository db = new ForumRespository()) { Forum_Thread RepliedToThread = db.GetThreadByID(model.id); if (RepliedToThread == null) return NotFoundView("Thread"); model.AddNavigation(RepliedToThread); model.AddNavigation("Reply to thread"); Forum_User Replier = GetCurrentUser(db); if (!db.CheckCategoryPermissions(RepliedToThread.Forum_Category, Replier, P => P.AllowReply)) return AuthenticationHelper.AccessDeniedView(model); if (RepliedToThread.Locked) return AuthenticationHelper.AccessDeniedView(model); if (IsHttpPost) { if (String.Equals(button, "preview", StringComparison.InvariantCultureIgnoreCase)) { model.ShowPost = true; model.PostHtml = PostParser.Parse(model.PostText); ModelState.Clear(); } else if (!AntiForgeryTokenValid) { ModelState.AddModelError("AntiForgery", "The antiforgery token was invalid."); } else if (ModelState.IsValid) { Forum_Post ReplyPost = new Forum_Post(); ReplyPost.TimeStamp = DateTime.Now; ReplyPost.PosterID = Replier.UserID; ReplyPost.PostText = model.PostText; RepliedToThread.Forum_Posts.Add(ReplyPost); RepliedToThread.LastPostTime = ReplyPost.TimeStamp; RepliedToThread.Posts = RepliedToThread.Forum_Posts.Count; // Save to database db.Save(); int PostIndex = RepliedToThread.Forum_Posts.IndexOf(ReplyPost); int NewPostPage = PostIndex / POSTS_PER_PAGE + 1; int NewPostNumber = PostIndex % POSTS_PER_PAGE + 1; return RedirectToAction("ViewThread", new { id = RepliedToThread.ThreadID, page = NewPostPage }).AddFragment(String.Format("Post_{0}", NewPostNumber)); } } else { ModelState.Clear(); Forum_Post QuotedPost = db.GetPostByID(QuoteId); if (QuotedPost != null) { model.PostText = String.Format("[quote={0}]{1}[/quote]", QuotedPost.Forum_User.Username, QuotedPost.PostText); } } model.ThreadID = model.id; model.Title = "Reply to Thread"; return View("WritePost", model); } }
public ActionResult NewThread(WritePostViewModel model, string button) { using (ForumRespository db = new ForumRespository()) { Forum_Category Category = db.GetCategoryByID(model.id); if (Category == null) return NotFoundView("Category"); model.AddNavigation(Category); model.AddNavigation("New thread"); Forum_User Poster = GetCurrentUser(db); if (!db.CheckCategoryPermissions(Category, Poster, P => P.AllowNewThread)) return AuthenticationHelper.AccessDeniedView(model); if (String.Equals(button, "preview", StringComparison.InvariantCultureIgnoreCase)) { model.ShowPost = true; model.PostHtml = PostParser.Parse(model.PostText); ModelState.Clear(); } else if (IsHttpPost) { if (!AntiForgeryTokenValid) { ModelState.AddModelError("AntiForgery", "The antiforgery token was invalid."); } if (String.IsNullOrEmpty(model.ThreadTitle)) { ModelState.AddModelError("ThreadTitle", "A thread title is required."); } if (ModelState.IsValid) { Forum_Thread NewThread = new Forum_Thread(); NewThread.Title = model.ThreadTitle; NewThread.PosterID = Poster.UserID; Forum_Post InitialPost = new Forum_Post(); InitialPost.TimeStamp = DateTime.Now; InitialPost.PosterID = NewThread.PosterID; InitialPost.PostText = model.PostText; NewThread.Forum_Posts.Add(InitialPost); NewThread.Posts = 1; NewThread.LastPostTime = InitialPost.TimeStamp; NewThread.CategoryID = model.id; // Save and add thread to database db.AddThread(NewThread); db.SetLastPost(NewThread, Poster, 1); db.Save(); return RedirectToAction("ViewCategory", new { id = model.id }); } } else { ModelState.Clear(); } model.EditTitle = true; model.Title = "Post new Thread"; return View("WritePost", model); } }