public ActionResult createPost(string postTitle, string postBody) { // clean up the whitespace postBody = postBody.Trim(); postTitle = postTitle.Trim(); // Declare our database connection // will exist only in the scope // of the using statement. using (Helpers.DAL.CapstoneEntities db = new Helpers.DAL.CapstoneEntities()) { Helpers.DAL.tTopicPost newPost = new Helpers.DAL.tTopicPost() { userID = Helpers.HelperQueries.getUserID(User.Identity.Name), topicTitle = postTitle, topicDate = DateTime.Now, topicPost = postBody }; db.AddTotTopicPosts(newPost); db.SaveChanges(); object returnData = new { postID = newPost.topicPostID, postedBy = User.Identity.Name, postTitle = postTitle, postBody = postBody, postDate = DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString() }; return Json(returnData, JsonRequestBehavior.AllowGet); } }
public ActionResult deletePost(int postID) { using (Helpers.DAL.CapstoneEntities db = new Helpers.DAL.CapstoneEntities()) { var replies = from x in db.tTopicPosts where (x.topicParentID == postID) select x; foreach (var reply in replies) { // attach it back to the table (?) i know this is dumb db.CreateObjectSet <Helpers.DAL.tTopicPost>().Attach(reply); // Delete it db.ObjectStateManager.ChangeObjectState(reply, System.Data.EntityState.Deleted); } // this is a bit of mojo to delete a post without a stored proc var post = new Helpers.DAL.tTopicPost(); // create a post, assign its id post.topicPostID = postID; // attach it back to the table (?) i know this is dumb db.CreateObjectSet <Helpers.DAL.tTopicPost>().Attach(post); // Delete it db.ObjectStateManager.ChangeObjectState(post, System.Data.EntityState.Deleted); db.SaveChanges(); }; return(Json(postID)); }
public ActionResult createPost(string postTitle, string postBody) { // clean up the whitespace postBody = postBody.Trim(); postTitle = postTitle.Trim(); // Declare our database connection // will exist only in the scope // of the using statement. using (Helpers.DAL.CapstoneEntities db = new Helpers.DAL.CapstoneEntities()) { Helpers.DAL.tTopicPost newPost = new Helpers.DAL.tTopicPost() { userID = Helpers.HelperQueries.getUserID(User.Identity.Name), topicTitle = postTitle, topicDate = DateTime.Now, topicPost = postBody }; db.AddTotTopicPosts(newPost); db.SaveChanges(); object returnData = new { postID = newPost.topicPostID, postedBy = User.Identity.Name, postTitle = postTitle, postBody = postBody, postDate = DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString() }; return(Json(returnData, JsonRequestBehavior.AllowGet)); } }
public ActionResult postComment(int postID, string comment) { // Do some data transforms, and encoding comment = comment.Trim(); // Declare our database connection // will exist only in the scope // of the using statement. using (Helpers.DAL.CapstoneEntities db = new Helpers.DAL.CapstoneEntities()) { Helpers.DAL.tTopicPost commentPost = new Helpers.DAL.tTopicPost() { topicParentID = postID, topicPost = comment, topicDate = DateTime.Now, userID = Helpers.HelperQueries.getUserID(User.Identity.Name), // theres a ridiculous bug here - it wont let me save null // objects on a nullable field, so set the title to 0 topicTitle = "0" }; db.AddTotTopicPosts(commentPost); db.SaveChanges(); } // This is where data modeling comes in handy. I hate that I have to declare yet ANOTHER model, but hey we cant have our cake and eat it too. ReplyModel returnObject = new ReplyModel() { parentID = postID, postBody = MSA.Encoder.HtmlEncode(comment), postDate = DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString(), postedBy = User.Identity.Name }; return(Json(returnObject, JsonRequestBehavior.AllowGet)); }
public ActionResult deletePost(int postID) { using (Helpers.DAL.CapstoneEntities db = new Helpers.DAL.CapstoneEntities()) { var replies = from x in db.tTopicPosts where (x.topicParentID == postID) select x; foreach(var reply in replies) { // attach it back to the table (?) i know this is dumb db.CreateObjectSet<Helpers.DAL.tTopicPost>().Attach(reply); // Delete it db.ObjectStateManager.ChangeObjectState(reply, System.Data.EntityState.Deleted); } // this is a bit of mojo to delete a post without a stored proc var post = new Helpers.DAL.tTopicPost(); // create a post, assign its id post.topicPostID = postID; // attach it back to the table (?) i know this is dumb db.CreateObjectSet<Helpers.DAL.tTopicPost>().Attach(post); // Delete it db.ObjectStateManager.ChangeObjectState(post, System.Data.EntityState.Deleted); db.SaveChanges(); }; return Json(postID); }
public ActionResult postComment(int postID, string comment) { // Do some data transforms, and encoding comment = comment.Trim(); // Declare our database connection // will exist only in the scope // of the using statement. using (Helpers.DAL.CapstoneEntities db = new Helpers.DAL.CapstoneEntities()) { Helpers.DAL.tTopicPost commentPost = new Helpers.DAL.tTopicPost() { topicParentID = postID, topicPost = comment, topicDate = DateTime.Now, userID = Helpers.HelperQueries.getUserID(User.Identity.Name), // theres a ridiculous bug here - it wont let me save null // objects on a nullable field, so set the title to 0 topicTitle = "0" }; db.AddTotTopicPosts(commentPost); db.SaveChanges(); } // This is where data modeling comes in handy. I hate that I have to declare yet ANOTHER model, but hey we cant have our cake and eat it too. ReplyModel returnObject = new ReplyModel() { parentID = postID, postBody = MSA.Encoder.HtmlEncode(comment), postDate = DateTime.Now.ToShortDateString() + " @ " + DateTime.Now.ToShortTimeString(), postedBy = User.Identity.Name }; return Json(returnObject, JsonRequestBehavior.AllowGet); }