瀏覽代碼

Fix exam start bug

qilikuacha 3 年之前
父節點
當前提交
2d95727e94

+ 1 - 1
src/main/java/com/jtxt/demo/config/MongoConfig.java

@@ -22,7 +22,7 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
 
     @Override
     public MongoClient mongoClient() {
-        ConnectionString connectionString = new ConnectionString("mongodb://jtxt-admin:jtxt-admin@120.77.221.199:27017/?authSource=jtxt-dev");
+        ConnectionString connectionString = new ConnectionString("mongodb://jtxt-admin:jtxt-admin@localhost:27017/?authSource=jtxt-dev");
         MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                 .applyConnectionString(connectionString)
                 .build();

+ 4 - 1
src/main/java/com/jtxt/demo/controller/ExamController.java

@@ -63,7 +63,10 @@ public class ExamController {
         if (exam == null) {
             return new ResponseEntity<>(HttpStatus.NOT_FOUND);
         }
-
+        List<String> questionList = examService.startExam(exam, user);
+        if (questionList == null) {
+            return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
+        }
         return new ResponseEntity<>(examService.startExam(exam, user), HttpStatus.OK);
     }
     @PostMapping("/exams/{examId}/end")

+ 12 - 5
src/main/java/com/jtxt/demo/service/ExamServiceImpl.java

@@ -124,11 +124,18 @@ public class ExamServiceImpl implements ExamService {
 
     @Override
     public List<String> startExam(Exam exam, User user) {
-        ExamHistory examHistory = new ExamHistory();
-        examHistory.setExamId(exam.getId());
-        examHistory.setStartTime(LocalDateTime.now(CHINA_ZONE));
-        examHistory.setUserId(user.getUserName());
-        examHistoryRepository.insert(examHistory);
+        ExamHistory examHistoryInDb = examHistoryRepository.findByUserIdAndExamId(user.getUserName(), exam.getId());
+        if (examHistoryInDb != null) {
+            if (examHistoryInDb.getEndTime() != null) {
+                return null; // exam has been finished.
+            }
+        } else {
+            ExamHistory examHistory = new ExamHistory();
+            examHistory.setExamId(exam.getId());
+            examHistory.setStartTime(LocalDateTime.now(CHINA_ZONE));
+            examHistory.setUserId(user.getUserName());
+            examHistoryRepository.insert(examHistory);
+        }
         return examToQuestionRepository
                 .findAllByExamId(exam.getId()).stream()
                 .map(x -> x.getQuestionId())

+ 1 - 1
src/main/resources/application.properties

@@ -1,4 +1,4 @@
-spring.data.mongodb.host=7000
+spring.data.mongodb.host=localhost
 spring.data.mongodb.database=jtxt-dev
 spring.data.mongodb.port=27017
 spring.data.mongodb.uri=mongodb://jtxt-admin:jtxt-admin@localhost:27017/?authSource=jtxt-dev