From 5affdde6f83adb3a7d616958aeada6f6540923e1 Mon Sep 17 00:00:00 2001 From: Dominika Zemanovicova <xzemanov@fi.muni.cz> Date: Thu, 23 Mar 2023 12:49:19 +0100 Subject: [PATCH] Add QuestionService --- .../question/QuestionService.java | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionService.java diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionService.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionService.java new file mode 100644 index 00000000..f30867aa --- /dev/null +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionService.java @@ -0,0 +1,61 @@ +package org.fuseri.moduleexercise.question; + +import jakarta.persistence.EntityNotFoundException; +import lombok.Getter; +import org.fuseri.moduleexercise.common.DomainService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +/** + * Represent a service for managing Question entities + */ +@Service +public class QuestionService extends DomainService<Question> { + + /** + * The repository instance used by this service + */ + @Getter + private final QuestionRepository repository; + + /** + * Construct a new instance of QuestionService with the specified repository + * + * @param repository the repository instance to be used by this service + */ + @Autowired + public QuestionService(QuestionRepository repository) { + this.repository = repository; + } + + /** + * Retrieve the Question entity with the specified ID + * + * @param id the ID of the Question entity to retrieve + * @return the Question entity with the specified ID + * @throws EntityNotFoundException if no Question entity exists with the specified ID + */ + @Transactional(readOnly = true) + public Question find(String id) { + return repository.findById(id) + .orElseThrow(() -> new EntityNotFoundException("Question '" + id + "' not found.")); + } + + /** + * Retrieve a page of Question entities associated with the specified exercise ID + * + * @param exerciseId the ID of the exercise to retrieve questions for + * @param page the page number to retrieve (0-indexed) + * @return a page of Question entities associated with the specified exercise ID + */ + @Transactional(readOnly = true) + public Page<Question> findByExerciseId(String exerciseId, int page) { + return repository.findByExerciseId( + exerciseId, + PageRequest.of(page, DomainService.DEFAULT_PAGE_SIZE)); + } + +} -- GitLab