Example #1
0
        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));
        }