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

Add API for questions removal

parent d99672fa
No related branches found
No related tags found
No related merge requests found
......@@ -250,6 +250,21 @@ public class AdaptiveTrainingDefinitionsRestController {
levelOperationsService.updateQuestion(questionUpdateDto);
}
@ApiOperation(httpMethod = "DELETE",
value = "Delete a specified question",
nickname = "deleteQuestion",
produces = MediaType.APPLICATION_JSON_VALUE
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Question deleted"),
@ApiResponse(code = 500, message = "Unexpected application error")
})
@DeleteMapping(value = "/questions/{questionId}", produces = MediaType.APPLICATION_JSON_VALUE)
public void deleteQuestion(
@ApiParam(value = "Question ID", required = true) @PathVariable(name = "questionId") Long questionId) {
levelOperationsService.deleteQuestion(questionId);
}
@ApiOperation(httpMethod = "PUT",
value = "Update question choice",
nickname = "updateQuestionChoice",
......
......@@ -2,6 +2,7 @@ package com.example.demo.repository;
import com.example.demo.domain.Question;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
......@@ -9,4 +10,10 @@ public interface QuestionRepository extends JpaRepository<Question, Long> {
@Query("SELECT COALESCE(MAX(q.order), -1) FROM Question q WHERE q.questionnaireLevel.id = :questionnaireId")
Integer getCurrentMaxOrder(@Param("questionnaireId") Long questionnaireId);
@Modifying
@Query("UPDATE Question q SET q.order = q.order - 1 " +
"WHERE q.questionnaireLevel.id = :questionnaireLevelId " +
"AND q.order > :order ")
void decreaseOrderAfterQuestionWasDeleted(@Param("order") int order, @Param("questionnaireLevelId") Long questionnaireLevelId);
}
......@@ -97,6 +97,11 @@ public class LevelOperationsService {
baseLevelRepository.delete(levelEntity.get());
}
@Transactional
public void deleteQuestion(Long questionId) {
questionService.deleteQuestion(questionId);
}
@Transactional
public void deleteQuestionChoice(Long questionChoiceId) {
questionChoiceService.deleteQuestionChoice(questionChoiceId);
......
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.dto.QuestionDto;
import com.example.demo.enums.QuestionType;
......@@ -61,4 +62,17 @@ public class QuestionService {
return BeanMapper.INSTANCE.toDto(savedEntity);
}
public void deleteQuestion(Long questionId) {
Optional<Question> question = questionRepository.findById(questionId);
if (question.isEmpty()) {
// TODO throw a proper exception
return;
}
int questionOrder = question.get().getOrder();
questionRepository.decreaseOrderAfterQuestionWasDeleted(questionOrder, question.get().getQuestionnaireLevel().getId());
questionRepository.delete(question.get());
}
}
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