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

Very simple implementation of GameDefinitionService creating DB entities

parent d8b7b65c
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/game-definition")
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*",
......@@ -39,7 +41,7 @@ public class GameDefinitionController {
@ApiResponses(value = {@ApiResponse(code = 200, message = "New game definition created"),
@ApiResponse(code = 500, message = "Unexpected application error")})
public GameDefinitionCreateDto createGameLevel(@ApiParam(value = "Game definition", required = true) @RequestBody(required = true)
GameDefinitionCreateDto gameLevelCreateDto) {
return gameDefinitionService.createGameDefinition(gameLevelCreateDto);
List<GameDefinitionCreateDto> gameDefinition) {
return gameDefinitionService.createGameDefinition(gameDefinition);
}
}
......@@ -3,20 +3,22 @@ package com.example.demo.domain;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import java.util.List;
@Entity
public class UnityLevel {
@Id
@GeneratedValue
private Long id;
public class UnityLevel extends BaseLevel {
@OrderBy
@OneToMany(mappedBy = "unityLevel", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<BaseLevel> subLevels;
public List<BaseLevel> getSubLevels() {
return subLevels;
}
public void setSubLevels(List<BaseLevel> subLevels) {
this.subLevels = subLevels;
}
}
package com.example.demo.dto;
import java.util.List;
public class UnityLevelDto extends BaseLevelDto {
// TODO any class extending BaseLevel could be used here - this piece of art is here in order to make mapstruct happy
private List<GameLevelDto> subLevels;
public List<GameLevelDto> getSubLevels() {
return subLevels;
}
public void setSubLevels(List<GameLevelDto> subLevels) {
this.subLevels = subLevels;
}
}
package com.example.demo.dto.input;
import lombok.Data;
import java.util.List;
@Data
public class GameDefinitionCreateDto {
private Long id;
private String title;
private Integer order;
private LevelType type;
private List<? extends GameDefinitionCreateDto> subLevels;
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 LevelType getType() {
return type;
}
public void setType(LevelType type) {
this.type = type;
}
public List<? extends GameDefinitionCreateDto> getSubLevels() {
return subLevels;
}
public void setSubLevels(List<? extends GameDefinitionCreateDto> subLevels) {
this.subLevels = subLevels;
}
}
package com.example.demo.dto.input;
public enum LevelType {
ASSESSMENT,
GAME,
INFO,
UNITY
assessment,
game,
info,
unity
}
......@@ -5,6 +5,7 @@ import com.example.demo.domain.Attachment;
import com.example.demo.domain.GameLevel;
import com.example.demo.domain.Hint;
import com.example.demo.domain.InfoLevel;
import com.example.demo.domain.UnityLevel;
import com.example.demo.dto.AssessmentLevelDto;
import com.example.demo.dto.AttachmentDto;
import com.example.demo.dto.GameLevelCreateDto;
......@@ -14,6 +15,7 @@ import com.example.demo.dto.HintDto;
import com.example.demo.dto.InfoLevelCreateDto;
import com.example.demo.dto.InfoLevelDto;
import com.example.demo.dto.InfoLevelUpdateDto;
import com.example.demo.dto.UnityLevelDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
......@@ -50,4 +52,8 @@ public interface BeanMapper {
AttachmentDto toDto(Attachment hint);
Attachment toEntity(AttachmentDto hint);
UnityLevelDto toDto(UnityLevel unityLevel);
UnityLevel toEntity(UnityLevelDto unityLevel);
}
package com.example.demo.repository;
import com.example.demo.domain.UnityLevel;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UnityLevelRepository extends JpaRepository<UnityLevel, Long> {
}
package com.example.demo.service;
import com.example.demo.domain.AssessmentLevel;
import com.example.demo.domain.BaseLevel;
import com.example.demo.domain.GameLevel;
import com.example.demo.domain.UnityLevel;
import com.example.demo.dto.input.GameDefinitionCreateDto;
import com.example.demo.dto.input.LevelType;
import com.example.demo.repository.AssessmentLevelRepository;
import com.example.demo.repository.GameLevelRepository;
import com.example.demo.repository.UnityLevelRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class GameDefinitionService {
public GameDefinitionCreateDto createGameDefinition(GameDefinitionCreateDto gameDefinitionCreateDto) {
@Autowired
private AssessmentLevelRepository assessmentLevelRepository;
@Autowired
private UnityLevelRepository unityLevelRepository;
@Autowired
private GameLevelRepository gameLevelRepository;
public GameDefinitionCreateDto createGameDefinition(List<GameDefinitionCreateDto> gameDefinition) {
for (GameDefinitionCreateDto gameDefinitionCreateDto : gameDefinition) {
if (gameDefinitionCreateDto.getType() == LevelType.assessment) {
AssessmentLevel assessmentLevel = new AssessmentLevel();
assessmentLevel.setTitle(gameDefinitionCreateDto.getTitle());
assessmentLevel.setOrderInTrainingDefinition(gameDefinitionCreateDto.getOrder());
assessmentLevelRepository.save(assessmentLevel);
} else if (gameDefinitionCreateDto.getType() == LevelType.unity) {
UnityLevel unityLevel = new UnityLevel();
unityLevel.setTitle(gameDefinitionCreateDto.getTitle());
unityLevel.setOrderInTrainingDefinition(gameDefinitionCreateDto.getOrder());
unityLevelRepository.save(unityLevel);
List<BaseLevel> subLevels = new ArrayList<>();
for (GameDefinitionCreateDto subLevel : gameDefinitionCreateDto.getSubLevels()) {
if (subLevel.getType() == LevelType.game) {
GameLevel gameLevel = new GameLevel();
gameLevel.setTitle(subLevel.getTitle());
gameLevel.setOrderInTrainingDefinition(subLevel.getOrder());
gameLevel.setUnityLevel(unityLevel);
subLevels.add(gameLevel);
gameLevelRepository.save(gameLevel);
}
}
unityLevel.setSubLevels(subLevels);
unityLevelRepository.save(unityLevel);
}
}
return null;
}
......
......@@ -3,3 +3,6 @@
#spring.datasource.username=sa
#spring.datasource.password=password
#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
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