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

Change REST API creating a new phase according to the new design

Related to #10
parent ec7606c0
No related branches found
No related tags found
No related merge requests found
......@@ -9,7 +9,6 @@ import com.example.demo.dto.QuestionUpdateDto;
import com.example.demo.dto.QuestionnaireUpdateDto;
import com.example.demo.dto.TaskUpdateDto;
import com.example.demo.dto.InfoLevelUpdateDto;
import com.example.demo.enums.LevelType;
import com.example.demo.enums.QuestionType;
import com.example.demo.service.LevelOperationsService;
import io.swagger.annotations.Api;
......@@ -29,7 +28,6 @@ import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
......@@ -81,25 +79,25 @@ public class AdaptiveTrainingDefinitionsRestController {
levelOperationsService.deleteLevel(levelId);
}
@ApiOperation(httpMethod = "POST",
value = "Create a new level",
notes = "Creates only default level with a specified type",
response = BaseLevelDto.class,
nickname = "createLevel",
produces = MediaType.APPLICATION_JSON_VALUE
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Level created"),
@ApiResponse(code = 500, message = "Unexpected application error")
})
@PostMapping(path = "/levels/{levelType}")
public BaseLevelDto createLevel(
@ApiParam(value = "Training definition ID", required = true) @RequestParam(name = "definitionId") Long definitionId,
@ApiParam(value = "Level type", allowableValues = "questionnaire, assessment, info, phase", required = true)
@PathVariable("levelType") LevelType levelType) {
return levelOperationsService.createLevel(definitionId, levelType);
}
// @ApiOperation(httpMethod = "POST",
// value = "Create a new level",
// notes = "Creates only default level with a specified type",
// response = BaseLevelDto.class,
// nickname = "createLevel",
// produces = MediaType.APPLICATION_JSON_VALUE
// )
// @ApiResponses(value = {
// @ApiResponse(code = 200, message = "Level created"),
// @ApiResponse(code = 500, message = "Unexpected application error")
// })
// @PostMapping(path = "/levels/{levelType}")
// public BaseLevelDto createLevel(
// @ApiParam(value = "Training definition ID", required = true) @RequestParam(name = "definitionId") Long definitionId,
// @ApiParam(value = "Level type", allowableValues = "questionnaire, assessment, info, phase", required = true)
// @PathVariable("levelType") LevelType levelType) {
//
// return levelOperationsService.createLevel(definitionId, levelType);
// }
@ApiOperation(httpMethod = "GET",
value = "Get level by ID",
......
package com.example.demo.controller;
import com.example.demo.dto.BaseLevelDto;
import com.example.demo.dto.PhaseCreateDTO;
import com.example.demo.service.LevelOperationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
@RequestMapping(value = "/training-definitions/{definitionId}/phases", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(value = "/training-definitions/{definitionId}/phases",
tags = "Phases",
consumes = MediaType.APPLICATION_JSON_VALUE,
authorizations = @Authorization(value = "bearerAuth"))
public class PhasesController {
@Autowired
private LevelOperationsService levelOperationsService;
@ApiOperation(httpMethod = "POST",
value = "Create a new phase",
notes = "Creates a new default phase with a specified type",
response = BaseLevelDto.class,
nickname = "createLevel",
produces = MediaType.APPLICATION_JSON_VALUE
)
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Phase created"),
@ApiResponse(code = 500, message = "Unexpected application error")
})
@PostMapping
public ResponseEntity<BaseLevelDto> createPhase(
@ApiParam(value = "Training definition ID", required = true)
@PathVariable(name = "definitionId") Long definitionId,
@ApiParam(value = "Level type", allowableValues = "questionnaire, info, game", required = true)
@RequestBody @Valid PhaseCreateDTO phaseCreateDTO) {
BaseLevelDto createdPhase = levelOperationsService.createLevel(definitionId, phaseCreateDTO);
return new ResponseEntity<>(createdPhase, HttpStatus.CREATED);
}
}
package com.example.demo.dto;
import com.example.demo.enums.LevelType;
import com.example.demo.enums.PhaseType;
import java.io.Serializable;
......@@ -11,7 +11,7 @@ public abstract class BaseLevelDto implements Serializable {
private Integer order;
private String estimatedDuration;
private Long maxScore;
private LevelType levelType;
private PhaseType phaseType;
private Integer allowedCommands;
private Integer allowedWrongFlags;
......@@ -55,12 +55,12 @@ public abstract class BaseLevelDto implements Serializable {
this.maxScore = maxScore;
}
public LevelType getLevelType() {
return levelType;
public PhaseType getPhaseType() {
return phaseType;
}
public void setLevelType(LevelType levelType) {
this.levelType = levelType;
public void setPhaseType(PhaseType phaseType) {
this.phaseType = phaseType;
}
public Integer getAllowedCommands() {
......@@ -87,7 +87,7 @@ public abstract class BaseLevelDto implements Serializable {
", order=" + order +
", estimatedDuration='" + estimatedDuration + '\'' +
", maxScore=" + maxScore +
", levelType=" + levelType +
", levelType=" + phaseType +
", allowedCommands=" + allowedCommands +
", allowedWrongFlags=" + allowedWrongFlags +
'}';
......
package com.example.demo.dto;
import com.example.demo.enums.PhaseType;
import io.swagger.annotations.ApiModelProperty;
import javax.validation.constraints.NotNull;
public class PhaseCreateDTO {
@ApiModelProperty(value = "Type of phase.", required = true, allowableValues = "QUESTIONNAIRE, INFO, GAME", example = "GAME")
@NotNull(message = "Phase type must be specified")
private PhaseType phaseType;
public PhaseType getPhaseType() {
return phaseType;
}
public void setPhaseType(PhaseType phaseType) {
this.phaseType = phaseType;
}
@Override
public String toString() {
return "PhaseCreateDTO{" +
"phaseType=" + phaseType +
'}';
}
}
package com.example.demo.dto.input;
import com.example.demo.dto.AttachmentDto;
import com.example.demo.enums.LevelType;
import com.example.demo.enums.PhaseType;
import java.util.List;
......@@ -10,7 +10,7 @@ public class GameDefinitionCreateDto {
private Long id;
private String title;
private Integer order;
private LevelType type;
private PhaseType type;
// assessment level fields
private String assessmentType;
......@@ -53,11 +53,11 @@ public class GameDefinitionCreateDto {
this.order = order;
}
public LevelType getType() {
public PhaseType getType() {
return type;
}
public void setType(LevelType type) {
public void setType(PhaseType type) {
this.type = type;
}
......
package com.example.demo.enums;
public enum LevelType {
assessment, // TODO deprecated, should be replaced by questionnaire
questionnaire,
task,
info,
phase
}
package com.example.demo.enums;
public enum PhaseType {
QUESTIONNAIRE,
task, // TODO remove once Task is not a subclass of BaseLevel
INFO,
GAME,
}
......@@ -59,12 +59,11 @@ public interface BeanMapper {
return baseLevelDto;
}
@Mapping(target = "levelType", constant = "assessment")
AssessmentLevelDto toDto(AssessmentLevel assessmentLevel);
AssessmentLevel toEntity(AssessmentLevelDto assessmentLevel);
@Mapping(target = "levelType", constant = "task")
@Mapping(target = "phaseType", constant = "task")
TaskDto toDto(Task task);
Task toEntity(TaskDto taskDto);
......@@ -73,7 +72,7 @@ public interface BeanMapper {
Task toEntity(TaskUpdateDto taskUpdateDto);
@Mapping(target = "levelType", constant = "info")
@Mapping(target = "phaseType", constant = "INFO")
InfoLevelDto toDto(InfoLevel infoLevel);
InfoLevel toEntity(InfoLevelDto infoLevel);
......@@ -86,7 +85,7 @@ public interface BeanMapper {
Attachment toEntity(AttachmentDto attachment);
@Mapping(target = "levelType", constant = "phase")
@Mapping(target = "phaseType", constant = "GAME")
PhaseLevelDto toDto(PhaseLevel phaseLevel);
PhaseLevel toEntity(PhaseLevelDto phaseLevel);
......@@ -113,16 +112,16 @@ public interface BeanMapper {
@Mapping(target = "subLevels", ignore = true)
PhaseLevel updatePhaseLevel(@MappingTarget PhaseLevel phaseLevel, GameDefinitionCreateDto gameDefinitionCreateDto);
@Mapping(target = "type", constant = "assessment")
// @Mapping(target = "type", constant = "assessment")
GameDefinitionCreateDto toLevelDefinitionDto(AssessmentLevel assessmentLevel);
@Mapping(target = "type", constant = "task")
// @Mapping(target = "type", constant = "task")
GameDefinitionCreateDto toLevelDefinitionDto(Task task);
@Mapping(target = "type", constant = "info")
// @Mapping(target = "type", constant = "info")
GameDefinitionCreateDto toLevelDefinitionDto(InfoLevel infoLevel);
@Mapping(target = "type", constant = "phase")
// @Mapping(target = "type", constant = "phase")
GameDefinitionCreateDto toLevelDefinitionDto(PhaseLevel phaseLevel);
DecisionMatrixRow toEntity(DecisionMatrixRowDto decisionMatrixRowDto);
......
......@@ -8,6 +8,7 @@ import com.example.demo.domain.QuestionnaireLevel;
import com.example.demo.domain.Task;
import com.example.demo.domain.InfoLevel;
import com.example.demo.dto.BaseLevelDto;
import com.example.demo.dto.PhaseCreateDTO;
import com.example.demo.dto.PhaseLevelUpdateDto;
import com.example.demo.dto.QuestionChoiceDto;
import com.example.demo.dto.QuestionChoiceUpdateDto;
......@@ -17,7 +18,7 @@ import com.example.demo.dto.QuestionnaireUpdateDto;
import com.example.demo.dto.TaskDto;
import com.example.demo.dto.TaskUpdateDto;
import com.example.demo.dto.InfoLevelUpdateDto;
import com.example.demo.enums.LevelType;
import com.example.demo.enums.PhaseType;
import com.example.demo.enums.QuestionType;
import com.example.demo.mapper.BeanMapper;
import com.example.demo.repository.BaseLevelRepository;
......@@ -106,26 +107,24 @@ public class LevelOperationsService {
questionChoiceService.deleteQuestionChoice(questionChoiceId);
}
public BaseLevelDto createLevel(Long trainingDefinitionId, LevelType levelType) {
public BaseLevelDto createLevel(Long trainingDefinitionId, PhaseCreateDTO phaseCreateDTO) {
BaseLevelDto baseLevelDto;
if (levelType.equals(LevelType.info)) {
if (PhaseType.INFO.equals(phaseCreateDTO.getPhaseType())) {
baseLevelDto = infoLevelService.createDefaultInfoLevel(trainingDefinitionId);
} else if (levelType.equals(LevelType.assessment)) {
baseLevelDto = assessmentLevelService.createDefaultAssessmentLevel(trainingDefinitionId);
} else if (levelType.equals(LevelType.questionnaire)) {
} else if (PhaseType.QUESTIONNAIRE.equals(phaseCreateDTO.getPhaseType())) {
baseLevelDto = questionnaireLevelService.createDefaultQuestionnaireLevel(trainingDefinitionId);
} else {
baseLevelDto = phaseLevelService.createDefaultPhaseLevel(trainingDefinitionId);
}
baseLevelDto.setLevelType(levelType);
baseLevelDto.setPhaseType(phaseCreateDTO.getPhaseType());
return baseLevelDto;
}
public BaseLevelDto createTask(Long phaseId) {
TaskDto createdTask = taskService.createDefaultTask(phaseId);
createdTask.setLevelType(LevelType.task);
createdTask.setPhaseType(PhaseType.task);
return createdTask;
}
......
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