CourseController.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package nju.seec.helper.controller;
  2. import nju.seec.helper.aspect.auth.Auth;
  3. import nju.seec.helper.aspect.auth.LoginUser;
  4. import nju.seec.helper.controller.response.PageResponse;
  5. import nju.seec.helper.dto.course.ChooseDTO;
  6. import nju.seec.helper.dto.course.CourseDTO;
  7. import nju.seec.helper.dto.groups.Create;
  8. import nju.seec.helper.dto.groups.Modify;
  9. import nju.seec.helper.enums.UserType;
  10. import nju.seec.helper.service.CourseService;
  11. import nju.seec.helper.vo.CourseVO;
  12. import nju.seec.helper.vo.UserVO;
  13. import org.springframework.data.domain.Pageable;
  14. import org.springframework.data.web.PageableDefault;
  15. import org.springframework.validation.annotation.Validated;
  16. import org.springframework.web.bind.annotation.*;
  17. /**
  18. * 课程
  19. *
  20. * @author cst
  21. */
  22. @RestController
  23. @RequestMapping("/api/course")
  24. public class CourseController {
  25. private final CourseService courseService;
  26. public CourseController(CourseService courseService) {
  27. this.courseService = courseService;
  28. }
  29. /**
  30. * 教师创建课程
  31. */
  32. @Auth(roles = UserType.TEACHER, message = "创建课程")
  33. @PostMapping
  34. public CourseVO createCourse(LoginUser user,
  35. @Validated(Create.class) @RequestBody CourseDTO courseDTO) {
  36. return courseService.createCourse(user, courseDTO);
  37. }
  38. /**
  39. * 教师删除课程(伪删除)
  40. */
  41. @Auth(roles = UserType.TEACHER, message = "删除课程")
  42. @DeleteMapping("/{courseId}")
  43. public void deleteCourse(LoginUser user,
  44. @PathVariable Long courseId) {
  45. courseService.deleteCourse(user, courseId);
  46. }
  47. /**
  48. * 教师修改课程
  49. */
  50. @Auth(roles = UserType.TEACHER, message = "修改课程")
  51. @PutMapping("/{courseId}")
  52. public CourseVO modifyCourse(LoginUser user,
  53. @PathVariable Long courseId,
  54. @Validated(Modify.class) @RequestBody CourseDTO courseDTO) {
  55. return courseService.modifyCourse(user, courseId, courseDTO);
  56. }
  57. /**
  58. * 学生选课
  59. */
  60. @Auth(roles = UserType.STUDENT, message = "选课")
  61. @PostMapping("/{courseId}/choose")
  62. public void chooseCourse(LoginUser user,
  63. @PathVariable Long courseId,
  64. @Validated @RequestBody ChooseDTO chooseDTO) {
  65. courseService.chooseCourse(user, courseId, chooseDTO);
  66. }
  67. /**
  68. * 学生退课
  69. */
  70. @Auth(roles = UserType.STUDENT, message = "退课")
  71. @PostMapping("/{courseId}/quit")
  72. public void quitCourse(LoginUser user,
  73. @PathVariable Long courseId) {
  74. courseService.quitCourse(user, courseId);
  75. }
  76. /**
  77. * 获取课程列表(无需登录)
  78. */
  79. @GetMapping
  80. public PageResponse<CourseVO> getCourses(@RequestParam(required = false, defaultValue = "") String key,
  81. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  82. return PageResponse.of(courseService.getCourses(key, pageable));
  83. }
  84. /**
  85. * 获取创建课程列表
  86. */
  87. @Auth(roles = UserType.TEACHER, message = "获取创建课程列表")
  88. @GetMapping("/created")
  89. public PageResponse<CourseVO> getCreatedCourses(LoginUser user,
  90. @RequestParam(required = false, defaultValue = "") String key,
  91. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  92. return PageResponse.of(courseService.getCreatedCourses(user, key, pageable));
  93. }
  94. /**
  95. * 获取选课列表
  96. */
  97. @Auth(roles = UserType.STUDENT, message = "获取选课列表")
  98. @GetMapping("/chosen")
  99. public PageResponse<CourseVO> getChosenCourses(LoginUser user,
  100. @RequestParam(required = false, defaultValue = "") String key,
  101. @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
  102. return PageResponse.of(courseService.getChosenCourses(user, key, pageable));
  103. }
  104. /**
  105. * 获取课程选课码
  106. */
  107. @Auth(roles = UserType.TEACHER, message = "获取选课码")
  108. @GetMapping("/{courseId}/code")
  109. public String getCourseCode(LoginUser user,
  110. @PathVariable Long courseId) {
  111. return courseService.getCourseCode(user, courseId);
  112. }
  113. /**
  114. * 获取某课程
  115. */
  116. @GetMapping("/{courseId}")
  117. public CourseVO getOneCourse(@PathVariable Long courseId) {
  118. return courseService.getOneCourse(courseId);
  119. }
  120. /**
  121. * 获取选课名单
  122. */
  123. @Auth(roles = UserType.TEACHER, message = "获取选课名单")
  124. @GetMapping("/{courseId}/students")
  125. public PageResponse<UserVO> getChooseStudents(LoginUser user,
  126. @PathVariable Long courseId,
  127. @RequestParam(required = false, defaultValue = "") String key,
  128. @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
  129. return PageResponse.of(courseService.getChooseStudents(user, courseId, key, pageable));
  130. }
  131. }