From d896b56883449633fa43ea6cf5a915e6ff3e7266 Mon Sep 17 00:00:00 2001 From: Jan Tymel <410388@mail.muni.cz> Date: Thu, 21 Jan 2021 08:35:19 +0100 Subject: [PATCH] Add API for questions update --- ...tiveTrainingDefinitionsRestController.java | 17 +++++ .../example/demo/dto/QuestionUpdateDto.java | 73 +++++++++++++++++++ .../com/example/demo/mapper/BeanMapper.java | 3 + .../demo/service/LevelOperationsService.java | 7 ++ .../demo/service/QuestionChoiceService.java | 6 +- .../example/demo/service/QuestionService.java | 23 ++++++ 6 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/example/demo/dto/QuestionUpdateDto.java diff --git a/src/main/java/com/example/demo/controller/AdaptiveTrainingDefinitionsRestController.java b/src/main/java/com/example/demo/controller/AdaptiveTrainingDefinitionsRestController.java index 18fbee38..3ba8c1b8 100644 --- a/src/main/java/com/example/demo/controller/AdaptiveTrainingDefinitionsRestController.java +++ b/src/main/java/com/example/demo/controller/AdaptiveTrainingDefinitionsRestController.java @@ -5,6 +5,7 @@ import com.example.demo.dto.PhaseLevelUpdateDto; import com.example.demo.dto.QuestionChoiceDto; import com.example.demo.dto.QuestionChoiceUpdateDto; import com.example.demo.dto.QuestionDto; +import com.example.demo.dto.QuestionUpdateDto; import com.example.demo.dto.TaskUpdateDto; import com.example.demo.dto.InfoLevelUpdateDto; import com.example.demo.enums.LevelType; @@ -214,6 +215,22 @@ public class AdaptiveTrainingDefinitionsRestController { return levelOperationsService.createQuestionChoice(questionId); } + @ApiOperation(httpMethod = "PUT", + value = "Update question", + nickname = "updateQuestion", + consumes = MediaType.APPLICATION_JSON_VALUE + ) + @ApiResponses(value = { + @ApiResponse(code = 200, message = "Question updated"), + @ApiResponse(code = 500, message = "Unexpected application error") + }) + @PutMapping(path = "/questions") + public void updateQuestion( + @ApiParam(value = "Question to be updated") @RequestBody QuestionUpdateDto questionUpdateDto) { + + levelOperationsService.updateQuestion(questionUpdateDto); + } + @ApiOperation(httpMethod = "PUT", value = "Update question choice", nickname = "updateQuestionChoice", diff --git a/src/main/java/com/example/demo/dto/QuestionUpdateDto.java b/src/main/java/com/example/demo/dto/QuestionUpdateDto.java new file mode 100644 index 00000000..c81e9fd3 --- /dev/null +++ b/src/main/java/com/example/demo/dto/QuestionUpdateDto.java @@ -0,0 +1,73 @@ +package com.example.demo.dto; + +import com.example.demo.enums.QuestionType; + +public class QuestionUpdateDto { + + private Long id; + private QuestionType questionType; + private String text; + private Integer points; + private Integer penalty; + private boolean required; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public QuestionType getQuestionType() { + return questionType; + } + + public void setQuestionType(QuestionType questionType) { + this.questionType = questionType; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Integer getPoints() { + return points; + } + + public void setPoints(Integer points) { + this.points = points; + } + + public Integer getPenalty() { + return penalty; + } + + public void setPenalty(Integer penalty) { + this.penalty = penalty; + } + + public boolean isRequired() { + return required; + } + + public void setRequired(boolean required) { + this.required = required; + } + + @Override + public String toString() { + return "QuestionUpdateDto{" + + "id=" + id + + ", questionType=" + questionType + + ", text='" + text + '\'' + + ", points=" + points + + ", penalty=" + penalty + + ", required=" + required + + '}'; + } +} diff --git a/src/main/java/com/example/demo/mapper/BeanMapper.java b/src/main/java/com/example/demo/mapper/BeanMapper.java index 62249a1c..1dd4aae9 100644 --- a/src/main/java/com/example/demo/mapper/BeanMapper.java +++ b/src/main/java/com/example/demo/mapper/BeanMapper.java @@ -22,6 +22,7 @@ import com.example.demo.dto.PhaseLevelUpdateDto; import com.example.demo.dto.QuestionChoiceDto; import com.example.demo.dto.QuestionChoiceUpdateDto; import com.example.demo.dto.QuestionDto; +import com.example.demo.dto.QuestionUpdateDto; import com.example.demo.dto.QuestionnaireLevelDto; import com.example.demo.dto.TaskCreateDto; import com.example.demo.dto.TaskDto; @@ -135,6 +136,8 @@ public interface BeanMapper { Question toEntity(QuestionDto questionDto); + Question toEntity(QuestionUpdateDto questionUpdateDto); + QuestionDto toDto(Question question); QuestionnaireLevel toEntity(QuestionnaireLevelDto questionnaireLevelDto); diff --git a/src/main/java/com/example/demo/service/LevelOperationsService.java b/src/main/java/com/example/demo/service/LevelOperationsService.java index c92f4f1e..f533d998 100644 --- a/src/main/java/com/example/demo/service/LevelOperationsService.java +++ b/src/main/java/com/example/demo/service/LevelOperationsService.java @@ -2,6 +2,7 @@ package com.example.demo.service; import com.example.demo.domain.BaseLevel; import com.example.demo.domain.PhaseLevel; +import com.example.demo.domain.Question; import com.example.demo.domain.QuestionChoice; import com.example.demo.domain.Task; import com.example.demo.domain.InfoLevel; @@ -10,6 +11,7 @@ import com.example.demo.dto.PhaseLevelUpdateDto; import com.example.demo.dto.QuestionChoiceDto; import com.example.demo.dto.QuestionChoiceUpdateDto; import com.example.demo.dto.QuestionDto; +import com.example.demo.dto.QuestionUpdateDto; import com.example.demo.dto.TaskDto; import com.example.demo.dto.TaskUpdateDto; import com.example.demo.dto.InfoLevelUpdateDto; @@ -154,6 +156,11 @@ public class LevelOperationsService { return createdQuestionChoice; } + public void updateQuestion(QuestionUpdateDto questionUpdateDto) { + Question question = BeanMapper.INSTANCE.toEntity(questionUpdateDto); + questionService.updateQuestion(question); + } + public void updateQuestionChoice(QuestionChoiceUpdateDto questionChoiceUpdateDto) { QuestionChoice questionChoice = BeanMapper.INSTANCE.toEntity(questionChoiceUpdateDto); questionChoiceService.updateQuestionChoice(questionChoice); diff --git a/src/main/java/com/example/demo/service/QuestionChoiceService.java b/src/main/java/com/example/demo/service/QuestionChoiceService.java index c97e4b08..c55dbbdd 100644 --- a/src/main/java/com/example/demo/service/QuestionChoiceService.java +++ b/src/main/java/com/example/demo/service/QuestionChoiceService.java @@ -2,11 +2,7 @@ package com.example.demo.service; import com.example.demo.domain.Question; import com.example.demo.domain.QuestionChoice; -import com.example.demo.domain.QuestionnaireLevel; -import com.example.demo.domain.Task; import com.example.demo.dto.QuestionChoiceDto; -import com.example.demo.dto.QuestionDto; -import com.example.demo.dto.TaskDto; import com.example.demo.mapper.BeanMapper; import com.example.demo.repository.QuestionChoiceRepository; import com.example.demo.repository.QuestionRepository; @@ -20,7 +16,7 @@ import java.util.Optional; @Service public class QuestionChoiceService { - private static final Logger LOG = LoggerFactory.getLogger(TaskService.class); + private static final Logger LOG = LoggerFactory.getLogger(QuestionChoiceService.class); @Autowired private QuestionRepository questionRepository; diff --git a/src/main/java/com/example/demo/service/QuestionService.java b/src/main/java/com/example/demo/service/QuestionService.java index 443a8cae..a43bbc9c 100644 --- a/src/main/java/com/example/demo/service/QuestionService.java +++ b/src/main/java/com/example/demo/service/QuestionService.java @@ -7,6 +7,8 @@ import com.example.demo.enums.QuestionType; import com.example.demo.mapper.BeanMapper; import com.example.demo.repository.QuestionRepository; import com.example.demo.repository.QuestionnaireLevelRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -15,6 +17,8 @@ import java.util.Optional; @Service public class QuestionService { + private static final Logger LOG = LoggerFactory.getLogger(QuestionService.class); + @Autowired private QuestionnaireLevelRepository questionnaireLevelRepository; @@ -38,4 +42,23 @@ public class QuestionService { return BeanMapper.INSTANCE.toDto(persistedEntity); } + + public QuestionDto updateQuestion(Question question) { + Optional<Question> persistedQuestion = questionRepository.findById(question.getId()); + + if (persistedQuestion.isEmpty()) { + // TODO return 404 + LOG.error("No question found with ID {}.", question.getId()); + return new QuestionDto(); + } + + question.setChoices(persistedQuestion.get().getChoices()); + question.setQuestionnaireLevel(persistedQuestion.get().getQuestionnaireLevel()); + question.setOrder(persistedQuestion.get().getOrder()); + + Question savedEntity = questionRepository.save(question); + + return BeanMapper.INSTANCE.toDto(savedEntity); + } + } -- GitLab