public async Task <IActionResult> PostGrade([FromBody] GradeRecived grade) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Student student = _context.Student.Where(s => s.Username.Equals(grade.username)).FirstOrDefault(); GradesToDiscipline gtd = student.Grades.Where(g => g.Discipline.Nume.Equals(grade.materie)).FirstOrDefault(); GradeType gt = GradeType.EXAMEN; if (grade.tipNota == "Examen final") { gt = GradeType.EXAMEN; } else if (grade.tipNota == "Laborator") { gt = GradeType.LAB; } if (grade.data == "") { grade.data = DateTime.Now.ToString("dd/MM/yyyy"); } if (grade.idNota == "") { Grade newGrade = new Grade() { GradeValue = double.Parse(grade.grade), DataNotei = grade.data, Type = gt }; if (gt == GradeType.EXAMEN) { if (gtd.Grades.Where(g => g.Type == GradeType.EXAMEN).FirstOrDefault() == null) { gtd.Grades.Add(newGrade); } } } else { Grade recievedGrade = gtd.Grades.Where(g => g.Id == int.Parse(grade.idNota)).FirstOrDefault(); recievedGrade.GradeValue = double.Parse(grade.grade); recievedGrade.DataNotei = grade.data; } await _context.SaveChangesAsync(); if (gt == GradeType.EXAMEN) { List <Student> students = new List <Student>(); students.Add(_context.Student.Where(s => s.Username.Equals(grade.username)).FirstOrDefault()); Service service = new Service(_context); service.SendMailToStudents(students, "Exam grade at " + grade.materie + " published", String.Format("Hello!\n\nYour teacher posted youe exam grade: {0}.\nWe hope you did good and we wish you the best of luck :)", grade.grade)); } return(Ok()); }
public IActionResult SetInnerandOutterPercentagesForGrades([FromBody] PercentageJSON body) { // Get all the students that are enrolled to the given discipline List <Student> students = _context.Student.Where( s => s.Grades.Where(gtd => gtd.Discipline.Nume.Equals(body.Materie)).FirstOrDefault() != null).ToList(); // For each student foreach (var s in students) { // Get the student's GradeToDiscipline for the respective discipline GradesToDiscipline gradeToDiscipline = s.Grades.Where(gtd => gtd.Discipline.Nume.Equals(body.Materie)).FirstOrDefault(); // For each grade foreach (var g in gradeToDiscipline.Grades) { // Update the inner and outter fields acordingly switch (g.Type.ToString()) { case "EXAMEN": if (body.Examen != "") { g.ProcentOuter = double.Parse(body.Examen); g.ProcentInnerType = 100; } break; case "SEMINAR": if (body.Seminar != "") { g.ProcentOuter = double.Parse(body.Seminar); g.ProcentInnerType = 100; } break; case "PARTIAL": if (body.Partial != "") { g.ProcentOuter = double.Parse(body.Partial); g.ProcentInnerType = 100; } break; case "BONUS": if (body.Bonus != "") { g.ProcentOuter = double.Parse(body.Bonus); g.ProcentInnerType = 100; } break; case "LAB": break; case "FINAL": g.ProcentOuter = 100; g.ProcentInnerType = 100; break; } } // Check if we need to update the lab grades if (body.Laborator.Keys.Count != 0) { // Now we update the lab grades by first selecting all the the lab grades of our current student List <Grade> labGrades = gradeToDiscipline.Grades.Where( g => g.Type.ToString().Equals("LAB") // and sort the list of grades after the grade ID ).ToList().OrderBy(g => g.Id).ToList(); // Check if the number of lab grades is the same as the one in the DB if (labGrades.Count != body.Laborator["Inner"].Count) { Dictionary <string, string> err = new Dictionary <string, string>(); err.Add("error", String.Format("The number of lab grades sent by the teacher is not the same as the number of lab grades for the student {0}", s.Username)); return(BadRequest(err)); } // For each lab grade update the inner and outter fields acordingly for (int i = 0; i < labGrades.Count; i++) { labGrades[i].ProcentOuter = double.Parse(body.Laborator["Outer"][0]); labGrades[i].ProcentInnerType = double.Parse(body.Laborator["Inner"][i]); } } } // Save changes into database _context.SaveChanges(); Dictionary <string, string> success = new Dictionary <string, string>(); success.Add("success", "The percentages for the provided grade types were successfully updated!"); return(Json(success)); }