Class AnswerController
java.lang.Object
it.unimib.unimibmodules.controller.DTOMapping<M,T>
it.unimib.unimibmodules.controller.DTOListMapping<Answer,AnswerDTO>
it.unimib.unimibmodules.controller.AnswerController
@RestController
@RequestMapping("/api")
public class AnswerController
extends DTOListMapping<Answer,AnswerDTO>
Controller handling HTTP requests related to Answer and ClosedEndedAnswer.
- Version:
- 0.4.1
- Author:
- Davide Costantini
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AnswerRepository
Instance of AnswerRepository that will be used to access the db.private final CloseEndedAnswerRepositoryReadOnly
Instance of AnswerRepository that will be used to access the db.private static final org.apache.logging.log4j.Logger
private final MailService
Instance of MailService, that will be used to send an email when the answers are saved.private final PdfService
Instance of PdfService that will be used to create the pdf.private final QuestionRepositoryReadOnly
Instance of AnswerRepository that will be used to access the db.private final SurveyRepositoryReadOnly
Instance of AnswerRepository that will be used to access the db.private final UserRepositoryReadOnly
Instance of AnswerRepository that will be used to access the db.Fields inherited from class it.unimib.unimibmodules.controller.DTOMapping
modelMapper
-
Constructor Summary
ConstructorsConstructorDescriptionAnswerController
(AnswerRepository answerRepository, UserRepositoryReadOnly userRepository, SurveyRepositoryReadOnly surveyRepository, QuestionRepositoryReadOnly questionRepository, CloseEndedAnswerRepositoryReadOnly closeEndedAnswerRepository, MailService mailService, PdfService pdfService, org.modelmapper.ModelMapper modelMapper) -
Method Summary
Modifier and TypeMethodDescriptionorg.springframework.http.ResponseEntity<String>
Creates an Answer.org.springframework.http.ResponseEntity<String>
cleanSurveyAnswers
(int surveyId, int userId) Cleans all the registered answers made by the user identified byuserId
on the survey identified bysurveyId
.convertListToDTO
(Iterable<Answer> answers) Converts a list of Answers to a list of AnswerDTOconvertToDTO
(Answer answer) Converts an instance of Answer to an instance of AnswerDTOconvertToEntity
(AnswerDTO answerDTO) Converts an instance of AnswerDTO to an instance of Answerorg.springframework.http.ResponseEntity<String>
deleteAnswer
(int id) Deletes the answer associated with the givenid
.org.springframework.http.ResponseEntity<AnswerDTO>
findAnswer
(int id) Finds the Answer associated with the givenid
.findSurveyAnswersForUser
(int surveyId, int userId) Finds all the Answer the User associated withuserId
has created for the Survey associated withsurveyId
.org.springframework.http.ResponseEntity<byte[]>
generatePdf
(int surveyId, int userId) Create a pdf of the Answer in the survey by the usersurveyId
.org.springframework.http.ResponseEntity<String>
modifyAnswer
(AnswerDTO modifiedAnswerDTO) Modifies an Answer using the values ofmodifiedAnswerDTO
.org.springframework.http.ResponseEntity<String>
saveModifiedAnswers
(int surveyId, int userId) Modifies and deletes the registered answers made by the user identified byuserId
on the survey identified bysurveyId
.org.springframework.http.ResponseEntity<String>
saveNewAnswers
(int surveyId, int userId) Inserts the registered answers made by the user identified byuserId
on the survey identified bysurveyId
.
-
Field Details
-
logger
private static final org.apache.logging.log4j.Logger logger -
answerRepository
Instance of AnswerRepository that will be used to access the db. -
userRepository
Instance of AnswerRepository that will be used to access the db. -
surveyRepository
Instance of AnswerRepository that will be used to access the db. -
questionRepository
Instance of AnswerRepository that will be used to access the db. -
closeEndedAnswerRepository
Instance of AnswerRepository that will be used to access the db. -
mailService
Instance of MailService, that will be used to send an email when the answers are saved. -
pdfService
Instance of PdfService that will be used to create the pdf.
-
-
Constructor Details
-
AnswerController
@Autowired public AnswerController(AnswerRepository answerRepository, UserRepositoryReadOnly userRepository, SurveyRepositoryReadOnly surveyRepository, QuestionRepositoryReadOnly questionRepository, CloseEndedAnswerRepositoryReadOnly closeEndedAnswerRepository, MailService mailService, PdfService pdfService, org.modelmapper.ModelMapper modelMapper)
-
-
Method Details
-
findAnswer
@GetMapping(path="/findAnswer/{id}") public org.springframework.http.ResponseEntity<AnswerDTO> findAnswer(@PathVariable int id) throws NotFoundException Finds the Answer associated with the givenid
.- Parameters:
id
- the id of the answer- Returns:
- an HTTP response with status 200 and the AnswerDTO if the answer has been found, 500 otherwise
- Throws:
NotFoundException
- if no answer identified byid
has been found
-
findSurveyAnswersForUser
@GetMapping(path="/findSurveyAnswersForUser") public org.springframework.http.ResponseEntity<List<AnswerDTO>> findSurveyAnswersForUser(@RequestParam int surveyId, @RequestParam int userId) throws NotFoundException Finds all the Answer the User associated withuserId
has created for the Survey associated withsurveyId
.- Parameters:
surveyId
- the id of the surveyuserId
- the id of the user- Returns:
- an HTTP response with status 200 and the AnswerDTO if the answer has been found, 500 otherwise
- Throws:
NotFoundException
- if no answer for the survey identified bysurveyId
and created by the user identified byuserId
has been found
-
generatePdf
@GetMapping(path="/generatePdf", produces="application/pdf") public org.springframework.http.ResponseEntity<byte[]> generatePdf(@RequestParam int surveyId, @RequestParam int userId) throws NotFoundException, com.itextpdf.text.DocumentException, IOException Create a pdf of the Answer in the survey by the usersurveyId
.- Parameters:
surveyId
- the id of the surveyuserId
- the id of the user- Returns:
- an HTTP response with status 200 and the pdf if the pdf was created, 500 otherwise
- Throws:
NotFoundException
- if no answer for the survey identified bysurveyId
and created by the user identified byuserId
has been foundcom.itextpdf.text.DocumentException
IOException
-
addAnswer
@PostMapping(path="/addAnswer", produces="application/json") public org.springframework.http.ResponseEntity<String> addAnswer(@RequestBody AnswerDTO answerDTO) throws EmptyFieldException, NotFoundException, IncorrectSizeException Creates an Answer.- Parameters:
answerDTO
- the serialized object of the answer- Returns:
- an HTTP Response with status 201 if the answer has been created, 500 otherwise
- Throws:
EmptyFieldException
- if the answer text is emptyNotFoundException
- if some of the attributes of the new answer can't be found on databaseIncorrectSizeException
-
modifyAnswer
@PatchMapping(path="/modifyAnswer") public org.springframework.http.ResponseEntity<String> modifyAnswer(@RequestBody AnswerDTO modifiedAnswerDTO) throws NotFoundException, EmptyFieldException, IncorrectSizeException Modifies an Answer using the values ofmodifiedAnswerDTO
.- Parameters:
modifiedAnswerDTO
- the serialization of the modified answer- Returns:
- an HTTP response with status 200 if the answer has been modified, 500 otherwise
- Throws:
NotFoundException
- if the original Answer has not been foundEmptyFieldException
- if the answer text is emptyIncorrectSizeException
-
deleteAnswer
@DeleteMapping(path="/deleteAnswer/{id}") public org.springframework.http.ResponseEntity<String> deleteAnswer(@PathVariable int id) throws NotFoundException Deletes the answer associated with the givenid
.- Parameters:
id
- the id of the answer that will be deleted- Returns:
- an HTTP Response with status 200 if the answer has been deleted, 500 otherwise
- Throws:
NotFoundException
- if no answer identified byid
has been found
-
saveNewAnswers
@PostMapping(path="/saveSurveyAnswers") public org.springframework.http.ResponseEntity<String> saveNewAnswers(@RequestParam int surveyId, @RequestParam int userId) throws NotFoundException Inserts the registered answers made by the user identified byuserId
on the survey identified bysurveyId
.- Parameters:
surveyId
- the id of the surveyuserId
- the id of the user- Returns:
- an HTTP Response with status 200 if the answer has been deleted, 500 otherwise
- Throws:
NotFoundException
- if no user identified byid
has been found
-
saveModifiedAnswers
@PostMapping(path="/saveModifiedSurveyAnswers") public org.springframework.http.ResponseEntity<String> saveModifiedAnswers(@RequestParam int surveyId, @RequestParam int userId) Modifies and deletes the registered answers made by the user identified byuserId
on the survey identified bysurveyId
.- Parameters:
surveyId
- the id of the surveyuserId
- the id of the user- Returns:
- an HTTP Response with status 200 if the answer has been deleted, 500 otherwise
-
cleanSurveyAnswers
@DeleteMapping(path="/cleanSurveyAnswers") public org.springframework.http.ResponseEntity<String> cleanSurveyAnswers(@RequestParam int surveyId, @RequestParam int userId) Cleans all the registered answers made by the user identified byuserId
on the survey identified bysurveyId
.- Parameters:
surveyId
- the id of the surveyuserId
- the id of the user- Returns:
- an HTTP Response with status 200 if the Unit Of Work has been cleaned, 500 otherwise
-
convertToDTO
Converts an instance of Answer to an instance of AnswerDTO- Specified by:
convertToDTO
in classDTOMapping<Answer,
AnswerDTO> - Parameters:
answer
- an instance of Answer- Returns:
- an instance of AnswerDTO, containing the serialized data of answer
- See Also:
-
convertListToDTO
Converts a list of Answers to a list of AnswerDTO- Specified by:
convertListToDTO
in classDTOListMapping<Answer,
AnswerDTO> - Parameters:
answers
- the list of Answer- Returns:
- a list of QuestionDTO, containing the serialized data of answers
- See Also:
-
convertToEntity
public Answer convertToEntity(AnswerDTO answerDTO) throws EmptyFieldException, NotFoundException, IncorrectSizeException Converts an instance of AnswerDTO to an instance of Answer- Specified by:
convertToEntity
in classDTOMapping<Answer,
AnswerDTO> - Parameters:
answerDTO
- an instance of AnswerDTO- Returns:
- an instance of Answer, containing the deserialized data of answerDTO
- Throws:
EmptyFieldException
- when one of the required field is emptyNotFoundException
- when one of the queries failsIncorrectSizeException
- See Also:
-