From 5630b5f83fded0b4de967b9c6edfb9638d248b0c Mon Sep 17 00:00:00 2001 From: Dominika Zemanovicova <xzemanov@fi.muni.cz> Date: Sun, 16 Apr 2023 16:48:40 +0200 Subject: [PATCH] Delete createMultiple from AnswerController --- .../answer/AnswerController.java | 32 ++++--------------- .../moduleexercise/answer/AnswerFacade.java | 27 ---------------- 2 files changed, 6 insertions(+), 53 deletions(-) diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java index ebdc9cd6..696c3703 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerController.java @@ -8,15 +8,16 @@ import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerDto; -import org.fuseri.model.dto.exercise.AnswersCreateDto; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; import org.springframework.web.server.ResponseStatusException; -import java.util.List; - /** * Represent a REST API controller for answers * Handle HTTP requests related to answers @@ -32,27 +33,6 @@ public class AnswerController { this.facade = facade; } - /** - * Create a new answer for the given question ID - * - * @param dto the AnswerCreateDto object containing information about the answer to create - * @return a ResponseEntity containing an AnswerDto object representing the newly created answer, or a 404 Not Found response - * if the question with the specified ID in dto was not found - */ - @Operation(summary = "Create new answers for question", description = "Creates new answers for question.") - @ApiResponses(value = { - @ApiResponse(responseCode = "201", description = "Answers created successfully."), - @ApiResponse(responseCode = "400", description = "Invalid input.") - }) - @PostMapping - public ResponseEntity<List<AnswerDto>> createMultiple(@Valid @RequestBody AnswersCreateDto dto) { - try { - return ResponseEntity.status(HttpStatus.CREATED).body(facade.createMultiple(dto)); - } catch (EntityNotFoundException e) { - return ResponseEntity.notFound().build(); - } - } - /** * Update an answer * diff --git a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java index d0f88ecb..38165ddb 100644 --- a/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java +++ b/application/module-exercise/src/main/java/org/fuseri/moduleexercise/answer/AnswerFacade.java @@ -3,15 +3,10 @@ package org.fuseri.moduleexercise.answer; import jakarta.transaction.Transactional; import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerDto; -import org.fuseri.model.dto.exercise.AnswersCreateDto; import org.fuseri.moduleexercise.question.Question; import org.fuseri.moduleexercise.question.QuestionService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import org.springframework.web.bind.annotation.*; - -import java.util.ArrayList; -import java.util.List; /** * Represent facade for managing answers @@ -39,28 +34,6 @@ public class AnswerFacade { this.mapper = mapper; } - /** - * Create a new answer for the given question ID - * - * @param dto the AnswerCreateDto object containing information about the answer to create - * @return an AnswerDto object representing the newly created answer - */ - public List<AnswerDto> createMultiple(@RequestBody AnswersCreateDto dto) { - List<Answer> createdAnswers = new ArrayList<>(); - for (var answerDto : dto.getAnswers()) { - Question question; - question = questionService.find(dto.getQuestionId()); - - Answer answer = mapper.fromCreateDto(answerDto); - answer.setQuestion(question); - var createdAnswer = answerService.create(answer); - question.getAnswers().add(answer); - createdAnswers.add(createdAnswer); - } - - return mapper.toDtoList(createdAnswers); - } - /** * Update an answer * -- GitLab