Przeglądaj źródła

feat: 增加管理员角色,增加用户状态管理功能

ChenSiTong 6 lat temu
rodzic
commit
95239f2a87
46 zmienionych plików z 433 dodań i 163 usunięć
  1. 1 1
      src/main/java/nju/seec/helper/aspect/auth/AuthAspect.java
  2. 0 1
      src/main/java/nju/seec/helper/aspect/auth/LoginUser.java
  3. 71 0
      src/main/java/nju/seec/helper/controller/AdminController.java
  4. 2 2
      src/main/java/nju/seec/helper/controller/CommentController.java
  5. 1 0
      src/main/java/nju/seec/helper/controller/ControllerAdvice.java
  6. 21 10
      src/main/java/nju/seec/helper/controller/CourseController.java
  7. 2 2
      src/main/java/nju/seec/helper/controller/CourseFileController.java
  8. 2 2
      src/main/java/nju/seec/helper/controller/MessageController.java
  9. 2 2
      src/main/java/nju/seec/helper/controller/NoticeController.java
  10. 3 3
      src/main/java/nju/seec/helper/controller/QuestionController.java
  11. 3 3
      src/main/java/nju/seec/helper/controller/QuizController.java
  12. 2 2
      src/main/java/nju/seec/helper/controller/ReplyController.java
  13. 2 2
      src/main/java/nju/seec/helper/controller/SlideController.java
  14. 3 5
      src/main/java/nju/seec/helper/controller/UserController.java
  15. 22 0
      src/main/java/nju/seec/helper/dao/AdminDAO.java
  16. 2 1
      src/main/java/nju/seec/helper/dao/UserDAO.java
  17. 18 0
      src/main/java/nju/seec/helper/dto/common/LoginDTO.java
  18. 23 0
      src/main/java/nju/seec/helper/entity/Admin.java
  19. 1 1
      src/main/java/nju/seec/helper/enums/UserType.java
  20. 40 0
      src/main/java/nju/seec/helper/service/AdminService.java
  21. 1 1
      src/main/java/nju/seec/helper/service/CommentService.java
  22. 1 1
      src/main/java/nju/seec/helper/service/CourseFileService.java
  23. 13 3
      src/main/java/nju/seec/helper/service/CourseService.java
  24. 1 1
      src/main/java/nju/seec/helper/service/MessageService.java
  25. 1 1
      src/main/java/nju/seec/helper/service/NoticeService.java
  26. 1 1
      src/main/java/nju/seec/helper/service/QuestionService.java
  27. 1 1
      src/main/java/nju/seec/helper/service/QuizService.java
  28. 1 1
      src/main/java/nju/seec/helper/service/QuizStudentAnswerService.java
  29. 1 1
      src/main/java/nju/seec/helper/service/ReplyService.java
  30. 1 1
      src/main/java/nju/seec/helper/service/SlideService.java
  31. 86 0
      src/main/java/nju/seec/helper/service/impl/AdminServiceImpl.java
  32. 7 7
      src/main/java/nju/seec/helper/service/impl/CommentServiceImpl.java
  33. 5 7
      src/main/java/nju/seec/helper/service/impl/CourseFileServiceImpl.java
  34. 15 29
      src/main/java/nju/seec/helper/service/impl/CourseServiceImpl.java
  35. 2 2
      src/main/java/nju/seec/helper/service/impl/MessageServiceImpl.java
  36. 9 11
      src/main/java/nju/seec/helper/service/impl/NoticeServiceImpl.java
  37. 6 8
      src/main/java/nju/seec/helper/service/impl/QuestionServiceImpl.java
  38. 7 11
      src/main/java/nju/seec/helper/service/impl/QuizServiceImpl.java
  39. 4 5
      src/main/java/nju/seec/helper/service/impl/QuizStudentAnswerServiceImpl.java
  40. 4 4
      src/main/java/nju/seec/helper/service/impl/ReplyServiceImpl.java
  41. 5 5
      src/main/java/nju/seec/helper/service/impl/SlideServiceImpl.java
  42. 2 7
      src/main/java/nju/seec/helper/service/impl/UserServiceImpl.java
  43. 11 14
      src/main/java/nju/seec/helper/util/BokUtil.java
  44. 19 0
      src/main/java/nju/seec/helper/vo/AdminVO.java
  45. 4 2
      src/main/resources/application-deploy.yml
  46. 4 2
      src/main/resources/application-dev.yml

+ 1 - 1
src/main/java/nju/seec/helper/aspect/auth/AuthAspect.java

@@ -1,8 +1,8 @@
 package nju.seec.helper.aspect.auth;
 
-import nju.seec.helper.util.Consts;
 import nju.seec.helper.enums.ExceptionType;
 import nju.seec.helper.exception.HelperException;
+import nju.seec.helper.util.Consts;
 import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;

+ 0 - 1
src/main/java/nju/seec/helper/aspect/auth/LoginUser.java

@@ -11,6 +11,5 @@ import nju.seec.helper.enums.UserType;
 @Accessors(chain = true)
 public class LoginUser {
     private Long id;
-    private String name;
     private UserType type;
 }

+ 71 - 0
src/main/java/nju/seec/helper/controller/AdminController.java

@@ -0,0 +1,71 @@
+package nju.seec.helper.controller;
+
+import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.controller.response.PageResponse;
+import nju.seec.helper.dto.common.LoginDTO;
+import nju.seec.helper.enums.UserState;
+import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.AdminService;
+import nju.seec.helper.util.Consts;
+import nju.seec.helper.vo.AdminVO;
+import nju.seec.helper.vo.UserVO;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.web.PageableDefault;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.*;
+
+import javax.servlet.http.HttpSession;
+
+/**
+ * @author cst
+ */
+@RestController
+@RequestMapping("/api/admin")
+public class AdminController {
+    private final AdminService adminService;
+
+    public AdminController(AdminService adminService) {
+        this.adminService = adminService;
+    }
+
+    @PostMapping("/login")
+    public AdminVO login(@Validated @RequestBody LoginDTO loginDTO, HttpSession session) {
+        AdminVO adminVO = adminService.getAdminByUsernameAndPassword(loginDTO.getUsername(), loginDTO.getPassword());
+
+        LoginUser user = new LoginUser()
+                .setId(adminVO.getId())
+                .setType(UserType.ADMIN);
+
+        session.setAttribute(Consts.SESSION_USER_NAME, user);
+        // 30分钟有效期
+        session.setMaxInactiveInterval(30 * 60);
+        return adminVO;
+    }
+
+    @Auth(roles = UserType.ADMIN, message = "获取注册教师列表")
+    @GetMapping("/user")
+    public PageResponse<UserVO> getUsers(LoginUser admin, UserType type, UserState state,
+                                         @RequestParam(required = false, defaultValue = "") String key,
+                                         @PageableDefault(size = Integer.MAX_VALUE) Pageable pageable) {
+        return PageResponse.of(adminService.getUsers(type, state, key, pageable));
+    }
+
+    @Auth(roles = UserType.ADMIN, message = "审核通过用户")
+    @PutMapping("/user/{userId}/pass")
+    public void passUser(LoginUser admin, @PathVariable Long userId) {
+        adminService.passUser(admin, userId);
+    }
+
+    @Auth(roles = UserType.ADMIN, message = "封禁用户")
+    @PutMapping("/user/{userId}/lock")
+    public void lockUser(LoginUser admin, @PathVariable Long userId) {
+        adminService.lockUser(admin, userId);
+    }
+
+    @Auth(roles = UserType.ADMIN, message = "解封用户")
+    @PutMapping("/user/{userId}/unlock")
+    public void unlockUser(LoginUser admin, @PathVariable Long userId) {
+        adminService.unlockUser(admin, userId);
+    }
+}

+ 2 - 2
src/main/java/nju/seec/helper/controller/CommentController.java

@@ -1,12 +1,12 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.comment.CommentDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.enums.UserType;
 import nju.seec.helper.service.CommentService;
 import nju.seec.helper.util.JsonUtils;
-import nju.seec.helper.enums.UserType;
 import nju.seec.helper.vo.CommentVO;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;

+ 1 - 0
src/main/java/nju/seec/helper/controller/ControllerAdvice.java

@@ -28,6 +28,7 @@ public class ControllerAdvice {
 
     @ExceptionHandler(MethodArgumentTypeMismatchException.class)
     public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
+        log.error(e.getParameter().getParameterName(), e);
         return ResponseEntity.badRequest().body(ErrorResponse.of("路径参数错误"));
     }
 

+ 21 - 10
src/main/java/nju/seec/helper/controller/CourseController.java

@@ -1,14 +1,14 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.course.ChooseDTO;
 import nju.seec.helper.dto.course.CourseDTO;
 import nju.seec.helper.dto.groups.Create;
 import nju.seec.helper.dto.groups.Modify;
-import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.CourseService;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.CourseService;
 import nju.seec.helper.vo.CourseVO;
 import nju.seec.helper.vo.UserVO;
 import org.springframework.data.domain.Pageable;
@@ -85,21 +85,32 @@ public class CourseController {
     /**
      * 获取课程列表(无需登录)
      */
-    @GetMapping("/all")
+    @GetMapping
     public PageResponse<CourseVO> getCourses(@RequestParam(required = false, defaultValue = "") String key,
                                              @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
         return PageResponse.of(courseService.getCourses(key, pageable));
     }
 
     /**
-     * 获取课程列表
+     * 获取创建课程列表
      */
-    @Auth(roles = {UserType.TEACHER, UserType.STUDENT}, message = "获取课程列表")
-    @GetMapping
-    public PageResponse<CourseVO> getCourses(LoginUser user,
-                                             @RequestParam(required = false, defaultValue = "") String key,
-                                             @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
-        return PageResponse.of(courseService.getCourses(user, key, pageable));
+    @Auth(roles = UserType.TEACHER, message = "获取创建课程列表")
+    @GetMapping("/created")
+    public PageResponse<CourseVO> getCreatedCourses(LoginUser user,
+                                                    @RequestParam(required = false, defaultValue = "") String key,
+                                                    @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
+        return PageResponse.of(courseService.getCreatedCourses(user, key, pageable));
+    }
+
+    /**
+     * 获取选课列表
+     */
+    @Auth(roles = UserType.STUDENT, message = "获取选课列表")
+    @GetMapping("/chosen")
+    public PageResponse<CourseVO> getChosenCourses(LoginUser user,
+                                                   @RequestParam(required = false, defaultValue = "") String key,
+                                                   @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
+        return PageResponse.of(courseService.getChosenCourses(user, key, pageable));
     }
 
     /**

+ 2 - 2
src/main/java/nju/seec/helper/controller/CourseFileController.java

@@ -1,11 +1,11 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.course.CourseFileDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.CourseFileService;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.CourseFileService;
 import nju.seec.helper.vo.CourseFileVO;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;

+ 2 - 2
src/main/java/nju/seec/helper/controller/MessageController.java

@@ -1,12 +1,12 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.message.MessageDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Message;
-import nju.seec.helper.service.MessageService;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.MessageService;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
 import org.springframework.validation.annotation.Validated;

+ 2 - 2
src/main/java/nju/seec/helper/controller/NoticeController.java

@@ -1,13 +1,13 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.groups.Create;
 import nju.seec.helper.dto.groups.Modify;
 import nju.seec.helper.dto.notice.NoticeDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.NoticeService;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.NoticeService;
 import nju.seec.helper.vo.NoticeVO;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;

+ 3 - 3
src/main/java/nju/seec/helper/controller/QuestionController.java

@@ -1,13 +1,13 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.groups.Create;
 import nju.seec.helper.dto.groups.Modify;
 import nju.seec.helper.dto.question.BaseQuestionDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.QuestionService;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.QuestionService;
 import nju.seec.helper.vo.question.BaseQuestionVO;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;
@@ -85,7 +85,7 @@ public class QuestionController {
 
     @Auth(roles = UserType.TEACHER)
     @GetMapping("/created/ids")
-    public Set<String> getIdsOfCreatedQuestions(LoginUser user) {
+    public Set<String> getCreatedQuestionIds(LoginUser user) {
         return questionService.getIdsOfCreatedQuestions(user);
     }
 }

+ 3 - 3
src/main/java/nju/seec/helper/controller/QuizController.java

@@ -1,16 +1,16 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.groups.Create;
 import nju.seec.helper.dto.groups.Modify;
 import nju.seec.helper.dto.quiz.QuizDTO;
 import nju.seec.helper.dto.quiz.QuizStudentAnswerDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.QuizService;
-import nju.seec.helper.service.QuizStudentAnswerService;
 import nju.seec.helper.enums.QuizState;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.QuizService;
+import nju.seec.helper.service.QuizStudentAnswerService;
 import nju.seec.helper.vo.quiz.QuizStudentAnswerVO;
 import nju.seec.helper.vo.quiz.QuizVO;
 import org.springframework.data.domain.Pageable;

+ 2 - 2
src/main/java/nju/seec/helper/controller/ReplyController.java

@@ -1,10 +1,10 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
-import nju.seec.helper.dto.reply.ReplyDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.ReplyService;
+import nju.seec.helper.dto.reply.ReplyDTO;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.ReplyService;
 import nju.seec.helper.vo.ReplyVO;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;

+ 2 - 2
src/main/java/nju/seec/helper/controller/SlideController.java

@@ -1,15 +1,15 @@
 package nju.seec.helper.controller;
 
 import nju.seec.helper.aspect.auth.Auth;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.controller.response.PageResponse;
 import nju.seec.helper.dto.groups.Create;
 import nju.seec.helper.dto.groups.Modify;
 import nju.seec.helper.dto.slide.SlideDTO;
 import nju.seec.helper.dto.slide.SlideFileDTO;
 import nju.seec.helper.dto.slide.SlideStateDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
-import nju.seec.helper.service.SlideService;
 import nju.seec.helper.enums.UserType;
+import nju.seec.helper.service.SlideService;
 import nju.seec.helper.vo.SlideVO;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.web.PageableDefault;

+ 3 - 5
src/main/java/nju/seec/helper/controller/UserController.java

@@ -5,7 +5,7 @@ import nju.seec.helper.aspect.auth.Auth;
 import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dto.groups.Create;
 import nju.seec.helper.dto.groups.Modify;
-import nju.seec.helper.dto.user.LoginDTO;
+import nju.seec.helper.dto.common.LoginDTO;
 import nju.seec.helper.dto.user.ResetPasswordDTO;
 import nju.seec.helper.dto.user.UserDTO;
 import nju.seec.helper.enums.ExceptionType;
@@ -62,12 +62,11 @@ public class UserController {
 
         LoginUser user = new LoginUser()
                 .setId(userVO.getId())
-                .setName(userVO.getName())
                 .setType(userVO.getType());
 
         session.setAttribute(Consts.SESSION_USER_NAME, user);
-        // 3小时有效
-        session.setMaxInactiveInterval(3 * 60 * 60);
+        // 永不过
+        session.setMaxInactiveInterval(0);
         return userVO;
     }
 
@@ -91,7 +90,6 @@ public class UserController {
         UserVO userVO = userService.modifyUser(user, userDTO);
         user = ((LoginUser) session.getAttribute(Consts.SESSION_USER_NAME))
                 .setId(userVO.getId())
-                .setName(userVO.getName())
                 .setType(userVO.getType());
         session.setAttribute(Consts.SESSION_USER_NAME, user);
         return userVO;

+ 22 - 0
src/main/java/nju/seec/helper/dao/AdminDAO.java

@@ -0,0 +1,22 @@
+package nju.seec.helper.dao;
+
+import nju.seec.helper.entity.Admin;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import java.util.Optional;
+
+/**
+ * @author cst
+ */
+@Repository
+public interface AdminDAO extends JpaRepository<Admin, Long> {
+    /**
+     * 用户名密码查询
+     *
+     * @param username
+     * @param password
+     * @return
+     */
+    Optional<Admin> findByUsernameAndPassword(String username, String password);
+}

+ 2 - 1
src/main/java/nju/seec/helper/dao/UserDAO.java

@@ -6,6 +6,7 @@ import nju.seec.helper.exception.HelperException;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.stereotype.Repository;
 
 import java.util.Optional;
@@ -15,7 +16,7 @@ import java.util.Set;
  * @author cst
  */
 @Repository
-public interface UserDAO extends JpaRepository<User, Long> {
+public interface UserDAO extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {
     /**
      * 封装findById
      *

+ 18 - 0
src/main/java/nju/seec/helper/dto/common/LoginDTO.java

@@ -0,0 +1,18 @@
+package nju.seec.helper.dto.common;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import java.io.Serializable;
+
+/**
+ * @author cst
+ */
+@Data
+public class LoginDTO implements Serializable {
+    private static final long serialVersionUID = -3359173125836901241L;
+    @NotBlank(message = "登录名不能为空")
+    private String username;
+    @NotBlank(message = "密码不能为空")
+    private String password;
+}

+ 23 - 0
src/main/java/nju/seec/helper/entity/Admin.java

@@ -0,0 +1,23 @@
+package nju.seec.helper.entity;
+
+import lombok.Data;
+
+import javax.persistence.*;
+
+/**
+ * @author cst
+ */
+@Data
+@Entity
+@Table(name = "admin", uniqueConstraints = @UniqueConstraint(name = "username_unique", columnNames = "username"))
+public class Admin {
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    private Long id;
+
+    @Column(nullable = false, unique = true)
+    private String username;
+
+    @Column(nullable = false)
+    private String password;
+}

+ 1 - 1
src/main/java/nju/seec/helper/enums/UserType.java

@@ -5,5 +5,5 @@ package nju.seec.helper.enums;
  */
 
 public enum UserType {
-    TEACHER, STUDENT
+    TEACHER, STUDENT, ADMIN
 }

+ 40 - 0
src/main/java/nju/seec/helper/service/AdminService.java

@@ -0,0 +1,40 @@
+package nju.seec.helper.service;
+
+import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.enums.UserState;
+import nju.seec.helper.enums.UserType;
+import nju.seec.helper.vo.AdminVO;
+import nju.seec.helper.vo.UserVO;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+/**
+ * @author cst
+ */
+public interface AdminService {
+    /**
+     * 用户名密码获取
+     *
+     * @param username
+     * @param password
+     * @return
+     */
+    AdminVO getAdminByUsernameAndPassword(String username, String password);
+
+    /**
+     * 获取用户列表
+     *
+     * @param type
+     * @param state
+     * @param key
+     * @param pageable
+     * @return
+     */
+    Page<UserVO> getUsers(UserType type, UserState state, String key, Pageable pageable);
+
+    void passUser(LoginUser admin, Long userId);
+
+    void lockUser(LoginUser admin, Long userId);
+
+    void unlockUser(LoginUser admin, Long userId);
+}

+ 1 - 1
src/main/java/nju/seec/helper/service/CommentService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.comment.CommentDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.comment.CommentDTO;
 import nju.seec.helper.vo.CommentVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;

+ 1 - 1
src/main/java/nju/seec/helper/service/CourseFileService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.course.CourseFileDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.course.CourseFileDTO;
 import nju.seec.helper.vo.CourseFileVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;

+ 13 - 3
src/main/java/nju/seec/helper/service/CourseService.java

@@ -1,8 +1,8 @@
 package nju.seec.helper.service;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dto.course.ChooseDTO;
 import nju.seec.helper.dto.course.CourseDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.vo.CourseVO;
 import nju.seec.helper.vo.UserVO;
 import org.springframework.data.domain.Page;
@@ -66,14 +66,24 @@ public interface CourseService {
     Page<CourseVO> getCourses(String key, Pageable pageable);
 
     /**
-     * 获取课程列表
+     * 获取创建课程列表
+     *
+     * @param user
+     * @param key
+     * @param pageable
+     * @return
+     */
+    Page<CourseVO> getCreatedCourses(LoginUser user, String key, Pageable pageable);
+
+    /**
+     * 获取选课列表
      *
      * @param user
      * @param key
      * @param pageable
      * @return
      */
-    Page<CourseVO> getCourses(LoginUser user, String key, Pageable pageable);
+    Page<CourseVO> getChosenCourses(LoginUser user, String key, Pageable pageable);
 
     /**
      * 获取课程选课码

+ 1 - 1
src/main/java/nju/seec/helper/service/MessageService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.message.MessageDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.message.MessageDTO;
 import nju.seec.helper.entity.Message;
 import nju.seec.helper.enums.MessageType;
 import org.springframework.data.domain.Page;

+ 1 - 1
src/main/java/nju/seec/helper/service/NoticeService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.notice.NoticeDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.notice.NoticeDTO;
 import nju.seec.helper.vo.NoticeVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;

+ 1 - 1
src/main/java/nju/seec/helper/service/QuestionService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.question.BaseQuestionDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.question.BaseQuestionDTO;
 import nju.seec.helper.entity.Quiz;
 import nju.seec.helper.vo.question.BaseQuestionVO;
 import org.springframework.data.domain.Page;

+ 1 - 1
src/main/java/nju/seec/helper/service/QuizService.java

@@ -3,8 +3,8 @@ package nju.seec.helper.service;
 //import nju.seec.helper.dto.quiz.QuizDTO;
 //import nju.seec.helper.vo.quiz.QuizVO;
 
-import nju.seec.helper.dto.quiz.QuizDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.quiz.QuizDTO;
 import nju.seec.helper.entity.Slide;
 import nju.seec.helper.enums.QuizState;
 import nju.seec.helper.vo.quiz.QuizVO;

+ 1 - 1
src/main/java/nju/seec/helper/service/QuizStudentAnswerService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.quiz.QuizStudentAnswerDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.quiz.QuizStudentAnswerDTO;
 import nju.seec.helper.vo.quiz.QuizStudentAnswerVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;

+ 1 - 1
src/main/java/nju/seec/helper/service/ReplyService.java

@@ -1,7 +1,7 @@
 package nju.seec.helper.service;
 
-import nju.seec.helper.dto.reply.ReplyDTO;
 import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dto.reply.ReplyDTO;
 import nju.seec.helper.vo.ReplyVO;
 
 /**

+ 1 - 1
src/main/java/nju/seec/helper/service/SlideService.java

@@ -1,9 +1,9 @@
 package nju.seec.helper.service;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dto.slide.SlideDTO;
 import nju.seec.helper.dto.slide.SlideFileDTO;
 import nju.seec.helper.dto.slide.SlideStateDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.vo.SlideVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;

+ 86 - 0
src/main/java/nju/seec/helper/service/impl/AdminServiceImpl.java

@@ -0,0 +1,86 @@
+package nju.seec.helper.service.impl;
+
+import nju.seec.helper.aspect.auth.LoginUser;
+import nju.seec.helper.dao.AdminDAO;
+import nju.seec.helper.dao.UserDAO;
+import nju.seec.helper.entity.User;
+import nju.seec.helper.enums.ExceptionType;
+import nju.seec.helper.enums.UserState;
+import nju.seec.helper.enums.UserType;
+import nju.seec.helper.exception.HelperException;
+import nju.seec.helper.service.AdminService;
+import nju.seec.helper.service.util.StringUtils;
+import nju.seec.helper.util.EncryptUtils;
+import nju.seec.helper.vo.AdminVO;
+import nju.seec.helper.vo.UserVO;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+/**
+ * @author cst
+ */
+@Service
+public class AdminServiceImpl implements AdminService {
+    private final AdminDAO adminDAO;
+    private final UserDAO userDAO;
+
+    public AdminServiceImpl(AdminDAO adminDAO, UserDAO userDAO) {
+        this.adminDAO = adminDAO;
+        this.userDAO = userDAO;
+    }
+
+    @Transactional(readOnly = true)
+    @Override
+    public AdminVO getAdminByUsernameAndPassword(String username, String password) {
+        return adminDAO.findByUsernameAndPassword(username, EncryptUtils.encode(password))
+                .map(AdminVO::new)
+                .orElseThrow(() -> HelperException.of(ExceptionType.NOT_FOUND, "用户名密码错误"));
+    }
+
+    @Transactional(readOnly = true)
+    @Override
+    public Page<UserVO> getUsers(UserType type, UserState state, String key, Pageable pageable) {
+        return findUsersByTypeAndStateAndKey(type, state, key, pageable).map(UserVO::new);
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void passUser(LoginUser admin, Long userId) {
+        User user = userDAO.findUserById(userId);
+        if (user.getState() != UserState.CHECKING) {
+            throw HelperException.of(ExceptionType.FORBIDDEN, "该帐号暂时无法审核通过");
+        }
+        userDAO.save(user.setState(UserState.NORMAL));
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void lockUser(LoginUser admin, Long userId) {
+        userDAO.save(userDAO.findUserById(userId).setState(UserState.LOCKED));
+    }
+
+    @Transactional(rollbackFor = Exception.class)
+    @Override
+    public void unlockUser(LoginUser admin, Long userId) {
+        User user = userDAO.findUserById(userId);
+        if (user.getState() != UserState.LOCKED) {
+            throw HelperException.of(ExceptionType.FORBIDDEN, "该帐号暂时无法解封");
+        }
+        userDAO.save(user.setState(UserState.NORMAL));
+    }
+
+    private Page<User> findUsersByTypeAndStateAndKey(UserType userType, UserState userState, String key, Pageable pageable) {
+        final String keyPattern = StringUtils.keyPattern(key);
+
+        return userDAO.findAll(
+                (Specification<User>) (root, query, cb) -> cb.and(
+                        cb.equal(root.get("type").as(UserType.class), userType)
+                        , cb.equal(root.get("state").as(UserState.class), userState)
+                        , cb.or(cb.like(root.get("email"), keyPattern), cb.like(root.get("phone"), keyPattern), cb.like(root.get("name"), keyPattern))
+                )
+                , pageable);
+    }
+}

+ 7 - 7
src/main/java/nju/seec/helper/service/impl/CommentServiceImpl.java

@@ -1,16 +1,16 @@
 package nju.seec.helper.service.impl;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.CommentDAO;
 import nju.seec.helper.dao.SlideDAO;
 import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.comment.CommentDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Comment;
 import nju.seec.helper.entity.Slide;
+import nju.seec.helper.enums.MessageType;
 import nju.seec.helper.service.CommentService;
 import nju.seec.helper.service.MessageService;
 import nju.seec.helper.service.util.AuthUtils;
-import nju.seec.helper.enums.MessageType;
 import nju.seec.helper.vo.CommentVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
@@ -42,16 +42,16 @@ public class CommentServiceImpl implements CommentService {
     @Transactional(rollbackFor = Exception.class)
     @Override
     public CommentVO createComment(LoginUser user, CommentDTO commentDTO) {
-        Comment comment = new Comment()
-                .setSlideId(commentDTO.getSlideId())
+        Slide slide = slideDAO.findSlideById(commentDTO.getSlideId());
+
+        Comment comment = commentDAO.save(new Comment()
+                .setSlideId(slide.getId())
                 .setPageNumber(commentDTO.getPageNumber())
                 .setUser(userDAO.findUserById(user.getId()))
                 .setTitle(commentDTO.getTitle())
-                .setContent(commentDTO.getContent());
-        comment = commentDAO.save(comment);
+                .setContent(commentDTO.getContent()));
 
         // 增加通知
-        Slide slide = slideDAO.findSlideById(comment.getSlideId());
         if (!user.getId().equals(slide.getTeacher().getId())) {
             messageService.createMessage(Collections.singleton(slide.getTeacher().getId()), MessageType.COMMENT_NEW, String.format("[%s] - [%s] 在第%d页有新评论", slide.getCourse().getName(), slide.getName(), comment.getPageNumber()), slide.getId());
         }

+ 5 - 7
src/main/java/nju/seec/helper/service/impl/CourseFileServiceImpl.java

@@ -1,20 +1,20 @@
 package nju.seec.helper.service.impl;
 
 import lombok.SneakyThrows;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.CourseDAO;
 import nju.seec.helper.dao.CourseFileDAO;
 import nju.seec.helper.dto.course.CourseFileDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Course;
 import nju.seec.helper.entity.CourseFile;
+import nju.seec.helper.enums.ExceptionType;
+import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.service.CourseFileService;
 import nju.seec.helper.service.CourseService;
 import nju.seec.helper.service.util.AuthUtils;
 import nju.seec.helper.util.Consts;
 import nju.seec.helper.util.OssUtils;
 import nju.seec.helper.util.RedisCacheUtils;
-import nju.seec.helper.enums.ExceptionType;
-import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.vo.CourseFileVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
@@ -65,13 +65,11 @@ public class CourseFileServiceImpl implements CourseFileService {
 
         String objectName = getCourseFileObjectName(course.getId(), fileName);
 
-        CourseFile courseFile = new CourseFile()
+        courseFileDAO.save(new CourseFile()
                 .setCourseId(course.getId())
                 .setTeacherId(user.getId())
                 .setFileName(file.getOriginalFilename())
-                .setObjectName(objectName);
-
-        courseFileDAO.save(courseFile);
+                .setObjectName(objectName));
 
         ossUtils.upload(objectName, file.getInputStream());
     }

+ 15 - 29
src/main/java/nju/seec/helper/service/impl/CourseServiceImpl.java

@@ -1,19 +1,19 @@
 package nju.seec.helper.service.impl;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.ChooseDAO;
 import nju.seec.helper.dao.CourseDAO;
 import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.course.ChooseDTO;
 import nju.seec.helper.dto.course.CourseDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Choose;
 import nju.seec.helper.entity.Course;
+import nju.seec.helper.enums.ExceptionType;
+import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.service.CourseService;
 import nju.seec.helper.service.util.AuthUtils;
 import nju.seec.helper.service.util.StringUtils;
 import nju.seec.helper.util.EncryptUtils;
-import nju.seec.helper.enums.ExceptionType;
-import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.vo.CourseVO;
 import nju.seec.helper.vo.UserVO;
 import org.springframework.data.domain.Page;
@@ -46,13 +46,11 @@ public class CourseServiceImpl implements CourseService {
         // 检查
         checkSameCourseName(0L, teacher.getId(), courseDTO.getName());
 
-        Course course = new Course()
+        return new CourseVO(courseDAO.save(new Course()
                 .setTeacher(userDAO.findUserById(teacher.getId()))
                 .setName(courseDTO.getName())
                 .setBio(courseDTO.getBio())
-                .setCode(EncryptUtils.encode(courseDTO.getCode()));
-        course = courseDAO.save(course);
-        return new CourseVO(course);
+                .setCode(EncryptUtils.encode(courseDTO.getCode()))));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -64,11 +62,9 @@ public class CourseServiceImpl implements CourseService {
         AuthUtils.checkDataAuth(user.getId(), course.getTeacher().getId(), "您无权修改该课程");
         checkSameCourseName(course.getId(), user.getId(), courseDTO.getName());
 
-        course.setName(courseDTO.getName())
+        return new CourseVO(courseDAO.save(course.setName(courseDTO.getName())
                 .setBio(courseDTO.getBio())
-                .setCode(EncryptUtils.encode(courseDTO.getCode()));
-        course = courseDAO.save(course);
-        return new CourseVO(course);
+                .setCode(EncryptUtils.encode(courseDTO.getCode()))));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -93,10 +89,9 @@ public class CourseServiceImpl implements CourseService {
             throw HelperException.of(ExceptionType.NOT_FOUND, "选课码错误");
         }
 
-        Choose choose = new Choose()
+        chooseDAO.save(new Choose()
                 .setCourseId(courseId)
-                .setStudentId(studentId);
-        chooseDAO.save(choose);
+                .setStudentId(studentId));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -113,23 +108,14 @@ public class CourseServiceImpl implements CourseService {
 
     @Transactional(readOnly = true)
     @Override
-    public Page<CourseVO> getCourses(LoginUser user, String key, Pageable pageable) {
-        switch (user.getType()) {
-            case TEACHER:
-                return teacherGetCourses(user.getId(), key, pageable);
-            case STUDENT:
-                return studentGetCourses(user.getId(), key, pageable);
-            default:
-                return Page.empty();
-        }
+    public Page<CourseVO> getCreatedCourses(LoginUser user, String key, Pageable pageable) {
+        return findByTeacherIdAndKey(user.getId(), key, pageable).map(CourseVO::new);
     }
 
-    private Page<CourseVO> teacherGetCourses(Long teacherId, String key, Pageable pageable) {
-        return findByTeacherIdAndKey(teacherId, key, pageable).map(CourseVO::new);
-    }
-
-    private Page<CourseVO> studentGetCourses(Long studentId, String key, Pageable pageable) {
-        return findByIdsAndKey(chooseDAO.findCourseIdsByStudentId(studentId), key, pageable).map(CourseVO::new);
+    @Transactional(readOnly = true)
+    @Override
+    public Page<CourseVO> getChosenCourses(LoginUser user, String key, Pageable pageable) {
+        return findByIdsAndKey(chooseDAO.findCourseIdsByStudentId(user.getId()), key, pageable).map(CourseVO::new);
     }
 
     @Transactional(readOnly = true)

+ 2 - 2
src/main/java/nju/seec/helper/service/impl/MessageServiceImpl.java

@@ -1,11 +1,11 @@
 package nju.seec.helper.service.impl;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.MessageDAO;
 import nju.seec.helper.dto.message.MessageDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Message;
-import nju.seec.helper.service.MessageService;
 import nju.seec.helper.enums.MessageType;
+import nju.seec.helper.service.MessageService;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.scheduling.annotation.Async;

+ 9 - 11
src/main/java/nju/seec/helper/service/impl/NoticeServiceImpl.java

@@ -1,17 +1,17 @@
 package nju.seec.helper.service.impl;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.ChooseDAO;
 import nju.seec.helper.dao.CourseDAO;
 import nju.seec.helper.dao.NoticeDAO;
 import nju.seec.helper.dto.notice.NoticeDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Course;
 import nju.seec.helper.entity.Notice;
+import nju.seec.helper.enums.MessageType;
 import nju.seec.helper.service.MessageService;
 import nju.seec.helper.service.NoticeService;
 import nju.seec.helper.service.util.AuthUtils;
 import nju.seec.helper.service.util.StringUtils;
-import nju.seec.helper.enums.MessageType;
 import nju.seec.helper.vo.NoticeVO;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
@@ -44,15 +44,14 @@ public class NoticeServiceImpl implements NoticeService {
     public NoticeVO createNotice(LoginUser user, NoticeDTO noticeDTO) {
         Course course = courseDAO.findCourseById(noticeDTO.getCourseId());
         AuthUtils.checkDataAuth(user.getId(), course.getTeacher().getId(), "您无权创建该课程的公告");
-        Notice notice = new Notice()
-                .setCourseId(noticeDTO.getCourseId())
-                .setTeacherId(user.getId())
-                .setTitle(noticeDTO.getTitle())
-                .setContent(noticeDTO.getContent());
 
         Set<Long> studentIds = chooseDAO.findStudentIdsByCourseId(noticeDTO.getCourseId());
         messageService.createMessage(studentIds, MessageType.NOTICE_NEW, String.format("[%s] 发布了新公告", course.getName()), null);
-        return new NoticeVO(noticeDAO.save(notice));
+        return new NoticeVO(noticeDAO.save(new Notice()
+                .setCourseId(noticeDTO.getCourseId())
+                .setTeacherId(user.getId())
+                .setTitle(noticeDTO.getTitle())
+                .setContent(noticeDTO.getContent())));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -60,9 +59,8 @@ public class NoticeServiceImpl implements NoticeService {
     public NoticeVO modifyNotice(LoginUser user, Long noticeId, NoticeDTO noticeDTO) {
         Notice notice = noticeDAO.findNoticeById(noticeId);
         AuthUtils.checkDataAuth(user.getId(), notice.getTeacherId(), "您无权修改该公告");
-        notice.setTitle(noticeDTO.getTitle())
-                .setContent(noticeDTO.getContent());
-        return new NoticeVO(noticeDAO.save(notice));
+
+        return new NoticeVO(noticeDAO.save(notice.setTitle(noticeDTO.getTitle()).setContent(noticeDTO.getContent())));
     }
 
     @Transactional(readOnly = true)

+ 6 - 8
src/main/java/nju/seec/helper/service/impl/QuestionServiceImpl.java

@@ -1,18 +1,18 @@
 package nju.seec.helper.service.impl;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.QuestionRecordDAO;
 import nju.seec.helper.dao.QuizDAO;
 import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.question.BaseQuestionDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.QuestionRecord;
 import nju.seec.helper.entity.Quiz;
-import nju.seec.helper.service.QuestionService;
-import nju.seec.helper.util.BokUtil;
 import nju.seec.helper.enums.ExceptionType;
 import nju.seec.helper.enums.QuizState;
 import nju.seec.helper.enums.UserType;
 import nju.seec.helper.exception.HelperException;
+import nju.seec.helper.service.QuestionService;
+import nju.seec.helper.util.BokUtil;
 import nju.seec.helper.vo.question.BaseQuestionVO;
 import nju.seec.helper.vo.question.BokQuestion;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -91,10 +91,9 @@ public class QuestionServiceImpl implements QuestionService {
         } while (questionRecordDAO.existsByQuestionId(questionId));
 
         BokQuestion bokQuestion = bokUtil.createQuestion(questionId, baseQuestionDTO);
-        QuestionRecord questionRecord = new QuestionRecord()
+        questionRecordDAO.save(new QuestionRecord()
                 .setTeacher(userDAO.findUserById(user.getId()))
-                .setQuestionId(bokQuestion.getId());
-        questionRecordDAO.save(questionRecord);
+                .setQuestionId(bokQuestion.getId()));
         return BaseQuestionVO.convertBokQuestionToVO(bokQuestion, true);
     }
 
@@ -120,8 +119,7 @@ public class QuestionServiceImpl implements QuestionService {
     @Transactional(readOnly = true)
     @Override
     public Page<BaseQuestionVO> getCreatedQuestions(LoginUser user, Pageable pageable) {
-        Page<String> questionIdPage = questionRecordDAO
-                .findQuestionIdsByTeacher(userDAO.findUserById(user.getId()), pageable);
+        Page<String> questionIdPage = questionRecordDAO.findQuestionIdsByTeacher(userDAO.findUserById(user.getId()), pageable);
 
         return new PageImpl<>(
                 bokUtil.bokFindByIdIn(questionIdPage.getContent())

+ 7 - 11
src/main/java/nju/seec/helper/service/impl/QuizServiceImpl.java

@@ -1,18 +1,18 @@
 package nju.seec.helper.service.impl;
 
 import com.google.common.collect.ImmutableSet;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.*;
 import nju.seec.helper.dto.quiz.QuizDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Course;
 import nju.seec.helper.entity.Quiz;
 import nju.seec.helper.entity.Slide;
+import nju.seec.helper.enums.*;
+import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.service.MessageService;
 import nju.seec.helper.service.QuizService;
 import nju.seec.helper.service.util.AuthUtils;
 import nju.seec.helper.service.util.StringUtils;
-import nju.seec.helper.enums.*;
-import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.vo.quiz.QuizVO;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
@@ -61,7 +61,7 @@ public class QuizServiceImpl implements QuizService {
         checkSameName(0L, quizDTO.getSlideId(), quizDTO.getName());
         checkQuizTypeOkToSlideState(slide.getState(), quizDTO.getType());
 
-        Quiz quiz = new Quiz()
+        return new QuizVO(quizDAO.save(new Quiz()
                 .setCourse(slide.getCourse())
                 .setSlide(slide)
                 .setTeacher(slide.getTeacher())
@@ -69,9 +69,7 @@ public class QuizServiceImpl implements QuizService {
                 .setState(QuizState.getQuizStateBySlideStateAndQuizType(slide.getState(), quizDTO.getType()))
                 .setName(quizDTO.getName())
                 .setMaxSubmitNumber(quizDTO.getMaxSubmitTime())
-                .setQuestions(quizDTO.getQuestions());
-
-        return new QuizVO(quizDAO.save(quiz));
+                .setQuestions(quizDTO.getQuestions())));
     }
 
     @Transactional(rollbackFor = Exception.class)
@@ -87,13 +85,11 @@ public class QuizServiceImpl implements QuizService {
 
         checkSameName(quiz.getId(), quizDTO.getSlideId(), quizDTO.getName());
 
-        quiz.setType(quizDTO.getType())
+        return new QuizVO(quizDAO.save(quiz.setType(quizDTO.getType())
                 .setState(QuizState.getQuizStateBySlideStateAndQuizType(slide.getState(), quizDTO.getType()))
                 .setName(quizDTO.getName())
                 .setMaxSubmitNumber(quizDTO.getMaxSubmitTime())
-                .setQuestions(quizDTO.getQuestions());
-
-        return new QuizVO(quizDAO.save(quiz));
+                .setQuestions(quizDTO.getQuestions())));
     }
 
     private void checkSameName(Long quizId, Long slideId, String name) {

+ 4 - 5
src/main/java/nju/seec/helper/service/impl/QuizStudentAnswerServiceImpl.java

@@ -1,17 +1,17 @@
 package nju.seec.helper.service.impl;
 
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.QuizDAO;
 import nju.seec.helper.dao.QuizStudentAnswerDAO;
 import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.quiz.QuizStudentAnswerDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Quiz;
 import nju.seec.helper.entity.QuizStudentAnswer;
 import nju.seec.helper.entity.User;
-import nju.seec.helper.service.QuestionService;
-import nju.seec.helper.service.QuizStudentAnswerService;
 import nju.seec.helper.enums.ExceptionType;
 import nju.seec.helper.exception.HelperException;
+import nju.seec.helper.service.QuestionService;
+import nju.seec.helper.service.QuizStudentAnswerService;
 import nju.seec.helper.vo.question.BaseQuestionVO;
 import nju.seec.helper.vo.quiz.QuizStudentAnswerVO;
 import org.springframework.data.domain.Page;
@@ -94,8 +94,7 @@ public class QuizStudentAnswerServiceImpl implements QuizStudentAnswerService {
     @Transactional(readOnly = true)
     @Override
     public Page<QuizStudentAnswerVO> teacherGetQuizStudentAnswers(LoginUser user, Long quizId, Pageable pageable) {
-        return quizStudentAnswerDAO.findByQuiz(quizDAO.findQuizById(quizId), pageable)
-                .map(quizStudentAnswer -> new QuizStudentAnswerVO(quizStudentAnswer, questionService.getQuestionsByQuiz(quizDAO.findQuizById(quizId))));
+        return quizStudentAnswerDAO.findByQuiz(quizDAO.findQuizById(quizId), pageable).map(quizStudentAnswer -> new QuizStudentAnswerVO(quizStudentAnswer, questionService.getQuestionsByQuiz(quizDAO.findQuizById(quizId))));
     }
 
     @Transactional(readOnly = true)

+ 4 - 4
src/main/java/nju/seec/helper/service/impl/ReplyServiceImpl.java

@@ -1,21 +1,21 @@
 package nju.seec.helper.service.impl;
 
 import lombok.extern.slf4j.Slf4j;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.CommentDAO;
 import nju.seec.helper.dao.ReplyDAO;
 import nju.seec.helper.dao.SlideDAO;
 import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.reply.ReplyDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Comment;
 import nju.seec.helper.entity.Reply;
 import nju.seec.helper.entity.Slide;
-import nju.seec.helper.service.MessageService;
-import nju.seec.helper.service.ReplyService;
-import nju.seec.helper.service.util.AuthUtils;
 import nju.seec.helper.enums.ExceptionType;
 import nju.seec.helper.enums.MessageType;
 import nju.seec.helper.exception.HelperException;
+import nju.seec.helper.service.MessageService;
+import nju.seec.helper.service.ReplyService;
+import nju.seec.helper.service.util.AuthUtils;
 import nju.seec.helper.vo.ReplyVO;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;

+ 5 - 5
src/main/java/nju/seec/helper/service/impl/SlideServiceImpl.java

@@ -3,6 +3,7 @@ package nju.seec.helper.service.impl;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.io.Files;
 import lombok.SneakyThrows;
+import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.dao.ChooseDAO;
 import nju.seec.helper.dao.CourseDAO;
 import nju.seec.helper.dao.SlideDAO;
@@ -10,9 +11,12 @@ import nju.seec.helper.dao.UserDAO;
 import nju.seec.helper.dto.slide.SlideDTO;
 import nju.seec.helper.dto.slide.SlideFileDTO;
 import nju.seec.helper.dto.slide.SlideStateDTO;
-import nju.seec.helper.aspect.auth.LoginUser;
 import nju.seec.helper.entity.Course;
 import nju.seec.helper.entity.Slide;
+import nju.seec.helper.enums.ExceptionType;
+import nju.seec.helper.enums.MessageType;
+import nju.seec.helper.enums.SlideState;
+import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.service.CourseService;
 import nju.seec.helper.service.MessageService;
 import nju.seec.helper.service.QuizService;
@@ -22,10 +26,6 @@ import nju.seec.helper.service.util.StringUtils;
 import nju.seec.helper.util.Consts;
 import nju.seec.helper.util.OssUtils;
 import nju.seec.helper.util.RedisCacheUtils;
-import nju.seec.helper.enums.ExceptionType;
-import nju.seec.helper.enums.MessageType;
-import nju.seec.helper.enums.SlideState;
-import nju.seec.helper.exception.HelperException;
 import nju.seec.helper.util.file.FileInfo;
 import nju.seec.helper.util.file.FileParserContext;
 import nju.seec.helper.vo.SlideVO;

+ 2 - 7
src/main/java/nju/seec/helper/service/impl/UserServiceImpl.java

@@ -52,24 +52,19 @@ public class UserServiceImpl implements UserService {
             throw HelperException.of(ExceptionType.NOT_FOUND, "手机号或验证码错误");
         }
 
-        User user = new User()
+        User user = userDAO.save(new User()
                 .setName(userDTO.getName())
                 .setEmail(userDTO.getEmail())
                 .setPhone(userDTO.getPhone())
                 .setPassword(EncryptUtils.encode(userDTO.getPassword()))
                 .setType(userDTO.getType())
-                .setState(userDTO.getType() == UserType.TEACHER ? UserState.CHECKING : UserState.NORMAL);
+                .setState(userDTO.getType() == UserType.TEACHER ? UserState.CHECKING : UserState.NORMAL));
 
-        user = userDAO.save(user);
         cacheUtils.remove(Consts.EMAIL_CACHE_NAME, email);
         cacheUtils.remove(Consts.PHONE_CACHE_NAME, phone);
         return new UserVO(user);
     }
 
-//    private UserType getUserType(String email) {
-//        return EMAIL_SUFFIX_AND_USER_TYPE_MAP.get(email.substring(email.indexOf("@") + 1));
-//    }
-
     @Transactional(rollbackFor = Exception.class)
     @Override
     public UserVO modifyUser(LoginUser loginUser, UserDTO userDTO) {

+ 11 - 14
src/main/java/nju/seec/helper/util/BokUtil.java

@@ -35,10 +35,12 @@ import java.util.stream.Collectors;
 public class BokUtil {
     private final RestRequestUtil restRequestUtil;
     private final GuavaCacheUtil cacheUtils;
-    @Value("${bok.searchUrl}")
-    private String searchUrl;
     @Value("${bok.tqUrl}")
     private String tqUrl;
+    @Value("${bok.tqStemSearchUrl}")
+    private String tqStemSearchUrl;
+    @Value("${bok.tqIdSearchUrl}")
+    private String tqIdSearchUrl;
 
     public BokUtil(RestRequestUtil restRequestUtil, GuavaCacheUtil cacheUtils) {
         this.restRequestUtil = restRequestUtil;
@@ -51,14 +53,10 @@ public class BokUtil {
                 "page", String.valueOf(1 + pageable.getPageNumber()),
                 "size", String.valueOf(pageable.getPageSize())
         );
-        BokSearchResult result = restRequestUtil.sendGetRequest(
-                searchUrl + "findByStemLike?content={content}&size={size}&page={page}", BokSearchResult.class, urlParams);
+        BokSearchResult result = restRequestUtil.sendGetRequest(tqStemSearchUrl, BokSearchResult.class, urlParams);
 
         List<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
-        cacheUtils.setAll(BOK_CACHE_NAME,
-                bokQuestions.parallelStream()
-                        .collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion))
-        );
+        cacheUtils.setAll(BOK_CACHE_NAME, bokQuestions.parallelStream().collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion)));
         return new PageImpl<>(bokQuestions, pageable, result.getPage().getTotalElements());
     }
 
@@ -77,11 +75,10 @@ public class BokUtil {
         if (!requestIds.isEmpty()) {
             Map<String, String> urlParams = ImmutableMap.of("id", requestIds.stream().reduce((a, b) -> a + "," + b).orElse(""));
 
-            BokSearchResult result = restRequestUtil.sendGetRequest(searchUrl + "findByIdIn?id={id}", BokSearchResult.class, urlParams);
+            BokSearchResult result = restRequestUtil.sendGetRequest(tqIdSearchUrl, BokSearchResult.class, urlParams);
             List<BokQuestion> bokQuestions = result.getEmbedded().getQuestions();
 
-            Map<String, BokQuestion> remoteBokQuestionsMap = bokQuestions.parallelStream()
-                    .collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion));
+            Map<String, BokQuestion> remoteBokQuestionsMap = bokQuestions.parallelStream().collect(Collectors.toMap(BokQuestion::getId, bokQuestion -> bokQuestion));
 
             bokQuestionsMap.putAll(remoteBokQuestionsMap);
             cacheUtils.setAll(BOK_CACHE_NAME, (Map) remoteBokQuestionsMap);
@@ -97,7 +94,7 @@ public class BokUtil {
         if (cachedBokQuestion instanceof BokQuestion) {
             return (BokQuestion) cachedBokQuestion;
         }
-        BokQuestion bokQuestion = restRequestUtil.sendGetRequest(tqUrl + questionId, BokQuestion.class, Collections.emptyMap());
+        BokQuestion bokQuestion = restRequestUtil.sendGetRequest(tqUrl + "/" + questionId, BokQuestion.class, Collections.emptyMap());
         cacheUtils.set(BOK_CACHE_NAME, bokQuestion.getId(), bokQuestion);
         return bokQuestion;
     }
@@ -112,12 +109,12 @@ public class BokUtil {
     public BokQuestion modifyQuestion(String questionId, BaseQuestionDTO baseQuestionDTO) {
         BokQuestion bokQuestion = getQuestion(baseQuestionDTO);
         bokQuestion.setId(questionId);
-        restRequestUtil.sendPutRequest(tqUrl + questionId, bokQuestion);
+        restRequestUtil.sendPutRequest(tqUrl + "/" + questionId, bokQuestion);
         return bokFindById(questionId);
     }
 
     public void deleteQuestion(String questionId) {
-        restRequestUtil.sendDeleteRequest(tqUrl + questionId);
+        restRequestUtil.sendDeleteRequest(tqUrl + "/" + questionId);
     }
 
     private BokQuestion getQuestion(BaseQuestionDTO baseQuestionDTO) {

+ 19 - 0
src/main/java/nju/seec/helper/vo/AdminVO.java

@@ -0,0 +1,19 @@
+package nju.seec.helper.vo;
+
+import lombok.Data;
+import lombok.NonNull;
+import nju.seec.helper.entity.Admin;
+
+/**
+ * @author cst
+ */
+@Data
+public class AdminVO {
+    private Long id;
+    private String username;
+
+    public AdminVO(@NonNull Admin admin) {
+        this.id = admin.getId();
+        this.username = admin.getUsername();
+    }
+}

+ 4 - 2
src/main/resources/application-deploy.yml

@@ -52,8 +52,10 @@ aliyun:
     templateCode: SMS_181555598
 bok:
   url: http://bok.seecoder.cn
-  searchUrl: ${bok.url}/api/question/search/
-  tqUrl: ${bok.url}/api/question/
+  tqUrl: ${bok.url}/api/question
+  tqSearchUrl: ${bok.tqUrl}/search
+  tqStemSearchUrl: ${bok.tqSearchUrl}/findByStemLike?content={content}&size={size}&page={page}
+  tqIdSearchUrl: ${bok.tqSearchUrl}/findByIdIn?id={id}
 guavaCache:
   maximumSize: 1000
   expireAfterAccessInSeconds: 0

+ 4 - 2
src/main/resources/application-dev.yml

@@ -52,8 +52,10 @@ aliyun:
     templateCode: SMS_181555598
 bok:
   url: http://bok.seecoder.cn
-  searchUrl: ${bok.url}/api/question/search/
-  tqUrl: ${bok.url}/api/question/
+  tqUrl: ${bok.url}/api/question
+  tqSearchUrl: ${bok.tqUrl}/search
+  tqStemSearchUrl: ${bok.tqSearchUrl}/findByStemLike?content={content}&size={size}&page={page}
+  tqIdSearchUrl: ${bok.tqSearchUrl}/findByIdIn?id={id}
 guavaCache:
   maximumSize: 1000
   expireAfterAccessInSeconds: 0