From baf28a7fba34d6d6e9ed8f2ecf178642b021c336 Mon Sep 17 00:00:00 2001
From: Dominika Zemanovicova <xzemanov@fi.muni.cz>
Date: Sun, 9 Apr 2023 12:57:37 +0200
Subject: [PATCH] Add ExerciseFacade

---
 .../exercise/ExerciseController.java          |  22 ++--
 .../exercise/ExerciseFacade.java              | 105 ++++++++++++++++++
 2 files changed, 116 insertions(+), 11 deletions(-)
 create mode 100644 application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java

diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java
index 1cf3dfb4..2817ab64 100644
--- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java
+++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java
@@ -24,7 +24,7 @@ import org.springframework.web.server.ResponseStatusException;
 @RequestMapping("/exercises")
 public class ExerciseController {
 
-    private final ExerciseService service;
+    private final ExerciseFacade facade;
 
     private final ExerciseMapper mapper;
     private final QuestionMapper questionMapper;
@@ -32,12 +32,12 @@ public class ExerciseController {
     /**
      * Constructor for AnswerController
      *
-     * @param service the service responsible for handling exercise-related logic
+     * @param facade the facade responsible for handling exercise-related logic
      * @param mapper  the mapper responsible for converting between DTOs and entities
      */
     @Autowired
-    public ExerciseController(ExerciseService service, ExerciseMapper mapper, QuestionMapper questionMapper) {
-        this.service = service;
+    public ExerciseController(ExerciseFacade facade, ExerciseMapper mapper, QuestionMapper questionMapper) {
+        this.facade = facade;
         this.mapper = mapper;
         this.questionMapper = questionMapper;
     }
@@ -51,7 +51,7 @@ public class ExerciseController {
     @PostMapping
     public ExerciseDto create(@Valid @RequestBody ExerciseCreateDto dto) {
         Exercise exercise = mapper.fromCreateDto(dto);
-        var a = service.create(exercise);
+        var a = facade.create(exercise);
         return mapper.toDto(a);
     }
 
@@ -63,7 +63,7 @@ public class ExerciseController {
      */
     @GetMapping("/{id}")
     public ExerciseDto find(@NotNull @PathVariable long id) {
-        return mapper.toDto(service.find(id));
+        return mapper.toDto(facade.find(id));
     }
 
     /**
@@ -74,7 +74,7 @@ public class ExerciseController {
      */
     @GetMapping
     public Result<ExerciseDto> findAll(@PositiveOrZero @RequestParam int page) {
-        Page<Exercise> exercise = service.findAll(page);
+        Page<Exercise> exercise = facade.findAll(page);
         return mapper.toResult(exercise);
     }
 
@@ -90,7 +90,7 @@ public class ExerciseController {
     public Result<ExerciseDto> findPerDifficultyPerCourse(
             @PositiveOrZero @RequestParam int page, @NotNull @RequestParam long courseId,
             @PositiveOrZero @RequestParam int difficulty) {
-        Page<Exercise> exercise = service.findPerDifficultyPerCourse(page, courseId, difficulty);
+        Page<Exercise> exercise = facade.findPerDifficultyPerCourse(page, courseId, difficulty);
         return mapper.toResult(exercise);
     }
 
@@ -104,7 +104,7 @@ public class ExerciseController {
     @GetMapping("/{exercise-id}/questions")
     public Result<QuestionDto> findQuestions(@NotNull @PathVariable("exercise-id") long exerciseId,
                                                 @PositiveOrZero @RequestParam int page) {
-        Page<Question> questions = service.getQuestions(exerciseId, page);
+        Page<Question> questions = facade.getQuestions(exerciseId, page);
         return questionMapper.toResult(questions);
     }
 
@@ -123,7 +123,7 @@ public class ExerciseController {
         exercise.setId(id);
 
         try {
-            return mapper.toDto(service.update(exercise));
+            return mapper.toDto(facade.update(exercise));
         } catch (IllegalArgumentException e) {
             throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage());
         } catch (EntityNotFoundException e) {
@@ -138,7 +138,7 @@ public class ExerciseController {
      */
     @DeleteMapping("/{id}")
     public void delete(@NotNull @PathVariable long id) {
-        service.delete(id);
+        facade.delete(id);
     }
 
 }
diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java
new file mode 100644
index 00000000..e8f606ce
--- /dev/null
+++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseFacade.java
@@ -0,0 +1,105 @@
+package org.fuseri.moduleexercise.exercise;
+
+import jakarta.persistence.EntityNotFoundException;
+import jakarta.transaction.Transactional;
+import org.fuseri.moduleexercise.question.Question;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.Page;
+import org.springframework.stereotype.Service;
+
+/**
+ * Represent facade for managing exercises
+ * Provide simplified interface for manipulating with exercises
+ */
+@Service
+@Transactional
+public class ExerciseFacade {
+    private final ExerciseService exerciseService;
+
+    /**
+     * Constructor for AnswerFacade
+     *
+     * @param exerciseService   the service responsible for handling answer-related logic
+     */
+    @Autowired
+    public ExerciseFacade(ExerciseService exerciseService) {
+        this.exerciseService = exerciseService;
+    }
+
+    /**
+     * Create a new exercise
+     *
+     * @param exercise to create
+     * @return a created exercise
+     */
+    public Exercise create(Exercise exercise) {
+        return exerciseService.create(exercise);
+    }
+
+    /**
+     * Retrieve the Exercise entity with the specified ID
+     *
+     * @param id the ID of the Exercise entity to retrieve
+     * @return the Exercise entity with the specified ID
+     * @throws EntityNotFoundException if no Exercise entity exists with the specified ID
+     */
+    @org.springframework.transaction.annotation.Transactional(readOnly = true)
+    public Exercise find(long id) {
+        return exerciseService.find(id);
+    }
+
+    /**
+     * Retrieve a page of Exercise entities
+     *
+     * @param page the page number to retrieve (0-indexed)
+     * @return a page of Exercise entities
+     */
+    @org.springframework.transaction.annotation.Transactional(readOnly = true)
+    public Page<Exercise> findAll(int page) {
+        return exerciseService.findAll(page);
+    }
+
+    /**
+     * Retrieve a page of exercises filtered by the specified course id and difficulty level
+     *
+     * @param page       the page number to retrieve
+     * @param courseId   the id of the course to filter by
+     * @param difficulty the difficulty level to filter by
+     * @return a {@link Page} of {@link Exercise} objects filtered by the specified course id and difficulty level
+     */
+    public Page<Exercise> findPerDifficultyPerCourse(int page, long courseId, int difficulty) {
+        return exerciseService.findPerDifficultyPerCourse(page, courseId, difficulty);
+    }
+
+    /**
+     * 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
+     */
+    @org.springframework.transaction.annotation.Transactional(readOnly = true)
+    public Page<Question> getQuestions(long exerciseId, int page) {
+        return exerciseService.getQuestions(
+                exerciseId, page);
+    }
+
+    /**
+     * Update exercise
+     *
+     * @param exercise to update
+     * @return updated exercise
+     */
+    public Exercise update(Exercise exercise) {
+        return exerciseService.update(exercise);
+    }
+
+    /**
+     * Delete exercise
+     *
+     * @param id of exercise to delete
+     */
+    public void delete(long id) {
+        exerciseService.delete(id);
+    }
+}
-- 
GitLab