public long Post([FromBody] Models.IncidentReport report) { if (report.userId == 0) { SessionController session = new SessionController(); var user = session.Get(); report.userId = user.userId; report.createdStation = user.stationInfo; report.currentUser = user.userId; session.Dispose(); } if (report.incidentId == 0) { // CREATE REPORT this._db.IncidentReports.Add(report); } else { this._db.IncidentReports.Attach(report); this._db.Entry(report).State = System.Data.Entity.EntityState.Modified; } try { this._db.SaveChanges(); // WRITE TO REPORT LOG Models.ReportLog log = new Models.ReportLog(); log.incidentId = report.incidentId; log.userId = report.userId; log.userStation = report.createdStation; log.logDateTime = DateTime.Now; log.logDetails = "Report created."; LogController logController = new LogController(); logController.Post(log); logController.Dispose(); // NOTIFY MailController mailer = new MailController(); StringBuilder messageBody = new StringBuilder(); messageBody.Append("<p>A new incident report for <b>" + report.clientName + "</b> has been created by " + report.staffName + ".</p>"); messageBody.Append("<p><a href=\"http://cfs-incidents/report/residential/" + report.incidentId.ToString() + "\">Click here to view the report.</a></p>"); if (report.incidentReportTypeId == 1) { mailer.SendMail( new List <string>() { "*****@*****.**" }, "*****@*****.**", "New Incident Report", System.Net.Mail.MailPriority.High, messageBody ); } else { mailer.SendMail( new List <string>() { "*****@*****.**" }, "*****@*****.**", "New Incident Report", System.Net.Mail.MailPriority.High, messageBody ); } mailer.Dispose(); return(report.incidentId); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); MailController mailer = new MailController(); mailer.SendMail( new List <string>() { "*****@*****.**" }, "*****@*****.**", "ERROR CREATING INCIDENT: VALIDATION", System.Net.Mail.MailPriority.High, exceptionMessage ); string currentUser = RequestContext.Principal.Identity.Name; mailer.SendExceptionDetail("post:/api/reports", exceptionMessage, ex.StackTrace, currentUser, report); // Throw a new DbEntityValidationException with the improved exception message. throw new System.Data.Entity.Validation.DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } catch (Exception ex) { string errorMessage = ex.Message; if (ex.InnerException != null) { errorMessage += " Inner Exception: " + ex.InnerException; } MailController mailer = new MailController(); mailer.SendMail( new List <string>() { "*****@*****.**" }, "*****@*****.**", "ERROR CREATING INCIDENT", System.Net.Mail.MailPriority.High, errorMessage ); string currentUser = RequestContext.Principal.Identity.Name; mailer.SendExceptionDetail("post:/api/reports", errorMessage, ex.StackTrace, currentUser, report); throw new Exception(errorMessage); } }
public long Post([FromBody] Models.Medical medical) { if (medical.incidentMedicalId == 0) { this._db.Medicals.Add(medical); } else { this._db.Medicals.Attach(medical); this._db.Entry(medical).State = System.Data.Entity.EntityState.Modified; } try { this._db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); MailController mailer = new MailController(); mailer.SendMail( new List <string>() { "*****@*****.**" }, "*****@*****.**", "ERROR CREATING MEDICAL ASSESSMENT: VALIDATION", System.Net.Mail.MailPriority.High, exceptionMessage ); string currentUser = RequestContext.Principal.Identity.Name; mailer.SendExceptionDetail("post:/api/medicals", exceptionMessage, ex.StackTrace, currentUser, medical); // Throw a new DbEntityValidationException with the improved exception message. throw new System.Data.Entity.Validation.DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } catch (Exception ex) { string errorMessage = ex.Message; if (ex.InnerException != null) { errorMessage += " Inner Exception: " + ex.InnerException; } MailController mailer = new MailController(); mailer.SendMail( new List <string>() { "*****@*****.**" }, "*****@*****.**", "ERROR CREATING MEDICAL ASSESSMENT", System.Net.Mail.MailPriority.High, errorMessage ); string currentUser = RequestContext.Principal.Identity.Name; mailer.SendExceptionDetail("post:/api/medicals", errorMessage, ex.StackTrace, currentUser, medical); throw new Exception(errorMessage); } return(medical.incidentMedicalId); }