public IHttpActionResult PutUpdatedCommentHeader(int section_id, int term_id, [FromBody] String content) { using (WebhostEntities db = new WebhostEntities()) { int header = -1; try { header = db.CommentHeaders.Where(h => h.SectionIndex == section_id && h.TermIndex == term_id).Single().id; } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e))); } CommentHeader headerParagraph = db.CommentHeaders.Find(header); content = CommentLetter.ConvertFromBase64String(content); headerParagraph.HTML = CommentLetter.CleanTags(content); try { db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new CommentHeaderInfo(header)))); } }
public IHttpActionResult CreateNewCommentHeader(int section_id, [FromBody] String content) { using (WebhostEntities db = new WebhostEntities()) { Section section = db.Sections.Find(section_id); if (section == null) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Invalid Section Id.")))); } WebhostUserPrinciple principal = (WebhostUserPrinciple)ActionContext.RequestContext.Principal; int id = ((WebhostIdentity)principal.Identity).User.Id; Faculty self = db.Faculties.Find(id); if (!self.Sections.Contains(section)) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, new UnauthorizedAccessException("You are not a teacher for this class.")))); } int termId = DateRange.GetCurrentOrLastTerm(); if (section.CommentHeaders.Where(h => h.TermIndex == termId).Count() > 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.BadRequest, new ArgumentException("Header Already Exists. Use PUT to Update.")))); } CommentHeader newHeader = new CommentHeader() { id = db.CommentHeaders.OrderBy(h => h.id).ToList().Last().id + 1, SectionIndex = section_id, TermIndex = termId, HTML = "" }; content = CommentLetter.ConvertFromBase64String(content); newHeader.HTML = CommentLetter.CleanTags(content); try { db.CommentHeaders.Add(newHeader); db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.Created, new CommentHeaderInfo(newHeader.id), "text/json"))); } }
public async Task <IHttpActionResult> PostNewCommentHeader(int section_id, int term_id) { using (WebhostEntities db = new WebhostEntities()) { if (db.CommentHeaders.Where(h => h.SectionIndex == section_id && h.TermIndex == term_id).Count() > 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, new ArgumentException("Comment Header already Exists.")))); } CommentHeader headerParagraph = new CommentHeader() { id = db.CommentHeaders.OrderBy(h => h.id).ToList().Last().id + 1, SectionIndex = section_id, TermIndex = term_id }; String content = await Request.Content.ReadAsStringAsync(); content = CommentLetter.ConvertFromBase64String(content); headerParagraph.HTML = CommentLetter.CleanTags(content); db.CommentHeaders.Add(headerParagraph); try { db.SaveChanges(); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Conflict, e))); } return(ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, new CommentHeaderInfo(headerParagraph.id)))); } }
protected void SaveBtn_Click(object sender, EventArgs e) { if (SelectedSectionId == -1) { State.log.WriteLine("{0} {1}: Cannot Save with no section selected.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); LogWarning("Cannot save with no section selected."); return; } if (Saving) { State.log.WriteLine("{1} {0}: Aborted multiple Saving attempts.", DateTime.Now.ToLongTimeString(), DateTime.Today.ToShortDateString()); LogWarning("Aborted multiple Saving Attempts."); return; } using (WebhostEntities db = new WebhostEntities()) { Saving = true; if (HeaderPanel.Visible) { CommentHeader header = db.CommentHeaders.Where(h => h.id == LoadedHeaderId).Single(); State.log.WriteLine("{0} {1}: Saving Header Paragraph.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); // Save the Header! if (HeaderEditor.Content.Length < header.HTML.Length) { State.log.WriteLine("{0} {1}: Warning--suspicious overwrite:{2}Original:{2}{3}{2}Replacement:{2}{4}", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString(), Environment.NewLine, header.HTML, HeaderEditor.Content); WebhostEventLog.CommentLog.LogWarning("Suspicious Overwrite:{0}Original:{0}{1}{0}========================================================{0}Replacement:{0}{2}", Environment.NewLine, header.HTML, HeaderEditor.Content); } String html = HeaderEditor.Content; String temp = html; html = CommentLetter.CleanTags(html); if (!temp.Equals(html)) { WebhostEventLog.CommentLog.LogWarning("CleanTags method changed:{0}{1}{0}============================================================={0}Into:{0}{2}", Environment.NewLine, temp, html); } WebhostEventLog.CommentLog.LogInformation("Saving Header:{0}{1}", Environment.NewLine, html); HeaderEditor.Content = html; header.HTML = HeaderEditor.Content; HeaderHTML = HeaderEditor.Content; } if (LoadedCommentId != -1) { State.log.WriteLine("{0} {1}: Saving Student Comment.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); StudentComment comment = db.StudentComments.Where(com => com.id == LoadedCommentId).Single(); if (StudentEditor.Content.Length < comment.HTML.Length) { State.log.WriteLine("{0} {1}: Warning--suspicious overwrite:{2}Original:{2}{3}{2}Replacement:{2}{4}", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString(), Environment.NewLine, comment.HTML, StudentEditor.Content); WebhostEventLog.CommentLog.LogWarning("Suspicious Overwrite:{0}Original:{0}{1}{0}========================================================{0}Replacement:{0}{2}", Environment.NewLine, comment.HTML, StudentEditor.Content); } String html = StudentEditor.Content; String temp = html; html = CommentLetter.CleanTags(html); if (!temp.Equals(html)) { WebhostEventLog.CommentLog.LogWarning("CleanTags method changed:{0}{1}{0}============================================================={0}Into:{0}{2}", Environment.NewLine, temp, html); } WebhostEventLog.CommentLog.LogInformation("Saving Comment for {2} {3}:{0}{1}", Environment.NewLine, html, comment.Student.FirstName, comment.Student.LastName); StudentEditor.Content = html; comment.HTML = StudentEditor.Content; comment.EffortGradeID = CommentGrades.EffortGradeID; comment.FinalGradeID = CommentGrades.FinalGradeID; comment.TermGradeID = CommentGrades.TrimesterGradeID; comment.ExamGradeID = CommentGrades.ExamGradeID; StudentCommentHTML = StudentEditor.Content; } db.SaveChanges(); State.log.WriteLine("{0} {1}: Changes saved to the Database.", DateTime.Today.ToShortDateString(), DateTime.Now.ToLongTimeString()); WebhostEventLog.CommentLog.LogInformation("Save Complete."); } Saving = false; }