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

Add API for phase update

Resolves #5
parent d87d4179
No related branches found
No related tags found
No related merge requests found
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.QuestionDto;
import com.example.demo.dto.TaskUpdateDto;
......@@ -129,6 +130,22 @@ public class AdaptiveTrainingDefinitionsRestController {
levelOperationsService.updateInfoLevel(infoLevelUpdateDto);
}
@ApiOperation(httpMethod = "PUT",
value = "Update phase",
nickname = "updatePhaseLevel",
consumes = MediaType.APPLICATION_JSON_VALUE
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Phase level"),
@ApiResponse(code = 500, message = "Unexpected application error")
})
@PutMapping(path = "/phases")
public void updatePhaseLevel(
@ApiParam(value = "Info level to be updated") @RequestBody PhaseLevelUpdateDto phaseLevelUpdateDto) {
levelOperationsService.updatePhaseLevel(phaseLevelUpdateDto);
}
@ApiOperation(httpMethod = "PUT",
value = "Update task",
nickname = "updateTask",
......
package com.example.demo.dto;
import java.util.List;
public class PhaseLevelUpdateDto {
private Long id;
private String title;
private Integer order;
private Integer allowedWrongFlags;
private Integer allowedCommands;
private Integer estimatedDuration;
private Long maxScore;
private List<DecisionMatrixRowDto> decisionMatrix;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
public Integer getAllowedWrongFlags() {
return allowedWrongFlags;
}
public void setAllowedWrongFlags(Integer allowedWrongFlags) {
this.allowedWrongFlags = allowedWrongFlags;
}
public Integer getAllowedCommands() {
return allowedCommands;
}
public void setAllowedCommands(Integer allowedCommands) {
this.allowedCommands = allowedCommands;
}
public Integer getEstimatedDuration() {
return estimatedDuration;
}
public void setEstimatedDuration(Integer estimatedDuration) {
this.estimatedDuration = estimatedDuration;
}
public Long getMaxScore() {
return maxScore;
}
public void setMaxScore(Long maxScore) {
this.maxScore = maxScore;
}
public List<DecisionMatrixRowDto> getDecisionMatrix() {
return decisionMatrix;
}
public void setDecisionMatrix(List<DecisionMatrixRowDto> decisionMatrix) {
this.decisionMatrix = decisionMatrix;
}
}
......@@ -18,6 +18,7 @@ import com.example.demo.dto.InfoLevelCreateDto;
import com.example.demo.dto.InfoLevelDto;
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.QuestionDto;
import com.example.demo.dto.QuestionnaireLevelDto;
......@@ -87,6 +88,8 @@ public interface BeanMapper {
PhaseLevel toEntity(PhaseLevelDto phaseLevel);
PhaseLevel toEntity(PhaseLevelUpdateDto phaseLevelUpdateDto);
AssessmentLevel toAssessmentLevel(GameDefinitionCreateDto gameDefinitionCreateDto);
InfoLevel toInfoLevel(GameDefinitionCreateDto gameDefinitionCreateDto);
......
package com.example.demo.service;
import com.example.demo.domain.BaseLevel;
import com.example.demo.domain.PhaseLevel;
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.QuestionDto;
import com.example.demo.dto.TaskDto;
......@@ -128,6 +130,11 @@ public class LevelOperationsService {
infoLevelService.updateInfoLevel(infoLevel);
}
public void updatePhaseLevel(PhaseLevelUpdateDto phaseLevelUpdateDto) {
PhaseLevel phaseLevel = BeanMapper.INSTANCE.toEntity(phaseLevelUpdateDto);
phaseLevelService.updatePhaseLevel(phaseLevel);
}
public void updateTask(TaskUpdateDto taskUpdateDto) {
Task task = BeanMapper.INSTANCE.toEntity(taskUpdateDto);
taskService.updateTask(task);
......
......@@ -6,14 +6,20 @@ import com.example.demo.dto.PhaseLevelDto;
import com.example.demo.mapper.BeanMapper;
import com.example.demo.repository.BaseLevelRepository;
import com.example.demo.repository.PhaseLevelRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.Optional;
@Service
public class PhaseLevelService {
private static final Logger LOG = LoggerFactory.getLogger(PhaseLevelService.class);
@Autowired
private PhaseLevelRepository phaseLevelRepository;
......@@ -42,4 +48,26 @@ public class PhaseLevelService {
return BeanMapper.INSTANCE.toDto(persistedEntity);
}
public PhaseLevelDto updatePhaseLevel(PhaseLevel phaseLevel) {
Optional<PhaseLevel> persistedPhaseLevel = phaseLevelRepository.findById(phaseLevel.getId());
if (persistedPhaseLevel.isEmpty()) {
// TODO return 404
LOG.error("No phase level found with ID {}.", phaseLevel.getId());
return new PhaseLevelDto();
}
phaseLevel.setTrainingDefinitionId(persistedPhaseLevel.get().getTrainingDefinitionId());
phaseLevel.setOrder(persistedPhaseLevel.get().getOrder());
phaseLevel.setSubLevels(persistedPhaseLevel.get().getSubLevels());
if (!CollectionUtils.isEmpty(phaseLevel.getDecisionMatrix())) {
phaseLevel.getDecisionMatrix().forEach(x -> x.setPhaseLevel(phaseLevel));
}
PhaseLevel savedEntity = phaseLevelRepository.save(phaseLevel);
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