2025年4月6日17:25:07

master
qmstyle 2025-04-06 17:26:44 +08:00
parent f7e739b115
commit 007442f2b8
3 changed files with 89 additions and 1 deletions

View File

@ -2,19 +2,27 @@ package com.zhangmeng.online.exam.controller;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.zhangmeng.jwt.dto.LoginUser;
import com.zhangmeng.online.exam.dao.PaperDao;
import com.zhangmeng.online.exam.dao.QuestionDao;
import com.zhangmeng.online.exam.dao.SubjectDao;
import com.zhangmeng.online.exam.dao.UserDao;
import com.zhangmeng.online.exam.dto.Result;
import com.zhangmeng.online.exam.entity.Paper;
import com.zhangmeng.online.exam.entity.Question;
import com.zhangmeng.online.exam.entity.Subject;
import com.zhangmeng.online.exam.entity.User;
import com.zhangmeng.online.exam.utils.PageUtils;
import com.zhangmeng.online.exam.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;
@ -35,6 +43,12 @@ public class PaperController {
@Autowired
private QuestionDao questionDao;
@Autowired
private SubjectDao subjectDao;
@Autowired
private UserDao userDao;
@RequestMapping("/list")
public Result list(Integer pageNum, Integer pageSize) {
@ -109,4 +123,45 @@ public class PaperController {
}
return Result.success(resultList);
}
@RequestMapping("/save")
public Result save(@RequestParam @RequestBody Map<String, Object> params) {
String id = (String) params.get("id");
String name = (String) params.get("name");
String description = (String) params.get("description");
Integer totalScore = Integer.parseInt( params.get("score").toString());
Integer passScore = Integer.parseInt( params.get("passScore").toString());
Integer examTime = Integer.parseInt( params.get("examTime").toString());
Integer status = Integer.parseInt( params.get("status").toString());
String subjectId = (String) params.get("subjectId");
Subject subject = null;
if (subjectId != null && !subjectId.equals("")){
subject = this.subjectDao.findById(Long.valueOf(subjectId)).get();
}
User creator = this.userDao.getById(UserUtils.getCurrentUserId());
Paper paper = null;
if (id != null && !id.isEmpty()){
paper = paperDao.getById(Long.valueOf(id));
}else {
paper = new Paper();
}
paper.setName(name);
paper.setDescription(description);
paper.setTotalScore(totalScore);
paper.setPassScore(passScore);
paper.setExamTime(examTime);
paper.setStatus(status);
paper.setSubject(subject);
paper.setCreator(creator);
paperDao.save(paper);
return Result.success(null);
}
}

View File

@ -0,0 +1,33 @@
package com.zhangmeng.online.exam.utils;
import com.zhangmeng.jwt.dto.LoginUser;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* @author zm
* @date 2025/4/6 16:21
* @version: 1.0
*/
public class UserUtils {
public static Long getCurrentUserId() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
LoginUser loginUser = null;
if (authentication != null) {
if (authentication instanceof AnonymousAuthenticationToken) {
return null;
}
if (authentication instanceof UsernamePasswordAuthenticationToken) {
loginUser = (LoginUser) authentication.getPrincipal();
}
}
if (loginUser != null){
return loginUser.getId();
}
return null;
}
}

View File

@ -1,5 +1,5 @@
server:
port: 8080
port: 8083
spring:
application:
name: online-exam