Class 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 Details

    • logger

      private static final org.apache.logging.log4j.Logger logger
    • answerRepository

      private final AnswerRepository answerRepository
      Instance of AnswerRepository that will be used to access the db.
    • userRepository

      private final UserRepositoryReadOnly userRepository
      Instance of AnswerRepository that will be used to access the db.
    • surveyRepository

      private final SurveyRepositoryReadOnly surveyRepository
      Instance of AnswerRepository that will be used to access the db.
    • questionRepository

      private final QuestionRepositoryReadOnly questionRepository
      Instance of AnswerRepository that will be used to access the db.
    • closeEndedAnswerRepository

      private final CloseEndedAnswerRepositoryReadOnly closeEndedAnswerRepository
      Instance of AnswerRepository that will be used to access the db.
    • mailService

      private final MailService mailService
      Instance of MailService, that will be used to send an email when the answers are saved.
    • pdfService

      private final PdfService pdfService
      Instance of PdfService that will be used to create the pdf.
  • Constructor Details

  • 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 given id.
      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 by id 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 with userId has created for the Survey associated with surveyId.
      Parameters:
      surveyId - the id of the survey
      userId - 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 by surveyId and created by the user identified by userId 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 user surveyId.
      Parameters:
      surveyId - the id of the survey
      userId - 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 by surveyId and created by the user identified by userId has been found
      com.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 empty
      NotFoundException - if some of the attributes of the new answer can't be found on database
      IncorrectSizeException
    • modifyAnswer

      @PatchMapping(path="/modifyAnswer") public org.springframework.http.ResponseEntity<String> modifyAnswer(@RequestBody AnswerDTO modifiedAnswerDTO) throws NotFoundException, EmptyFieldException, IncorrectSizeException
      Modifies an Answer using the values of modifiedAnswerDTO.
      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 found
      EmptyFieldException - if the answer text is empty
      IncorrectSizeException
    • deleteAnswer

      @DeleteMapping(path="/deleteAnswer/{id}") public org.springframework.http.ResponseEntity<String> deleteAnswer(@PathVariable int id) throws NotFoundException
      Deletes the answer associated with the given id.
      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 by id 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 by userId on the survey identified by surveyId.
      Parameters:
      surveyId - the id of the survey
      userId - 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 by id 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 by userId on the survey identified by surveyId.
      Parameters:
      surveyId - the id of the survey
      userId - 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 by userId on the survey identified by surveyId.
      Parameters:
      surveyId - the id of the survey
      userId - the id of the user
      Returns:
      an HTTP Response with status 200 if the Unit Of Work has been cleaned, 500 otherwise
    • convertToDTO

      public AnswerDTO convertToDTO(Answer answer)
      Converts an instance of Answer to an instance of AnswerDTO
      Specified by:
      convertToDTO in class DTOMapping<Answer,AnswerDTO>
      Parameters:
      answer - an instance of Answer
      Returns:
      an instance of AnswerDTO, containing the serialized data of answer
      See Also:
    • convertListToDTO

      public List<AnswerDTO> convertListToDTO(Iterable<Answer> answers)
      Converts a list of Answers to a list of AnswerDTO
      Specified by:
      convertListToDTO in class DTOListMapping<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 class DTOMapping<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 empty
      NotFoundException - when one of the queries fails
      IncorrectSizeException
      See Also: