Skip to content
Snippets Groups Projects
Commit 5630b5f8 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová
Browse files

Delete createMultiple from AnswerController

parent 0642c062
No related branches found
No related tags found
3 merge requests!31M2,!30M2 exercise,!29M2 exercise
...@@ -8,15 +8,16 @@ import jakarta.validation.Valid; ...@@ -8,15 +8,16 @@ import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerCreateDto;
import org.fuseri.model.dto.exercise.AnswerDto; import org.fuseri.model.dto.exercise.AnswerDto;
import org.fuseri.model.dto.exercise.AnswersCreateDto;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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 org.springframework.web.server.ResponseStatusException;
import java.util.List;
/** /**
* Represent a REST API controller for answers * Represent a REST API controller for answers
* Handle HTTP requests related to answers * Handle HTTP requests related to answers
...@@ -32,27 +33,6 @@ public class AnswerController { ...@@ -32,27 +33,6 @@ public class AnswerController {
this.facade = facade; 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 * Update an answer
* *
......
...@@ -3,15 +3,10 @@ package org.fuseri.moduleexercise.answer; ...@@ -3,15 +3,10 @@ package org.fuseri.moduleexercise.answer;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.fuseri.model.dto.exercise.AnswerCreateDto; import org.fuseri.model.dto.exercise.AnswerCreateDto;
import org.fuseri.model.dto.exercise.AnswerDto; 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.Question;
import org.fuseri.moduleexercise.question.QuestionService; import org.fuseri.moduleexercise.question.QuestionService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
/** /**
* Represent facade for managing answers * Represent facade for managing answers
...@@ -39,28 +34,6 @@ public class AnswerFacade { ...@@ -39,28 +34,6 @@ public class AnswerFacade {
this.mapper = mapper; 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 * Update an answer
* *
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment