diff --git a/src/main/java/com/example/demo/controller/GameLevelController.java b/src/main/java/com/example/demo/controller/GameLevelController.java index cad6e55da3b3aeab2a13f3027ce43e2a80dbe82b..76ef027a35f559dc038d72504a1ed2d45ae2f54c 100644 --- a/src/main/java/com/example/demo/controller/GameLevelController.java +++ b/src/main/java/com/example/demo/controller/GameLevelController.java @@ -1,6 +1,7 @@ package com.example.demo.controller; import com.example.demo.dto.GameLevelDto; +import com.example.demo.dto.GameLevelUpdateDto; import com.example.demo.service.GameLevelService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; @@ -50,7 +51,7 @@ public class GameLevelController { @ApiResponses(value = {@ApiResponse(code = 200, message = "Updated game level"), @ApiResponse(code = 500, message = "Unexpected application error")}) public GameLevelDto updateGameLevel(@ApiParam(value = "Game Level ID", required = true) @PathVariable("id") final Long id, - @ApiParam(value = "Update data", required = true) @RequestBody(required = true) GameLevelDto gameLevelDto) { - return gameLevelService.updateGameLevel(id, gameLevelDto); + @ApiParam(value = "Update data", required = true) @RequestBody(required = true) GameLevelUpdateDto gameLevelUpdateDto) { + return gameLevelService.updateGameLevel(id, gameLevelUpdateDto); } } diff --git a/src/main/java/com/example/demo/dto/GameLevelDto.java b/src/main/java/com/example/demo/dto/GameLevelDto.java index a553393dd739e073113cde22c6713d7c1c75eae7..f7a6ad3dfaa28f8516f26d4b2c3265f4157b6b7f 100644 --- a/src/main/java/com/example/demo/dto/GameLevelDto.java +++ b/src/main/java/com/example/demo/dto/GameLevelDto.java @@ -1,23 +1,11 @@ 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; } @@ -26,76 +14,11 @@ public class GameLevelDto extends BaseLevelDto implements Serializable { 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; - } @Override public String toString() { return "GameLevelDto{" + "id=" + id + - ", content='" + content + '\'' + - ", solutionPenalized='" + solutionPenalized + '\'' + - ", flag='" + flag + '\'' + - ", solution='" + solution + '\'' + - ", attachments='" + attachments + '\'' + - ", incorrectFlagLimit='" + incorrectFlagLimit + '\'' + - ", hints=" + hints + "} " + super.toString(); } } diff --git a/src/main/java/com/example/demo/dto/GameLevelUpdateDto.java b/src/main/java/com/example/demo/dto/GameLevelUpdateDto.java new file mode 100644 index 0000000000000000000000000000000000000000..d2e292c6eafde0bacbb978a427b775e5c3de6a8e --- /dev/null +++ b/src/main/java/com/example/demo/dto/GameLevelUpdateDto.java @@ -0,0 +1,89 @@ +package com.example.demo.dto; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +public class GameLevelUpdateDto extends BaseLevelDto { + + private String content; + private String solutionPenalized; + private String flag; + private String solution; + private String attachments; + private String incorrectFlagLimit; + + private List<HintDto> hints; + + 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; + } + + @Override + public String toString() { + return "GameLevelDto{" + + "content='" + content + '\'' + + ", solutionPenalized='" + solutionPenalized + '\'' + + ", flag='" + flag + '\'' + + ", solution='" + solution + '\'' + + ", attachments='" + attachments + '\'' + + ", incorrectFlagLimit='" + incorrectFlagLimit + '\'' + + ", hints=" + hints + + "} " + super.toString(); + } +} diff --git a/src/main/java/com/example/demo/mapper/BeanMapper.java b/src/main/java/com/example/demo/mapper/BeanMapper.java index 50e3346ce757ea47291f83a7cc5f2790cf8d3fca..78d0bff9e2678cbf2182622f1762649b8246f120 100644 --- a/src/main/java/com/example/demo/mapper/BeanMapper.java +++ b/src/main/java/com/example/demo/mapper/BeanMapper.java @@ -6,6 +6,7 @@ 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.GameLevelUpdateDto; import com.example.demo.dto.HintDto; import com.example.demo.dto.InfoLevelDto; import org.mapstruct.Mapper; @@ -25,6 +26,8 @@ public interface BeanMapper { GameLevel toEntity(GameLevelDto gameLevel); + GameLevel toEntity(GameLevelUpdateDto gameLevel); + InfoLevelDto toDto(InfoLevel infoLevel); InfoLevel toEntity(InfoLevelDto infoLevel); diff --git a/src/main/java/com/example/demo/service/GameLevelService.java b/src/main/java/com/example/demo/service/GameLevelService.java index bd2fcebcb95bcf104113df8a85cc00cd479f32b2..ebaf94861700de30e849dd759840bb1332e4922e 100644 --- a/src/main/java/com/example/demo/service/GameLevelService.java +++ b/src/main/java/com/example/demo/service/GameLevelService.java @@ -2,19 +2,24 @@ package com.example.demo.service; import com.example.demo.domain.GameLevel; import com.example.demo.dto.GameLevelDto; +import com.example.demo.dto.GameLevelUpdateDto; import com.example.demo.mapper.BeanMapper; import com.example.demo.repository.GameLevelRepository; -import com.example.demo.service.GameLevelService; import org.apache.commons.collections4.IterableUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; +import java.util.Optional; @Service public class GameLevelService { + private static final Logger LOG = LoggerFactory.getLogger(GameLevelService.class); + private final GameLevelRepository gameLevelRepository; @Autowired @@ -37,8 +42,16 @@ public class GameLevelService { return result; } - public GameLevelDto updateGameLevel(Long id, GameLevelDto gameLevelDto) { - GameLevel gameLevel = BeanMapper.INSTANCE.toEntity(gameLevelDto); + public GameLevelDto updateGameLevel(Long id, GameLevelUpdateDto gameLevelUpdateDto) { + Optional<GameLevel> persistedGameLevel = gameLevelRepository.findById(id); + + if (persistedGameLevel.isEmpty()) { + LOG.error("No game level found with attribute {}.", id); + return new GameLevelDto(); + } + + GameLevel gameLevel = BeanMapper.INSTANCE.toEntity(gameLevelUpdateDto); + gameLevel.setId(persistedGameLevel.get().getId()); GameLevel savedEntity = gameLevelRepository.save(gameLevel);