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 createShout(string shoutMessage) { // pull the person making the shout from the // current session string userName = User.Identity.Name; // Declare our database context // using (var db = new Helpers.DAL.CapstoneEntities()) { // Declare a new shout - use the Entity Framework object // to make this easier use object initialization format Helpers.DAL.tShout thisShout = new Helpers.DAL.tShout() { shoutString = shoutMessage, userID = Helpers.HelperQueries.getUserID(userName) }; db.AddTotShouts(thisShout); db.SaveChanges(); var returnShout = new Models.ShoutModel() { shoutString = thisShout.shoutString, userID = Helpers.HelperQueries.GetUserName((int)thisShout.userID) }; // this tshout now contains everything we need to copy it to // a blank object - that we can JSON encode and return to the browser // and use jquery to append it to he html <Span> return Json(returnShout, JsonRequestBehavior.AllowGet); } }
// set this to require authorization public ActionResult createShout(string shoutMessage) { // pull the person making the shout from the // current session string userName = User.Identity.Name; // Declare our database context // using (var db = new Helpers.DAL.CapstoneEntities()) { // Declare a new shout - use the Entity Framework object // to make this easier use object initialization format Helpers.DAL.tShout thisShout = new Helpers.DAL.tShout() { shoutString = shoutMessage, userID = Helpers.HelperQueries.getUserID(userName) }; db.AddTotShouts(thisShout); db.SaveChanges(); var returnShout = new Models.ShoutModel() { shoutString = thisShout.shoutString, userID = Helpers.HelperQueries.GetUserName((int)thisShout.userID) }; // this tshout now contains everything we need to copy it to // a blank object - that we can JSON encode and return to the browser // and use jquery to append it to he html <Span> return(Json(returnShout, 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 readShout() { // create your JSon Object context using (var db = new Helpers.DAL.CapstoneEntities()) { // Linq query // var listOfShouts = from x in db.tShouts // select x; var listOfShouts = from x in db.tShouts select x; /*new Models.ShoutModel() * { * // This is stupid - entity framework until its fully assigned says ?int * // and i said " Look bitch, its an int. GTFO" -- seems needless i know * // but it worked. Pseudo objects blow under certain circumstances * userID = String.Format("{0}", x.userID), * shoutString = x.shoutString * };*/ // You have a list of shouts, iterate through them and add them to the return object var p = new List <Models.ShoutModel>(); foreach (var shout in listOfShouts) { var y = new Models.ShoutModel() { shoutString = shout.shoutString, userID = Helpers.HelperQueries.GetUserName((int)shout.userID) }; p.Add(y); } // return JSON return(Json(p, JsonRequestBehavior.AllowGet)); } }
public static string FetchShoutBoxJson() { // create your JSon Object context using (var db = new Helpers.DAL.CapstoneEntities()) { // Linq query // var listOfShouts = from x in db.tShouts // select x; var listOfShouts = from x in db.tShouts select x; /*new Models.ShoutModel() { // This is stupid - entity framework until its fully assigned says ?int // and i said " Look bitch, its an int. GTFO" -- seems needless i know // but it worked. Pseudo objects blow under certain circumstances userID = String.Format("{0}", x.userID), shoutString = x.shoutString };*/ // You have a list of shouts, iterate through them and add them to the return object var p = new List<Models.ShoutModel>(); foreach (var shout in listOfShouts) { var y = new Models.ShoutModel() { shoutString = shout.shoutString, userID = HelperQueries.GetUserName((int)shout.userID) }; p.Add(y); } // return JSON return (JsonConvert.SerializeObject(p)); } }
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 readShout() { // create your JSon Object context using (var db = new Helpers.DAL.CapstoneEntities()) { // Linq query // var listOfShouts = from x in db.tShouts // select x; var listOfShouts = from x in db.tShouts select new { userID = x.userID, shoutString = x.shoutString }; // listOfShouts is now a loaded collection of all the shouts in the table. what do you do with them // to get them back to the browser in a consistent and human readable way? var p = new List<Models.ShoutModel>(); viewData("ShoutBoxJSON") = Json(ListOfShoutModels); foreach (var x in listOfShouts) { var y = new Models.ShoutModel() { userID = Helpers.HelperQueries.GetUserName((int)x.userID), shoutString = x.shoutString }; p.Add(y); } viewData("ShoutBoxJSON")=Json(p); return Json(p, 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 getPosts() { object postObject = new object(); // Declare a connection to the database using (var db = new Helpers.DAL.CapstoneEntities()) { // Pull the parent posts using Linq var parentPosts = from post in db.tTopicPosts where post.topicParentID == null // this denotes it is a root-level post. or a Parent post select new { postID = post.topicPostID, postTopic = post.topicTitle, postDate = post.topicDate, postedBy = post.userID, postBody = post.topicPost }; // using the json logic from // http://stackoverflow.com/questions/5978904/how-to-build-object-hierarchy-for-serialization-with-json-net // build a list to hold all of the post objects var jsonPosts = new List<object>(); // Iterate through each post - and build an object that we can serialize to JSON foreach (var post in parentPosts) { // Declare it as a temporary post - not very memory efficient but it works var tempPost = new { // Set each property - since this is an anonymous object, we could be arbitrary // with the property names - but lets be intelligent here post.postID, // anonymous properties gain the name of the 'host' object postDate = post.postDate.ToShortDateString() + " @ " + post.postDate.ToShortTimeString(), // by default, dates are serial strings that make no sense - ms from epoch? postedBy = Helpers.HelperQueries.GetUserName(post.postedBy), postTitle = post.postTopic, postBody = post.postBody, Replys = new List<object>() // important - notice how its a list of object? }; // lets use the prior list now - pull the replys we want to populate it with var replys = from reply in db.tTopicPosts where reply.topicParentID == post.postID // remember that whole list of object thing above? // we can be arbitrary here too - lets not get funky tho select new { postID = reply.topicPostID, postedBy = reply.userID, postDate = reply.topicDate, postTitle = reply.topicTitle, postBody = reply.topicPost }; // Now ITERATE ALL THE THINGS AGAIN!! foreach (var reply in replys) { var tempReply = new { postID = reply.postID, postedBy = Helpers.HelperQueries.GetUserName(reply.postedBy), postDate = reply.postDate.ToShortDateString() + " @ " + reply.postDate.ToShortTimeString(), // by default, dates are serial strings that make no sense - ms from epoch? postBody = reply.postBody }; // add it to the temporary post object tempPost.Replys.Add(tempReply); } // add it to the parent list jsonPosts.Add(tempPost); } // serialize it and return it. return Json(Newtonsoft.Json.JsonConvert.SerializeObject(jsonPosts), JsonRequestBehavior.AllowGet); } // close DB connection }
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 getPosts() { object postObject = new object(); // Declare a connection to the database using (var db = new Helpers.DAL.CapstoneEntities()) { // Pull the parent posts using Linq var parentPosts = from post in db.tTopicPosts where post.topicParentID == null // this denotes it is a root-level post. or a Parent post select new { postID = post.topicPostID, postTopic = post.topicTitle, postDate = post.topicDate, postedBy = post.userID, postBody = post.topicPost }; // using the json logic from // http://stackoverflow.com/questions/5978904/how-to-build-object-hierarchy-for-serialization-with-json-net // build a list to hold all of the post objects var jsonPosts = new List <object>(); // Iterate through each post - and build an object that we can serialize to JSON foreach (var post in parentPosts) { // Declare it as a temporary post - not very memory efficient but it works var tempPost = new { // Set each property - since this is an anonymous object, we could be arbitrary // with the property names - but lets be intelligent here post.postID, // anonymous properties gain the name of the 'host' object postDate = post.postDate.ToShortDateString() + " @ " + post.postDate.ToShortTimeString(), // by default, dates are serial strings that make no sense - ms from epoch? postedBy = Helpers.HelperQueries.GetUserName(post.postedBy), postTitle = post.postTopic, postBody = post.postBody, Replys = new List <object>() // important - notice how its a list of object? }; // lets use the prior list now - pull the replys we want to populate it with var replys = from reply in db.tTopicPosts where reply.topicParentID == post.postID // remember that whole list of object thing above? // we can be arbitrary here too - lets not get funky tho select new { postID = reply.topicPostID, postedBy = reply.userID, postDate = reply.topicDate, postTitle = reply.topicTitle, postBody = reply.topicPost }; // Now ITERATE ALL THE THINGS AGAIN!! foreach (var reply in replys) { var tempReply = new { postID = reply.postID, postedBy = Helpers.HelperQueries.GetUserName(reply.postedBy), postDate = reply.postDate.ToShortDateString() + " @ " + reply.postDate.ToShortTimeString(), // by default, dates are serial strings that make no sense - ms from epoch? postBody = reply.postBody }; // add it to the temporary post object tempPost.Replys.Add(tempReply); } // add it to the parent list jsonPosts.Add(tempPost); } // serialize it and return it. return(Json(Newtonsoft.Json.JsonConvert.SerializeObject(jsonPosts), JsonRequestBehavior.AllowGet)); } // close DB connection } // end get posts