From e183edfe3f077634f4444e1a6fbf9c719654edc1 Mon Sep 17 00:00:00 2001 From: Dominika Zemanovicova <xzemanov@fi.muni.cz> Date: Thu, 23 Mar 2023 14:22:46 +0100 Subject: [PATCH] Add ExerciseController --- .../exercise/ExerciseController.java | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.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 new file mode 100644 index 00000000..5700b3b7 --- /dev/null +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/exercise/ExerciseController.java @@ -0,0 +1,70 @@ +package org.fuseri.moduleexercise.exercise; + +import org.fuseri.model.dto.common.Result; +import org.fuseri.model.dto.exercise.ExerciseCreateDto; +import org.fuseri.model.dto.exercise.ExerciseDto; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.web.bind.annotation.*; + +/** + * Represent a REST API controller for exercises + * Handle HTTP requests related to exercises + */ +@RestController +@RequestMapping("/exercises") +public class ExerciseController { + + private final ExerciseService service; + + private final ExerciseMapper mapper; + + /** + * Constructor for AnswerController + * + * @param service the service responsible for handling exercise-related logic + * @param mapper the mapper responsible for converting between DTOs and entities + */ + @Autowired + public ExerciseController(ExerciseService service, ExerciseMapper mapper) { + this.service = service; + this.mapper = mapper; + } + + /** + * Create a new answer for the given question ID + * + * @param dto the ExerciseCreateDto object containing information about the exercise to create + * @return an ExerciseDto object representing the newly created exercise + */ + @PostMapping + public ExerciseDto create(@RequestBody ExerciseCreateDto dto) { + Exercise withoutId = mapper.fromCreateDto(dto); + Exercise exercise = service.create(withoutId); + return mapper.toDto(exercise); + } + + /** + * Find an exercise by ID + * + * @param id the ID of the exercise to find + * @return an ExerciseDto object representing the found exercise + */ + @GetMapping("/{id}") + public ExerciseDto find(@PathVariable String id) { + return mapper.toDto(service.find(id)); + } + + /** + * Find exercises and return them in a paginated format + * + * @param page the page number of the exercises to retrieve + * @return a Result object containing a list of ExerciseDto objects and pagination information + */ + @GetMapping + public Result<ExerciseDto> findAll(@RequestParam int page) { + Page<Exercise> exercise = service.findAll(page); + return mapper.toResult(exercise); + } + +} \ No newline at end of file -- GitLab