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

Add DTOs, mapstruct for entity - DTO conversion, PoC service and get rid of lombok in entities

parent a4bbb152
No related branches found
No related tags found
No related merge requests found
Showing
with 526 additions and 10 deletions
......@@ -19,6 +19,7 @@
<java.version>11</java.version>
<projectlombok.version>1.18.12</projectlombok.version>
<org.mapstruct.version>1.3.1.Final</org.mapstruct.version>
</properties>
<dependencies>
......@@ -51,8 +52,23 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${projectlombok.version}</version>
<version>1.18.12</version>
<!-- <version>${projectlombok.version}</version>-->
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.3.1.Final</version>
<!-- <version>${org.mapstruct.version}</version>-->
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
</dependencies>
<build>
......@@ -61,6 +77,23 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>11</source>
<target>11</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
......
......@@ -2,8 +2,10 @@ package com.example.demo;
import com.example.demo.domain.GameLevel;
import com.example.demo.domain.Person;
import com.example.demo.dto.GameLevelDto;
import com.example.demo.repository.GameLevelRepository;
import com.example.demo.repository.PersonRepository;
import com.example.demo.service.GameLevelService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
......@@ -79,4 +81,13 @@ public class DemoApplication {
log.info("Saved game level: {}", foundGameLevel);
};
}
@Bean
CommandLineRunner yetAnotherDemo(GameLevelService gameLevelService) {
return args -> {
List<GameLevelDto> allGameLevels = gameLevelService.findAllGameLevels();
allGameLevels.forEach(gameLevel -> log.info("Found game level with ID {} and title {}", gameLevel.getId(), gameLevel.getTitle()));
};
}
}
package com.example.demo.domain;
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
@Data
public class AssessmentLevel extends BaseLevel {
@Id
......@@ -16,4 +14,19 @@ public class AssessmentLevel extends BaseLevel {
private String assessmentType;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAssessmentType() {
return assessmentType;
}
public void setAssessmentType(String assessmentType) {
this.assessmentType = assessmentType;
}
}
package com.example.demo.domain;
import lombok.Data;
import org.neo4j.ogm.annotation.NodeEntity;
@NodeEntity
@Data
public class BaseLevel {
public abstract class BaseLevel {
private String title;
private String estimatedDuration;
private Long maxScore;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEstimatedDuration() {
return estimatedDuration;
}
public void setEstimatedDuration(String estimatedDuration) {
this.estimatedDuration = estimatedDuration;
}
public Long getMaxScore() {
return maxScore;
}
public void setMaxScore(Long maxScore) {
this.maxScore = maxScore;
}
}
package com.example.demo.domain;
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@NodeEntity
@Data
public class GameLevel extends BaseLevel {
@Id
......@@ -26,4 +26,71 @@ public class GameLevel extends BaseLevel {
@Relationship(type = "GAME_LEVEL_HINTS", direction = Relationship.UNDIRECTED)
private List<Hint> hints;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSolutionPenalized() {
return solutionPenalized;
}
public void setSolutionPenalized(String solutionPenalized) {
this.solutionPenalized = solutionPenalized;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getSolution() {
return solution;
}
public void setSolution(String solution) {
this.solution = solution;
}
public String getAttachments() {
return attachments;
}
public void setAttachments(String attachments) {
this.attachments = attachments;
}
public String getIncorrectFlagLimit() {
return incorrectFlagLimit;
}
public void setIncorrectFlagLimit(String incorrectFlagLimit) {
this.incorrectFlagLimit = incorrectFlagLimit;
}
public List<Hint> getHints() {
if (Objects.isNull(hints)) {
hints = new ArrayList<>();
}
return hints;
}
public void setHints(List<Hint> hints) {
this.hints = hints;
}
}
package com.example.demo.domain;
import lombok.Data;
import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
@Data
@NodeEntity
public class Hint {
@Id
......@@ -15,4 +15,44 @@ public class Hint {
private String content;
private String hintPenalty;
private String orderInLevel;
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 String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getHintPenalty() {
return hintPenalty;
}
public void setHintPenalty(String hintPenalty) {
this.hintPenalty = hintPenalty;
}
public String getOrderInLevel() {
return orderInLevel;
}
public void setOrderInLevel(String orderInLevel) {
this.orderInLevel = orderInLevel;
}
}
......@@ -12,4 +12,20 @@ public class InfoLevel extends BaseLevel {
private Long id;
private String content;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.example.demo.dto;
import java.io.Serializable;
public class AssessmentLevelDto extends BaseLevelDto implements Serializable {
private Long id;
private String assessmentType;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getAssessmentType() {
return assessmentType;
}
public void setAssessmentType(String assessmentType) {
this.assessmentType = assessmentType;
}
}
package com.example.demo.dto;
import java.io.Serializable;
public class BaseLevelDto implements Serializable {
private String title;
private String estimatedDuration;
private Long maxScore;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getEstimatedDuration() {
return estimatedDuration;
}
public void setEstimatedDuration(String estimatedDuration) {
this.estimatedDuration = estimatedDuration;
}
public Long getMaxScore() {
return maxScore;
}
public void setMaxScore(Long maxScore) {
this.maxScore = maxScore;
}
}
package com.example.demo.dto;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class GameLevelDto extends BaseLevelDto implements Serializable {
private Long id;
private String content;
private String solutionPenalized;
private String flag;
private String solution;
private String attachments;
private String incorrectFlagLimit;
private List<HintDto> hints;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getSolutionPenalized() {
return solutionPenalized;
}
public void setSolutionPenalized(String solutionPenalized) {
this.solutionPenalized = solutionPenalized;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getSolution() {
return solution;
}
public void setSolution(String solution) {
this.solution = solution;
}
public String getAttachments() {
return attachments;
}
public void setAttachments(String attachments) {
this.attachments = attachments;
}
public String getIncorrectFlagLimit() {
return incorrectFlagLimit;
}
public void setIncorrectFlagLimit(String incorrectFlagLimit) {
this.incorrectFlagLimit = incorrectFlagLimit;
}
public List<HintDto> getHints() {
if (Objects.isNull(hints)) {
hints = new ArrayList<>();
}
return hints;
}
public void setHints(List<HintDto> hints) {
this.hints = hints;
}
}
package com.example.demo.dto;
import java.io.Serializable;
public class HintDto implements Serializable {
private Long id;
private String title;
private String content;
private String hintPenalty;
private String orderInLevel;
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 String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getHintPenalty() {
return hintPenalty;
}
public void setHintPenalty(String hintPenalty) {
this.hintPenalty = hintPenalty;
}
public String getOrderInLevel() {
return orderInLevel;
}
public void setOrderInLevel(String orderInLevel) {
this.orderInLevel = orderInLevel;
}
}
package com.example.demo.dto;
import java.io.Serializable;
public class InfoLevelDto extends BaseLevelDto implements Serializable {
private Long id;
private String content;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package com.example.demo.mapper;
import com.example.demo.domain.AssessmentLevel;
import com.example.demo.domain.BaseLevel;
import com.example.demo.domain.GameLevel;
import com.example.demo.domain.Hint;
import com.example.demo.domain.InfoLevel;
import com.example.demo.dto.AssessmentLevelDto;
import com.example.demo.dto.GameLevelDto;
import com.example.demo.dto.HintDto;
import com.example.demo.dto.InfoLevelDto;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
@Mapper
public interface ObjectMapper {
ObjectMapper INSTANCE = Mappers.getMapper(ObjectMapper.class);
AssessmentLevelDto toDto(AssessmentLevel assessmentLevel);
AssessmentLevel toEntity(AssessmentLevelDto assessmentLevel);
GameLevelDto toDto(GameLevel gameLevel);
GameLevel toEntity(GameLevelDto gameLevel);
InfoLevelDto toDto(InfoLevel infoLevel);
InfoLevel toEntity(InfoLevelDto infoLevel);
HintDto toDto(Hint hint);
Hint toEntity(HintDto hint);
}
package com.example.demo.service;
import com.example.demo.dto.GameLevelDto;
import java.util.List;
public interface GameLevelService {
List<GameLevelDto> findAllGameLevels();
}
package com.example.demo.service.impl;
import com.example.demo.domain.GameLevel;
import com.example.demo.dto.GameLevelDto;
import com.example.demo.mapper.ObjectMapper;
import com.example.demo.repository.GameLevelRepository;
import com.example.demo.service.GameLevelService;
import org.apache.commons.collections4.IterableUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
@Service
public class GameLevelServiceImpl implements GameLevelService {
private final GameLevelRepository gameLevelRepository;
@Autowired
public GameLevelServiceImpl(GameLevelRepository gameLevelRepository) {
this.gameLevelRepository = gameLevelRepository;
}
@Override
public List<GameLevelDto> findAllGameLevels() {
Iterable<GameLevel> allGameLevels = gameLevelRepository.findAll();
List<GameLevelDto> result = new ArrayList<>();
if (!IterableUtils.isEmpty(allGameLevels)) {
for (GameLevel gameLevel : allGameLevels) {
result.add(ObjectMapper.INSTANCE.toDto(gameLevel));
}
}
return result;
}
}
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