public bool SubmitComment([FromBody] CommentSubmissionModel cm) { var sanitizer = new Ganss.XSS.HtmlSanitizer(); using (var db = new ChaliceDb()) { db.BeginTransaction(); var comment = new Comment { Glyph = cm.Glyph, CommentText = sanitizer.Sanitize(cm.Text), PostedBy = User.Identity.Name, Posted = System.DateTime.Now }; var history = new UserHistory { UserName = User.Identity.Name, Action = "comment", Target = cm.Glyph, Value = "", Created = System.DateTime.Now }; db.InsertWithIdentity(comment); db.InsertWithIdentity(history); db.CommitTransaction(); } return(true); }
public IActionResult PostNewHomepageItem([FromForm] HomePageItem item) { if (UserHasAdminRoles() == false) { return(View("_Error", "You are not authorized to do this")); } using (var db = new ChaliceDb()) { db.BeginTransaction(); db.InsertWithIdentity(new Article { Section = "homepage", Title = item.Title, Content = item.Content, PostedBy = User.Identity.Name, Posted = System.DateTime.Now }); db.InsertWithIdentity(new UserHistory { UserName = User.Identity.Name, Action = "new_article", Target = "homepage", Value = item.Title, Created = System.DateTime.Now }); db.CommitTransaction(); } return(Redirect("/")); }
public bool SubmitVote([FromBody] VotePackageModel vote) { using (var db = new ChaliceDb()) { db.BeginTransaction(); var glyph = db.DungeonGlyphs.FirstOrDefault(d => d.Glyph == vote.Glyph); switch (vote.Vote.ToLower()) { case "up": glyph.Upvotes += 1; break; case "down": glyph.Downvotes += 1; break; case "retract": { var prevVote = db.UserHistory.FirstOrDefault(h => h.UserName == User.Identity.Name && h.Target == vote.Glyph && h.Action == "vote"); switch (prevVote.Value) { case "up": glyph.Upvotes -= 1; break; case "down": glyph.Downvotes -= 1; break; case "closed": glyph.ClosedVotes -= 1; break; } db.Delete(prevVote); } break; case "closed": { // Delete previous vote if needed var prevVote = db.UserHistory.FirstOrDefault(h => h.UserName == User.Identity.Name && h.Target == vote.Glyph && h.Action == "vote"); if (prevVote != null) { switch (prevVote.Value) { case "up": glyph.Upvotes -= 1; break; case "down": glyph.Downvotes -= 1; break; } db.Delete(prevVote); } glyph.ClosedVotes += 1; } break; } db.Update(glyph); // Don't write history for vote retraction if (vote.Vote.ToLower() != "retract") { db.InsertWithIdentity(new UserHistory { UserName = User.Identity.Name, Action = "vote", Target = vote.Glyph, Value = vote.Vote, Created = System.DateTime.Now }); } db.CommitTransaction(); } return(true); }