From 7526ab0c0342aa0d840f15638a99527d488780cc Mon Sep 17 00:00:00 2001 From: esoe Date: Tue, 19 Nov 2024 17:58:11 +0300 Subject: [PATCH] qq --- .../main/core/answer/AnswerEntity.java | 37 +++++++ .../main/core/answer/AnswerRepository.java | 12 +++ .../main/core/answer/AnswerService.java | 25 +++++ .../main/core/question/QuestionEntity.java | 1 - .../core/question/QuestionRepository.java | 4 + .../main/core/question/QuestionService.java | 8 ++ .../face/supplier/SupAnswerController.java | 23 +++++ .../face/supplier/SupCourseController.java | 92 ++++++++++++++++- .../main/face/user/UserQuizController.java | 32 +++++- .../static/content/courses/102/main.md | 21 ++++ .../templates/pages/supplier/answers.html | 34 +++++++ .../templates/pages/supplier/questions.html | 17 +++- .../resources/templates/pages/user/quiz.html | 2 +- .../resources/templates/shards/answers.html | 98 +++++++++++++++++++ .../resources/templates/shards/courses.html | 1 + .../resources/templates/shards/questions.html | 36 ++++--- .../resources/templates/shards/quizes.html | 17 +++- 17 files changed, 437 insertions(+), 23 deletions(-) create mode 100644 main/src/main/java/gsp/technologies/main/core/answer/AnswerEntity.java create mode 100644 main/src/main/java/gsp/technologies/main/core/answer/AnswerRepository.java create mode 100644 main/src/main/java/gsp/technologies/main/core/answer/AnswerService.java create mode 100644 main/src/main/java/gsp/technologies/main/face/supplier/SupAnswerController.java create mode 100644 main/src/main/resources/static/content/courses/102/main.md create mode 100644 main/src/main/resources/templates/pages/supplier/answers.html create mode 100644 main/src/main/resources/templates/shards/answers.html diff --git a/main/src/main/java/gsp/technologies/main/core/answer/AnswerEntity.java b/main/src/main/java/gsp/technologies/main/core/answer/AnswerEntity.java new file mode 100644 index 0000000..d9b1245 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/core/answer/AnswerEntity.java @@ -0,0 +1,37 @@ +package gsp.technologies.main.core.answer; + +import org.hibernate.annotations.OnDelete; +import org.hibernate.annotations.OnDeleteAction; + +import gsp.technologies.main.core.question.QuestionEntity; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +@Entity +public class AnswerEntity { + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private Long id; + private String body; + private Boolean correct; + private String description; + + @ManyToOne(fetch = FetchType.EAGER, optional = false) + @JoinColumn(name = "question", referencedColumnName = "id") + @OnDelete(action = OnDeleteAction.CASCADE) + private QuestionEntity question; + +} diff --git a/main/src/main/java/gsp/technologies/main/core/answer/AnswerRepository.java b/main/src/main/java/gsp/technologies/main/core/answer/AnswerRepository.java new file mode 100644 index 0000000..2aa0b45 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/core/answer/AnswerRepository.java @@ -0,0 +1,12 @@ +package gsp.technologies.main.core.answer; + +import java.util.Collection; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface AnswerRepository extends JpaRepository { + + Collection findAllByQuestionId(Long qid); +} diff --git a/main/src/main/java/gsp/technologies/main/core/answer/AnswerService.java b/main/src/main/java/gsp/technologies/main/core/answer/AnswerService.java new file mode 100644 index 0000000..3e72067 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/core/answer/AnswerService.java @@ -0,0 +1,25 @@ +package gsp.technologies.main.core.answer; + +import java.util.Collection; + +import org.springframework.stereotype.Service; + +@Service +public class AnswerService { + private final AnswerRepository repo; + + public AnswerService(AnswerRepository repository) { + this.repo = repository; + } + + public AnswerEntity save(AnswerEntity entity) { + return repo.save(entity); + } + + + public Collection findAllByQuestionId(Long qid) { + return repo.findAllByQuestionId(qid); + + } + +} diff --git a/main/src/main/java/gsp/technologies/main/core/question/QuestionEntity.java b/main/src/main/java/gsp/technologies/main/core/question/QuestionEntity.java index 917ab56..6883d7b 100644 --- a/main/src/main/java/gsp/technologies/main/core/question/QuestionEntity.java +++ b/main/src/main/java/gsp/technologies/main/core/question/QuestionEntity.java @@ -4,7 +4,6 @@ import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import gsp.technologies.main.core.course.CourseEntity; -import gsp.technologies.main.core.organization.OrganizationEntity; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; diff --git a/main/src/main/java/gsp/technologies/main/core/question/QuestionRepository.java b/main/src/main/java/gsp/technologies/main/core/question/QuestionRepository.java index c74ff9f..65ced95 100644 --- a/main/src/main/java/gsp/technologies/main/core/question/QuestionRepository.java +++ b/main/src/main/java/gsp/technologies/main/core/question/QuestionRepository.java @@ -1,9 +1,13 @@ package gsp.technologies.main.core.question; +import java.util.Collection; + import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface QuestionRepository extends JpaRepository { + Collection findQuestionsByCourseId(Long id); + } diff --git a/main/src/main/java/gsp/technologies/main/core/question/QuestionService.java b/main/src/main/java/gsp/technologies/main/core/question/QuestionService.java index 9386a8d..d339f1b 100644 --- a/main/src/main/java/gsp/technologies/main/core/question/QuestionService.java +++ b/main/src/main/java/gsp/technologies/main/core/question/QuestionService.java @@ -24,4 +24,12 @@ public class QuestionService { repo.deleteById(id); } + public Collection findQuestionsByCourseId(Long id) { + return repo.findQuestionsByCourseId(id); + } + + public QuestionEntity findById(Long qid) { + return repo.findById(qid).get(); + } + } diff --git a/main/src/main/java/gsp/technologies/main/face/supplier/SupAnswerController.java b/main/src/main/java/gsp/technologies/main/face/supplier/SupAnswerController.java new file mode 100644 index 0000000..d185804 --- /dev/null +++ b/main/src/main/java/gsp/technologies/main/face/supplier/SupAnswerController.java @@ -0,0 +1,23 @@ +package gsp.technologies.main.face.supplier; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import gsp.technologies.main.core.answer.AnswerService; + +@Controller +@RequestMapping(path = "/supplier") +public class SupAnswerController { + private static final Logger log = LoggerFactory.getLogger(SupAnswerController.class); + + @Autowired + private AnswerService answerService; + + public SupAnswerController(AnswerService answerService) { + this.answerService = answerService; + } + +} diff --git a/main/src/main/java/gsp/technologies/main/face/supplier/SupCourseController.java b/main/src/main/java/gsp/technologies/main/face/supplier/SupCourseController.java index b2b98a9..a59fed7 100644 --- a/main/src/main/java/gsp/technologies/main/face/supplier/SupCourseController.java +++ b/main/src/main/java/gsp/technologies/main/face/supplier/SupCourseController.java @@ -8,12 +8,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import gsp.technologies.main.core.account.AccountService; +import gsp.technologies.main.core.answer.AnswerEntity; +import gsp.technologies.main.core.answer.AnswerService; import gsp.technologies.main.core.course.CourseEntity; import gsp.technologies.main.core.course.CourseService; +import gsp.technologies.main.core.question.QuestionEntity; +import gsp.technologies.main.core.question.QuestionService; import org.springframework.web.bind.annotation.PostMapping; @@ -26,11 +31,19 @@ public class SupCourseController { @Autowired private CourseService courseService; + @Autowired + private QuestionService questionService; + + @Autowired + private AnswerService answerService; + @Autowired private AccountService accountService; - public SupCourseController(CourseService courseService, AccountService accountService) { + public SupCourseController(CourseService courseService, QuestionService questionService, AnswerService answerService, AccountService accountService) { this.courseService = courseService; + this.questionService = questionService; + this.answerService = answerService; this.accountService = accountService; } @@ -63,4 +76,81 @@ public class SupCourseController { return "redirect:/supplier/courses"; } + + @GetMapping("/courses/{id}/questions") + public String questions(@PathVariable(name = "id", required = true) Long id, + Model model) { + log.info("GET /supplier/courses/{}/questions", id); + + if (id == Long.parseLong("0")) { + model.addAttribute("courses", courseService.findAll()); + return "/pages/supplier/questions"; + } + + model.addAttribute("questions", questionService.findQuestionsByCourseId(id)); + + model.addAttribute("courses", courseService.findAll()); + + model.addAttribute("course", courseService.findById(id)); + return "/pages/supplier/questions"; + } + + @PostMapping("/courses/{id}/questions") + public String createQuestion(@PathVariable(name = "id", required = true) Long id, + @RequestParam(name = "question-body", required = true) String body) { + log.info("POST /supplier/courses/{}/questions", id); + QuestionEntity entity = QuestionEntity.builder() + .body(body) + .course(courseService.findById(id)) + .build(); + questionService.save(entity); + + return "redirect:/supplier/courses/" + id + "/questions"; + } + + @GetMapping("/courses/{id}/questions/{qid}/delete") + public String deleteQuestion(@PathVariable(name = "id", required = true) Long id, + @PathVariable(name = "qid", required = true) Long qid) { + log.info("GET /supplier/courses/{}/questions/{}/delete", id, qid); + questionService.delete(qid); + return "redirect:/supplier/courses/" + id + "/questions"; + } + + @GetMapping("/courses/{id}/questions/{qid}/answers") + public String answers(Model model, + @PathVariable(name = "id", required = true) Long id, + @PathVariable(name = "qid", required = true) Long qid) { + log.info("GET /supplier/courses/{}/questions/{}/answers", id, qid); + model.addAttribute("answers", answerService.findAllByQuestionId(qid)); + model.addAttribute("question", questionService.findById(qid)); + return "/pages/supplier/answers"; + } + + @PostMapping("/courses/{id}/questions/{qid}/answers") + public String answerCrate( + @PathVariable(name = "id", required = true) Long id, + @PathVariable(name = "qid", required = true) Long qid, + @RequestParam(name="body", required = true) String body, + @RequestParam(name="description", required = false) String description, + @RequestParam(name="correct", required = false) Boolean correct + ){ + log.info("POST /supplier/courses/{}/questions/{}/answers", id, qid); + + if (correct == null) correct = false; + if (description == null) description = ""; + + AnswerEntity entity = AnswerEntity.builder() + .body(body) + .correct(correct) + .description(description) + .question(questionService.findById(qid)) + .build(); + answerService.save(entity); + + return "redirect:/supplier/courses/" + + id + + "/questions/" + + qid + + "/answers"; + } } diff --git a/main/src/main/java/gsp/technologies/main/face/user/UserQuizController.java b/main/src/main/java/gsp/technologies/main/face/user/UserQuizController.java index e4e3d4a..7fcd33b 100644 --- a/main/src/main/java/gsp/technologies/main/face/user/UserQuizController.java +++ b/main/src/main/java/gsp/technologies/main/face/user/UserQuizController.java @@ -1,5 +1,8 @@ package gsp.technologies.main.face.user; +import java.util.ArrayList; +import java.util.Collection; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -10,17 +13,31 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import gsp.technologies.main.core.account.AccountService; +import gsp.technologies.main.core.answer.AnswerEntity; +import gsp.technologies.main.core.answer.AnswerService; +import gsp.technologies.main.core.question.QuestionEntity; +import gsp.technologies.main.core.question.QuestionService; @Controller @RequestMapping(path = "/user") public class UserQuizController { private static final Logger log = LoggerFactory.getLogger(UserQuizController.class); + @Autowired private AccountService accountService; - public UserQuizController(AccountService accountService) { + @Autowired + private QuestionService questionService; + + @Autowired + private AnswerService answerService; + + + public UserQuizController(AccountService accountService, QuestionService questionService, AnswerService answerService) { this.accountService = accountService; + this.questionService = questionService; + this.answerService = answerService; } @GetMapping("/quiz") @@ -34,7 +51,18 @@ public class UserQuizController { model.addAttribute("account", accountService.findById(Long.valueOf(accountid))); - + Collection questions = questionService.findQuestionsByCourseId(Long.parseLong(courseid)); + model.addAttribute("questions", questions); + + Collection answers = new ArrayList<>(); + for (QuestionEntity entity : questions) { + answers.addAll(answerService.findAllByQuestionId(entity.getId())); + } + + model.addAttribute("answers", answers); + + + // model.addAttribute("answers", answerService.findAnswersByCourseId(Long.parseLong(courseid))); return "pages/user/quiz"; } } diff --git a/main/src/main/resources/static/content/courses/102/main.md b/main/src/main/resources/static/content/courses/102/main.md new file mode 100644 index 0000000..d22f97a --- /dev/null +++ b/main/src/main/resources/static/content/courses/102/main.md @@ -0,0 +1,21 @@ +# Дефектоскопист. Разрушающий контроль + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. + +Lorem, ipsum dolor sit amet consectetur adipisicing elit. Omnis inventore eveniet sunt modi deserunt, officia saepe aut in architecto id eaque reiciendis ullam eos deleniti minima, facere asperiores temporibus necessitatibus. \ No newline at end of file diff --git a/main/src/main/resources/templates/pages/supplier/answers.html b/main/src/main/resources/templates/pages/supplier/answers.html new file mode 100644 index 0000000..0ed7e73 --- /dev/null +++ b/main/src/main/resources/templates/pages/supplier/answers.html @@ -0,0 +1,34 @@ + + + + + + exam-answers + + + +
+ +
+
+ + + + +
+ +
+ + +
    +
  • +
    +
  • +
+ + +
+
+
+ \ No newline at end of file diff --git a/main/src/main/resources/templates/pages/supplier/questions.html b/main/src/main/resources/templates/pages/supplier/questions.html index 2ea322a..4eaba4c 100644 --- a/main/src/main/resources/templates/pages/supplier/questions.html +++ b/main/src/main/resources/templates/pages/supplier/questions.html @@ -14,14 +14,25 @@ xmlns:th="http://www.thymeleaf.org"> -

Вопросы

+ +
+ -
+
-
+
+ \ No newline at end of file diff --git a/main/src/main/resources/templates/pages/user/quiz.html b/main/src/main/resources/templates/pages/user/quiz.html index 8422bb1..bafa6d0 100644 --- a/main/src/main/resources/templates/pages/user/quiz.html +++ b/main/src/main/resources/templates/pages/user/quiz.html @@ -21,7 +21,7 @@ xmlns:th="http://www.thymeleaf.org"> - +