Skip to content
Snippets Groups Projects
Commit f95a7ecc authored by Jan Tymel's avatar Jan Tymel
Browse files

Add API for update of question choices

parent f90ab117
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@ package com.example.demo.controller;
import com.example.demo.dto.BaseLevelDto;
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.TaskUpdateDto;
import com.example.demo.dto.InfoLevelUpdateDto;
......@@ -212,4 +213,20 @@ public class AdaptiveTrainingDefinitionsRestController {
return levelOperationsService.createQuestionChoice(questionId);
}
@ApiOperation(httpMethod = "PUT",
value = "Update question choice",
nickname = "updateQuestionChoice",
consumes = MediaType.APPLICATION_JSON_VALUE
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Question choice updated"),
@ApiResponse(code = 500, message = "Unexpected application error")
})
@PutMapping(path = "/question-choices")
public void updateQuestionChoice(
@ApiParam(value = "Question choice to be updated") @RequestBody QuestionChoiceUpdateDto questionChoiceUpdateDto) {
levelOperationsService.updateQuestionChoice(questionChoiceUpdateDto);
}
}
package com.example.demo.dto;
public class QuestionChoiceUpdateDto {
private Long id;
private String text;
private boolean correct;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public boolean isCorrect() {
return correct;
}
public void setCorrect(boolean correct) {
this.correct = correct;
}
@Override
public String toString() {
return "QuestionChoiceUpdateDto{" +
"id=" + id +
", text='" + text + '\'' +
", correct=" + correct +
'}';
}
}
......@@ -20,6 +20,7 @@ import com.example.demo.dto.InfoLevelUpdateDto;
import com.example.demo.dto.PhaseLevelDto;
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.QuestionnaireLevelDto;
import com.example.demo.dto.TaskCreateDto;
......@@ -128,6 +129,8 @@ public interface BeanMapper {
QuestionChoice toEntity(QuestionChoiceDto questionChoiceDto);
QuestionChoice toEntity(QuestionChoiceUpdateDto questionChoiceDto);
QuestionChoiceDto toDto(QuestionChoice questionChoice);
Question toEntity(QuestionDto questionDto);
......
......@@ -2,11 +2,13 @@ package com.example.demo.service;
import com.example.demo.domain.BaseLevel;
import com.example.demo.domain.PhaseLevel;
import com.example.demo.domain.QuestionChoice;
import com.example.demo.domain.Task;
import com.example.demo.domain.InfoLevel;
import com.example.demo.dto.BaseLevelDto;
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.TaskDto;
import com.example.demo.dto.TaskUpdateDto;
......@@ -151,4 +153,9 @@ public class LevelOperationsService {
return createdQuestionChoice;
}
public void updateQuestionChoice(QuestionChoiceUpdateDto questionChoiceUpdateDto) {
QuestionChoice questionChoice = BeanMapper.INSTANCE.toEntity(questionChoiceUpdateDto);
questionChoiceService.updateQuestionChoice(questionChoice);
}
}
......@@ -3,11 +3,15 @@ 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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -16,6 +20,8 @@ import java.util.Optional;
@Service
public class QuestionChoiceService {
private static final Logger LOG = LoggerFactory.getLogger(TaskService.class);
@Autowired
private QuestionRepository questionRepository;
......@@ -38,4 +44,22 @@ public class QuestionChoiceService {
return BeanMapper.INSTANCE.toDto(persistedEntity);
}
public QuestionChoiceDto updateQuestionChoice(QuestionChoice questionChoice) {
Optional<QuestionChoice> persistedQuestionChoice = questionChoiceRepository.findById(questionChoice.getId());
if (persistedQuestionChoice.isEmpty()) {
// TODO return 404
LOG.error("No question choice found with ID {}.", questionChoice.getId());
return new QuestionChoiceDto();
}
questionChoice.setQuestion(persistedQuestionChoice.get().getQuestion());
questionChoice.setOrder(persistedQuestionChoice.get().getOrder());
QuestionChoice savedEntity = questionChoiceRepository.save(questionChoice);
return BeanMapper.INSTANCE.toDto(savedEntity);
}
}
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