public ActionResult <Student> CreateStudent([FromBody] StudentAddDto studentDto) { if (!ModelState.IsValid) { var errorMessages = new List <KeyValuePair <string, string> >(); foreach (var key in ModelState.Keys) { errorMessages.AddRange(ModelState[key].Errors .Select(error => new KeyValuePair <string, string>(key, error.ErrorMessage))); } return(BadRequest(GeneralResponse <Student> .ValidationError(errorMessages))); } var studentEntity = new Student { Name = studentDto.Name, StudentNo = studentDto.StudentNo, Age = studentDto.Age, PhoneNumber = studentDto.PhoneNumber }; var studentAfterAdd = _studentService.AddStudent(studentEntity); return(Created(new Uri($"{Request.Path}/{studentAfterAdd.Id}", UriKind.Relative), GeneralResponse <Student> .Ok(studentAfterAdd))); }
public ActionResult <Student> GetStudent([FromRoute] int studentId) { var student = _studentService.QueryStudentById(studentId); if (student == null) { return(NotFound()); } return(Ok(GeneralResponse <Student> .Ok(student))); }
public ActionResult <List <Student> > GetStudentList([FromQuery] StudentQueryDto queryDto) { var students = _studentService.QueryStudents(queryDto).ToList(); return(Ok(GeneralResponse <List <Student> > .Ok(students))); }