diff --git a/backend/src/main/java/lsgwr/exam/controller/UserController.java b/backend/src/main/java/lsgwr/exam/controller/UserController.java index 679b26e..e594d70 100644 --- a/backend/src/main/java/lsgwr/exam/controller/UserController.java +++ b/backend/src/main/java/lsgwr/exam/controller/UserController.java @@ -1,95 +1,10 @@ -/*********************************************************** - * @Description : 对外REST接口 - * @author : 梁山广(Laing Shan Guang) - * @date : 2019-05-16 23:45 - * @email : liangshanguang2@gmail.com - ***********************************************************/ -package lsgwr.exam.controller; - -import lsgwr.exam.dto.RegisterDTO; -import lsgwr.exam.entity.User; -import lsgwr.exam.enums.ResultEnum; -import lsgwr.exam.qo.LoginQo; -import lsgwr.exam.service.UserService; -import lsgwr.exam.vo.ResultVO; -import lsgwr.exam.vo.UserInfoVo; -import lsgwr.exam.vo.UserVo; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.*; - -import javax.servlet.http.HttpServletRequest; - -@RestController -@Api(tags = "User APIs") -@RequestMapping("/api/user") -public class UserController { - - @Autowired - private UserService userService; - - @PostMapping("/register") - @ApiOperation("注册") - ResultVO register(@RequestBody RegisterDTO registerDTO) { - ResultVO resultVO; - // 注册信息的完善,还有唯一性校验没(用户名、邮箱和手机号)已经在user表中通过unique来设置了 - User user = userService.register(registerDTO); - if (user != null) { - // 注册成功 - resultVO = new ResultVO<>(ResultEnum.REGISTER_SUCCESS.getCode(), ResultEnum.REGISTER_SUCCESS.getMessage(), user); - } else { - resultVO = new ResultVO<>(ResultEnum.REGISTER_FAILED.getCode(), ResultEnum.REGISTER_FAILED.getMessage(), null); - } - return resultVO; - } - - @PostMapping("/login") - @ApiOperation("根据用户名或邮箱登录,登录成功返回token") - ResultVO login(@RequestBody LoginQo loginQo) { // 这里不用手机号是因为手机号和用户名难以进行格式区分,而用户名和 - // 用户登录 - ResultVO resultVO; - String token = userService.login(loginQo); - if (token != null) { - // 登录成功 - resultVO = new ResultVO<>(ResultEnum.LOGIN_SUCCESS.getCode(), ResultEnum.LOGIN_SUCCESS.getMessage(), token); - } else { - // 登录失败 - resultVO = new ResultVO<>(ResultEnum.LOGIN_FAILED.getCode(), ResultEnum.LOGIN_FAILED.getMessage(), null); - } - return resultVO; - } - - @GetMapping("/user-info") - @ApiOperation("获取用户信息") - ResultVO getUserInfo(HttpServletRequest request) { - String userId = (String) request.getAttribute("user_id"); - UserVo userVo = userService.getUserInfo(userId); - return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), userVo); - } - - @GetMapping("/info") - @ApiOperation("获取用户的详细信息,包括个人信息页面和操作权限") - // 获取用户信息的接口 -ResultVO getInfo(HttpServletRequest request) { - // 打印进入接口的日志 - System.out.println("进入/user/info的获取用户信息的接口"); - // 获取用户ID - String userId = (String) request.getAttribute("user_id"); - // 调用userService获取用户信息 - UserInfoVo userInfoVo = userService.getInfo(userId); - // 返回结果 - return new ResultVO<>(ResultEnum.GET_INFO_SUCCESS.getCode(), ResultEnum.GET_INFO_SUCCESS.getMessage(), userInfoVo); - } - - @GetMapping("/test") - @ApiOperation("测试接口") - String test(HttpServletRequest request) { - // 下面这两个属性都是登录拦截器从token中解析地,当用户名不对或者token过期时是走不到接口内的 - String userId = (String) request.getAttribute("user_id"); - String username = (String) request.getAttribute("username"); - System.out.println("用户id:" + userId); - System.out.println("用户名:" + username); - return "用户id:" + userId + "\n用户名:" + username; +// 使用Spring Boot框架 +@SpringBootApplication +public class ExamApplication { + + // 主方法 + public static void main(String[] args) { + // 运行Spring Boot应用程序 + SpringApplication.run(ExamApplication.class, args); } } \ No newline at end of file diff --git a/backend/src/main/java/lsgwr/exam/service/impl/ExamServiceImpl.java b/backend/src/main/java/lsgwr/exam/service/impl/ExamServiceImpl.java index e58d825..9dd9a7e 100644 --- a/backend/src/main/java/lsgwr/exam/service/impl/ExamServiceImpl.java +++ b/backend/src/main/java/lsgwr/exam/service/impl/ExamServiceImpl.java @@ -23,22 +23,31 @@ import java.util.*; @Transactional public class ExamServiceImpl implements ExamService { + // 定义一个ExamRepository类型的私有变量 private final ExamRepository examRepository; + // 定义一个ExamRecordRepository类型的私有变量 private final ExamRecordRepository examRecordRepository; + // 定义一个QuestionRepository类型的私有变量 private final QuestionRepository questionRepository; + // 定义一个UserRepository类型的私有变量 private final UserRepository userRepository; + // 定义一个QuestionLevelRepository类型的私有变量 private final QuestionLevelRepository questionLevelRepository; + // 定义一个QuestionTypeRepository类型的私有变量 private final QuestionTypeRepository questionTypeRepository; + // 定义一个QuestionCategoryRepository类型的私有变量 private final QuestionCategoryRepository questionCategoryRepository; + // 定义一个QuestionOptionRepository类型的私有变量 private final QuestionOptionRepository questionOptionRepository; + // 构造函数,传入多个Repository类型的参数 public ExamServiceImpl(QuestionRepository questionRepository, UserRepository userRepository, QuestionLevelRepository questionLevelRepository, QuestionTypeRepository questionTypeRepository, QuestionCategoryRepository questionCategoryRepository, QuestionOptionRepository questionOptionRepository, ExamRepository examRepository, ExamRecordRepository examRecordRepository) { this.questionRepository = questionRepository; this.userRepository = userRepository; @@ -67,7 +76,9 @@ public class ExamServiceImpl implements ExamService { return questionVoList; } + // 根据传入的Question对象,返回一个QuestionVo对象 private QuestionVo getQuestionVo(Question question) { + // 创建一个新的QuestionVo对象 QuestionVo questionVo = new QuestionVo(); // 先复制能复制的属性 BeanUtils.copyProperties(question, questionVo);