private bool IsKeyValid(TeachingAssignmentModel model)
        {
            if (!_exist.TeacherExists(model.TeacherID))
            {
                _resp.code = 404;
                _resp.messages.Add("Teacher not found");
                return(false);
            }

            if (!_exist.ClassExists(model.ClassID))
            {
                _resp.code = 404;
                _resp.messages.Add("Class not found");
                return(false);
            }

            if (!_exist.SubjectExists(model.SubjectID))
            {
                _resp.code = 404;
                _resp.messages.Add("Subject not found");
                return(false);
            }

            return(true);
        }
        public IActionResult EditClass(int classId, ClassModel model)
        {
            var oldClass = _classService.GetClass(classId);

            // if no class is found
            if (oldClass == null)
            {
                return(NotFound());
            }

            // check if foreign key(s), unique key(s) are invalid
            if (!IsEditKeysValid(model))
            {
                return(StatusCode(resp.code, resp));
            }

            // check if model matches with data annotation in front-end model
            if (ModelState.IsValid)
            {
                var newClass = new Class(oldClass);

                newClass.Name          = model.Name;
                newClass.Year          = model.Year;
                newClass.GradeID       = model.GradeID;
                newClass.HeadTeacherID = model.HeadTeacherID;

                // check for (Name, Year)
                if (newClass.Name != oldClass.Name || newClass.Year != oldClass.Year)
                {
                    if (exist.ClassExists(newClass.Name, newClass.Year))
                    {
                        resp.code = 400; // bad request
                        resp.messages.Add(new { Name = "Already have class " + model.Name + " in " + model.Year });
                        return(StatusCode(resp.code, resp));
                    }
                }

                // check if Head Teacher is not belong to another class
                if (newClass.HeadTeacherID != oldClass.HeadTeacherID && newClass.HeadTeacherID != null)
                {
                    // check if Head Teacher is not belong to another class
                    if (exist.HeadTeacherExists((int)model.HeadTeacherID))
                    {
                        resp.code = 400; //Bad Request
                        resp.messages.Add(new { HeadTeacherID = "Head teacher is belong to another class" });
                        return(StatusCode(resp.code, resp));
                    }
                }

                // bind value(s)
                oldClass.Name          = newClass.Name;
                oldClass.Year          = newClass.Year;
                oldClass.GradeID       = newClass.GradeID;
                oldClass.HeadTeacherID = newClass.HeadTeacherID;

                _classService.Update();

                return(Ok(oldClass));
            }
            else
            {
                var errors = new List <string>();
                foreach (var state in ModelState)
                {
                    foreach (var error in state.Value.Errors)
                    {
                        errors.Add(error.ErrorMessage);
                    }
                }
                return(BadRequest(errors));
            }
        }