public MarksDto Add_Mark(MarksDto marks) { MapperDto_Class.ConfigAutoMapper(); if (this.IsValidmark(marks)) { var NewMARK = repository.Insert(Mapper.Map <Marks>(marks)); return(Mapper.Map <MarksDto>(NewMARK)); } else { throw new ArgumentNullException("Provided information is not valid."); } }
public IActionResult Add(int studentId, int courseId, [FromBody] MarksModel model) { var student = studentRepository.GetById(studentId); if (student == null) { return(NotFound("Student does not exist.")); } var course = courseRepository.GetById(model.CourseCode); if (course == null) { return(NotFound("Course does not exist.")); } var existingMark = marksRepository.Find(m => m.StudentId == model.StudentId && m.CourseCode == model.CourseCode).FirstOrDefault(); if (existingMark != null) { return(BadRequest("Student already has that mark entered.")); } if (model.MarkValue < 6 || model.MarkValue > 10) { return(BadRequest("Invalid mark. Allowed values: 6 - 10")); } var mark = new Marks { StudentId = model.StudentId, CourseCode = model.CourseCode, MarkValue = model.MarkValue }; marksRepository.Insert(mark); marksRepository.Save(); return(CreatedAtRoute("GetMark", new { studentId = mark.StudentId, courseId = mark.CourseCode }, mark)); }