Skip to content
Snippets Groups Projects
Commit 7830eb65 authored by Dominika Zemanovičová's avatar Dominika Zemanovičová Committed by Martin Gargalovič
Browse files

Move functionality from AnswerController to AnswerFacade

parent bf5065b5
No related branches found
No related tags found
3 merge requests!31M2,!28M2 user,!27Draft: M2 user
package org.fuseri.moduleexercise.answer; package org.fuseri.moduleexercise.answer;
import jakarta.persistence.EntityNotFoundException;
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.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.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
...@@ -22,22 +17,11 @@ import java.util.List; ...@@ -22,22 +17,11 @@ import java.util.List;
@RequestMapping("/answers") @RequestMapping("/answers")
public class AnswerController { public class AnswerController {
private final AnswerService answerService; private final AnswerFacade facade;
private final QuestionService questionService;
private final AnswerMapper mapper;
/**
* Constructor for AnswerController
*
* @param answerService the service responsible for handling answer-related logic
* @param questionService the service responsible for handling question-related logic
* @param mapper the mapper responsible for converting between DTOs and entities
*/
@Autowired @Autowired
public AnswerController(AnswerService answerService, QuestionService questionService, AnswerMapper mapper) { public AnswerController(AnswerFacade facade) {
this.answerService = answerService; this.facade = facade;
this.questionService = questionService;
this.mapper = mapper;
} }
/** /**
...@@ -48,7 +32,7 @@ public class AnswerController { ...@@ -48,7 +32,7 @@ public class AnswerController {
*/ */
@GetMapping("/{question_id}") @GetMapping("/{question_id}")
public List<AnswerDto> findAllByQuestionId(@PathVariable("question_id") String questionId) { public List<AnswerDto> findAllByQuestionId(@PathVariable("question_id") String questionId) {
return mapper.toDtoList(answerService.findAllByQuestionId(questionId)); return facade.findAllByQuestionId(questionId);
} }
/** /**
...@@ -60,24 +44,7 @@ public class AnswerController { ...@@ -60,24 +44,7 @@ public class AnswerController {
*/ */
@PostMapping @PostMapping
public List<AnswerDto> createMultiple(@RequestBody AnswersCreateDto dto) { public List<AnswerDto> createMultiple(@RequestBody AnswersCreateDto dto) {
List<Answer> createdAnswers = new ArrayList<>(); return facade.createMultiple(dto);
for (var answerDto : dto.getAnswers()) {
Question question;
try {
question = questionService.find(dto.getQuestionId());
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Answer not found with id: " + dto.getQuestionId());
}
Answer answer = mapper.fromCreateDto(answerDto);
answer.setQuestionId(question.getId());
var createdAnswer = answerService.create(answer);
question.getAnswers().add(answer);
createdAnswers.add(createdAnswer);
}
return mapper.toDtoList(createdAnswers);
} }
/** /**
...@@ -88,26 +55,8 @@ public class AnswerController { ...@@ -88,26 +55,8 @@ public class AnswerController {
* @throws ResponseStatusException if the question id specified in the dto does not exist * @throws ResponseStatusException if the question id specified in the dto does not exist
*/ */
@PutMapping("/{id}") @PutMapping("/{id}")
public AnswerDto update(@PathVariable String id, AnswerCreateDto dto) { public AnswerDto update(@PathVariable String id, @RequestBody AnswerCreateDto dto) {
var updatedAnswer = mapper.fromCreateDto(dto); return facade.update(id, dto);
updatedAnswer.setId(id);
answerService.update(updatedAnswer);
Question question;
try {
question = questionService.find(dto.getQuestionId());
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Question not found with id: " + dto.getQuestionId());
}
var questionAnswers = question.getAnswers();
questionAnswers.removeIf(a -> a.getId().equals(id));
questionAnswers.add(updatedAnswer);
question.setAnswers(questionAnswers);
questionService.update(question);
return mapper.toDto(updatedAnswer);
} }
/** /**
...@@ -118,19 +67,6 @@ public class AnswerController { ...@@ -118,19 +67,6 @@ public class AnswerController {
*/ */
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public void delete(@PathVariable String id) { public void delete(@PathVariable String id) {
var answer = answerService.find(id); facade.delete(id);
Question question;
try {
question = questionService.find(answer.getQuestionId());
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Question not found with id: " + answer.getQuestionId());
}
var questionAnswers = question.getAnswers();
questionAnswers.removeIf(a -> a.getId().equals(answer.getId()));
question.setAnswers(questionAnswers);
answerService.delete(id);
} }
} }
package org.fuseri.moduleexercise.answer;
import jakarta.persistence.EntityNotFoundException;
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.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import java.util.ArrayList;
import java.util.List;
/**
* Represent facade for managing answers
* Provide simplified interface for manipulating with answers
*/
@Service
public class AnswerFacade {
private final AnswerService answerService;
private final QuestionService questionService;
private final AnswerMapper mapper;
/**
* Constructor for AnswerFacade
*
* @param answerService the service responsible for handling answer-related logic
* @param questionService the service responsible for handling question-related logic
* @param mapper the mapper responsible for converting between DTOs and entities
*/
@Autowired
public AnswerFacade(AnswerService answerService, QuestionService questionService, AnswerMapper mapper) {
this.answerService = answerService;
this.questionService = questionService;
this.mapper = mapper;
}
/**
* Retrieve a list of AnswerDto objects which belong to question with questionId
*
* @param questionId the ID of the question for which to retrieve answers
* @return a List of AnswerDto objects
*/
public List<AnswerDto> findAllByQuestionId(String questionId) {
return mapper.toDtoList(answerService.findAllByQuestionId(questionId));
}
/**
* 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
* @throws ResponseStatusException if the question ID specified in the dto does not exist
*/
public List<AnswerDto> createMultiple(@RequestBody AnswersCreateDto dto) {
List<Answer> createdAnswers = new ArrayList<>();
for (var answerDto : dto.getAnswers()) {
Question question;
try {
question = questionService.find(dto.getQuestionId());
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Answer not found with id: " + dto.getQuestionId());
}
Answer answer = mapper.fromCreateDto(answerDto);
answer.setQuestionId(question.getId());
var createdAnswer = answerService.create(answer);
question.getAnswers().add(answer);
createdAnswers.add(createdAnswer);
}
return mapper.toDtoList(createdAnswers);
}
/**
* Update an answer
*
* @param id of answer to update
* @param dto dto with updated answer information
* @throws ResponseStatusException if the question id specified in the dto does not exist
*/
public AnswerDto update(String id, AnswerCreateDto dto) {
var updatedAnswer = mapper.fromCreateDto(dto);
updatedAnswer.setId(id);
answerService.update(updatedAnswer);
Question question;
try {
question = questionService.find(dto.getQuestionId());
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Question not found with id: " + dto.getQuestionId());
}
var questionAnswers = question.getAnswers();
questionAnswers.removeIf(a -> a.getId().equals(id));
questionAnswers.add(updatedAnswer);
question.setAnswers(questionAnswers);
questionService.update(question);
return mapper.toDto(updatedAnswer);
}
/**
* Delete answer with the given id
*
* @param id of answer to delete
* @throws ResponseStatusException if the question id specified in the dto does not exist
*/
public void delete(String id) {
var answer = answerService.find(id);
Question question;
try {
question = questionService.find(answer.getQuestionId());
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND,
"Question not found with id: " + answer.getQuestionId());
}
var questionAnswers = question.getAnswers();
questionAnswers.removeIf(a -> a.getId().equals(answer.getId()));
question.setAnswers(questionAnswers);
answerService.delete(id);
}
}
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