From 42791baae93dd1af46970dd4c84f2ec27d4e76a0 Mon Sep 17 00:00:00 2001
From: Dominika Zemanovicova <xzemanov@fi.muni.cz>
Date: Thu, 23 Mar 2023 12:36:52 +0100
Subject: [PATCH] Add QuestionRepository and QuestionRepositoryImpl

---
 .../question/QuestionRepository.java          | 20 +++++++++++
 .../question/QuestionRepositoryImpl.java      | 36 +++++++++++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java
 create mode 100644 application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepositoryImpl.java

diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java
new file mode 100644
index 00000000..0f924cd5
--- /dev/null
+++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepository.java
@@ -0,0 +1,20 @@
+package org.fuseri.moduleexercise.question;
+
+import org.fuseri.moduleexercise.common.DomainRepository;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageRequest;
+
+/**
+ * A repository interface for managing Question entities
+ */
+public interface QuestionRepository extends DomainRepository<Question, String> {
+
+    /**
+     * Find a page of questions associated with the exercise with the specified ID
+     *
+     * @param exerciseId  the ID of the exercise to find questions for
+     * @param pageRequest the page request specifying the page number and page size
+     * @return a page of questions associated with the specified exercise
+     */
+    Page<Question> findByExerciseId(String exerciseId, PageRequest pageRequest);
+}
\ No newline at end of file
diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepositoryImpl.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepositoryImpl.java
new file mode 100644
index 00000000..7085bca8
--- /dev/null
+++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/question/QuestionRepositoryImpl.java
@@ -0,0 +1,36 @@
+package org.fuseri.moduleexercise.question;
+
+import org.fuseri.moduleexercise.common.DomainRepositoryImpl;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.PageImpl;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.stereotype.Repository;
+
+import java.util.List;
+
+/**
+ * An implementation of the QuestionRepository interface
+ * Provides access to Question entities stored in a data source
+ */
+@Repository
+public class QuestionRepositoryImpl extends DomainRepositoryImpl<Question, String> implements QuestionRepository {
+
+    /**
+     * Find a page of questions associated with the exercise with the specified ID
+     *
+     * @param exerciseId  the ID of the exercise to find questions for
+     * @param pageRequest the page request specifying the page number and page size
+     * @return a page of questions associated with the specified exercise
+     */
+    @Override
+    public Page<Question> findByExerciseId(String exerciseId, PageRequest pageRequest) {
+        List<Question> filteredQuestions = getItems()
+                .stream()
+                .filter(e -> e.getExerciseId().equals(exerciseId))
+                .skip(pageRequest.getOffset())
+                .limit(pageRequest.getPageSize())
+                .toList();
+
+        return new PageImpl<>(filteredQuestions, pageRequest, filteredQuestions.size());
+    }
+}
-- 
GitLab