public ActionResult Create(IncidentAdd newItem) { /* * if (!ModelState.IsValid) * { * return RedirectToAction("Create", AutoMapper.Mapper.Map<IncidentAddForm>(newItem)); * }*/ if (newItem.DocUpload != null) { if (newItem.DocUpload.ContentType != "application/pdf") { IncidentAddForm form = new IncidentAddForm(); form.StudentId = new List <string>(); form.StudentName = new List <string>(); form.campus = newItem.campus; form.coursecode = newItem.coursecode; form.description = newItem.description; form.IncidentDate = newItem.IncidentDate; form.InstructorName = newItem.InstructorName; form.isMinor = newItem.isMinor; form.program = newItem.program; form.StudentName = newItem.StudentName; form.StudentId = newItem.StudentId; return(View(form)); } } // Process the input var addedItem = m.IncidentAdd(newItem); if (addedItem == null) { return(RedirectToAction("Create", AutoMapper.Mapper.Map <IncidentAddForm>(newItem))); } else { return(RedirectToAction("Details", new { id = addedItem.Id })); } }
public IncidentWithDetails IncidentAdd(IncidentAdd newItem) { ICollection <Student> mystudent = new List <Student>(); foreach (var x in newItem.StudentId) { if (x != "") { mystudent.Add(ds.Students.Include("Incidents").SingleOrDefault(s => s.studentId == x)); } } //= ds.Students.SingleOrDefault(s => s.studentId == newItem.StudentId); var instruct = ds.Instructors.SingleOrDefault(e => e.name == newItem.InstructorName); var myCourse = ds.Courses.SingleOrDefault(c => c.courseCode == newItem.coursecode); //var addedItem = ds.Incidents.Add(Mapper.Map<Incident>(newItem)); var first = mystudent.First(); // var streamLength = newItem.DocUpload.InputStream.Length; // var fileBytes = new byte[streamLength]; // newItem.DocUpload.InputStream.Read(fileBytes, 0, fileBytes.Length); //one of them wasn't found from Database for (int i = 0; i < mystudent.Count(); i++) { if (mystudent.ElementAt(i).name != newItem.StudentName.ElementAt(i)) { return(null); } } if (mystudent == null || instruct == null || myCourse == null) { return(null); } //student name + number dont match what is on Database else { //create incident Incident incident = new Incident(); incident.dateReported = newItem.IncidentDate; incident.description = newItem.description; incident.Instructor = instruct; foreach (var x in mystudent) { incident.Students.Add(x); } // incident.Doc = docBytes; // incident.DocContentType = newItem.DocUpload.ContentType; incident.status = "open"; incident.campus = newItem.campus; incident.program = newItem.program; if (newItem.isMinor) { bool checkall = false; foreach (var a in mystudent) { if (a.Incidents.Count() > 0) { checkall = true; } } if (!checkall) { incident.offence = "Minor"; } else { incident.offence = "Minor (Repeat)"; } } else { incident.offence = "Major"; } byte[] docBytes = null; if (newItem.DocUpload != null) { docBytes = new byte[newItem.DocUpload.ContentLength]; newItem.DocUpload.InputStream.Read(docBytes, 0, newItem.DocUpload.ContentLength); // Then, configure the new object's properties incident.Doc = docBytes; incident.DocContentType = newItem.DocUpload.ContentType; } ds.Incidents.Add(incident); ds.SaveChanges(); //After saving in DB send email SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587); smtpClient.EnableSsl = true; MailMessage msg = new MailMessage(); string body; foreach (var x in mystudent) { msg.To.Clear(); msg.To.Add(x.emailAddress); msg.Subject = "Seneca Academic Honesty Notice"; body = "Hello " + x.name; body += "\n\n"; body += "\tYour Seneca Academic Honesty record has been updated. Please log into the site to check it."; msg.Body = body; smtpClient.Send(msg); } msg = new MailMessage(); msg.To.Add(first.emailAddress); msg.Subject = "Seneca Academic Honesty Notice"; body = "Hello " + instruct.name; body += "\n\n"; body += "\tYour Seneca Academic Honesty Report has been successfully recorded to our database."; msg.Body = body; smtpClient.Send(msg); msg = new MailMessage(); msg.To.Add(first.emailAddress); msg.Subject = "Seneca Academic Honesty Notice"; body = "Hello Academic Honesty Chair of " + incident.campus; body += "\n\n"; body += "\tAn Academic Honesty report has recently been submitted. Please login to the site to check it."; msg.Body = body; smtpClient.Send(msg); return(Mapper.Map <IncidentWithDetails>(incident)); } }