public ActionResult Post(NewPostViewModel npvm) { if (Session["User"] == null) { return RedirectToAction("LoginPage", "Application"); } else if (npvm.id != Convert.ToInt32(Session["User"])) { return RedirectToAction("LoggedInProfile"); } else if (this.ModelState.IsValid) { Post p = new Post(); Account a = db.Accounts.Find(Session["User"]); if (a == null) { return RedirectToAction("LoginPage", "Application"); } else { p.account_id = a.id; var sanitizer = new HtmlSanitizer(); if (npvm.entry == null) { npvm.entry = ""; } string sanitized = sanitizer.Sanitize(npvm.entry); p.entry = HttpUtility.HtmlEncode(sanitized); p.date_posted = DateTime.Now; foreach(LinkedItemViewModel livm in npvm.linked_items.Where(x => x.isChecked == true).ToList()) { LinkedItem li = new LinkedItem(); li.item_id = livm.id; li.post_id = p.id; db.LinkedItems.Add(li); } db.Posts.Add(p); db.SaveChanges(); return RedirectToAction("Boards", "Application"); } } else { return RedirectToAction("Boards", "Application"); } }
public BoardPostViewModel GetBoardPostViewModel(Post p) { BoardPostViewModel bpvm = new BoardPostViewModel(); bpvm.access_level_id = p.Account.access_level_id; if (p.Editor == null || p.date_edited == null) { bpvm.date_edited_text = ""; } else { bpvm.date_edited_text = "Edited by " + p.Editor.username + " on " + p.date_edited.ToString() + "."; } bpvm.date_posted_text = p.date_posted.ToString(); bpvm.entry = p.entry; bpvm.id = p.id; bpvm.account_id = p.account_id; bpvm.joined_date_text = "Member since " + p.Account.birthdate.ToString("MMMM d, yyyy"); bpvm.username = p.Account.username; bpvm.name = p.Account.first_name + " " + p.Account.last_name; List<LinkedItemViewModel> l_livm = new List<LinkedItemViewModel>(); foreach(LinkedItem li in p.LinkedItems.ToList()) { LinkedItemViewModel livm = new LinkedItemViewModel(); livm.id = li.item_id; livm.item_name = li.Item.name; l_livm.Add(livm); } bpvm.linked_items = l_livm; return bpvm; }