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

First iteration of CRUD operations on InfoLevel

parent b5e5be5b
No related branches found
No related tags found
No related merge requests found
package com.example.demo.controller;
import com.example.demo.dto.InfoLevelCreateDto;
import com.example.demo.dto.InfoLevelDto;
import com.example.demo.dto.InfoLevelUpdateDto;
import com.example.demo.service.InfoLevelService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
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.RestController;
import java.util.List;
@RestController
@RequestMapping("/info-level")
@CrossOrigin(origins = "*", allowCredentials = "true", allowedHeaders = "*",
methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT})
@Api(value = "/info-level", tags = {"Info Level"})
public class InfoLevelController {
private final InfoLevelService infoLevelService;
@Autowired
public InfoLevelController(InfoLevelService infoLevelService) {
this.infoLevelService = infoLevelService;
}
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Create a new info level")
@ApiResponses(value = {@ApiResponse(code = 200, message = "New info level created"),
@ApiResponse(code = 500, message = "Unexpected application error")})
public InfoLevelDto createInfoLevel(@ApiParam(value = "Game level", required = true) @RequestBody(required = true)
InfoLevelCreateDto infoLevelCreateDto) {
return infoLevelService.createInfoLevel(infoLevelCreateDto);
}
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Return info levels")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Return info levels"),
@ApiResponse(code = 500, message = "Unexpected application error")})
public List<InfoLevelDto> findInfoLevels() {
return infoLevelService.findAllInfoLevels();
}
@PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Get info level detail")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Game level detail"),
@ApiResponse(code = 500, message = "Unexpected application error")})
public InfoLevelDto getInfoLevel(
@ApiParam(value = "Game Level ID", required = true) @PathVariable("id") final Long id) {
return infoLevelService.getInfoLevel(id);
}
@PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Update info level")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Updated info level"),
@ApiResponse(code = 500, message = "Unexpected application error")})
public InfoLevelDto updateInfoLevel(
@ApiParam(value = "Game Level ID", required = true) @PathVariable("id") final Long id,
@ApiParam(value = "Update data", required = true) @RequestBody(required = true)
InfoLevelUpdateDto infoLevelUpdateDto) {
return infoLevelService.updateInfoLevel(id, infoLevelUpdateDto);
}
@DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Remove info level")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Game level removed"),
@ApiResponse(code = 500, message = "Unexpected application error")})
public void removeInfoLevel(@ApiParam(value = "Game Level ID", required = true) @PathVariable("id") final Long id) {
infoLevelService.removeInfoLevel(id);
}
}
package com.example.demo.dto;
public class InfoLevelCreateDto extends InfoLevelUpdateDto {
@Override
public String toString() {
return "InfoLevelCreateDto{} " + super.toString();
}
}
......@@ -2,11 +2,10 @@ package com.example.demo.dto;
import java.io.Serializable;
public class InfoLevelDto extends BaseLevelDto implements Serializable {
public class InfoLevelDto extends InfoLevelUpdateDto implements Serializable {
private Long id;
private String content;
public Long getId() {
return id;
......@@ -16,19 +15,10 @@ public class InfoLevelDto extends BaseLevelDto implements Serializable {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "InfoLevelDto{" +
"id=" + id +
", content='" + content + '\'' +
"} " + super.toString();
}
}
package com.example.demo.dto;
public class InfoLevelUpdateDto extends BaseLevelDto {
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "InfoLevelUpdateDto{" + "content='" + content + '\'' + '}';
}
}
......@@ -11,7 +11,9 @@ import com.example.demo.dto.GameLevelCreateDto;
import com.example.demo.dto.GameLevelDto;
import com.example.demo.dto.GameLevelUpdateDto;
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 org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
......@@ -37,6 +39,10 @@ public interface BeanMapper {
InfoLevel toEntity(InfoLevelDto infoLevel);
InfoLevel toEntity(InfoLevelCreateDto gameLevel);
InfoLevel toEntity(InfoLevelUpdateDto gameLevel);
HintDto toDto(Hint hint);
Hint toEntity(HintDto hint);
......
package com.example.demo.repository;
import com.example.demo.domain.InfoLevel;
import org.springframework.data.neo4j.repository.Neo4jRepository;
public interface InfoLevelRepository extends Neo4jRepository<InfoLevel, Long> {
}
package com.example.demo.service;
import com.example.demo.domain.InfoLevel;
import com.example.demo.dto.InfoLevelCreateDto;
import com.example.demo.dto.InfoLevelDto;
import com.example.demo.dto.InfoLevelUpdateDto;
import com.example.demo.mapper.BeanMapper;
import com.example.demo.repository.InfoLevelRepository;
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 InfoLevelService {
private static final Logger LOG = LoggerFactory.getLogger(InfoLevelService.class);
private final InfoLevelRepository infoLevelRepository;
@Autowired
public InfoLevelService(InfoLevelRepository infoLevelRepository) {
this.infoLevelRepository = infoLevelRepository;
}
public InfoLevelDto createInfoLevel(InfoLevelCreateDto infoLevelCreateDto) {
InfoLevel infoLevel = BeanMapper.INSTANCE.toEntity(infoLevelCreateDto);
InfoLevel persistedEntity = infoLevelRepository.save(infoLevel);
return BeanMapper.INSTANCE.toDto(persistedEntity);
}
public List<InfoLevelDto> findAllInfoLevels() {
Iterable<InfoLevel> allInfoLevels = infoLevelRepository.findAll();
List<InfoLevelDto> result = new ArrayList<>();
if (!IterableUtils.isEmpty(allInfoLevels)) {
for (InfoLevel infoLevel : allInfoLevels) {
result.add(BeanMapper.INSTANCE.toDto(infoLevel));
}
}
return result;
}
public InfoLevelDto getInfoLevel(Long id) {
Optional<InfoLevel> infoLevel = infoLevelRepository.findById(id);
if (infoLevel.isEmpty()) {
LOG.error("No info level found with ID {}.", id);
return new InfoLevelDto();
}
return BeanMapper.INSTANCE.toDto(infoLevel.get());
}
public InfoLevelDto updateInfoLevel(Long id, InfoLevelUpdateDto infoLevelUpdateDto) {
Optional<InfoLevel> persistedInfoLevel = infoLevelRepository.findById(id);
if (persistedInfoLevel.isEmpty()) {
LOG.error("No info level found with ID {}.", id);
return new InfoLevelDto();
}
InfoLevel infoLevel = BeanMapper.INSTANCE.toEntity(infoLevelUpdateDto);
infoLevel.setId(persistedInfoLevel.get().getId());
InfoLevel savedEntity = infoLevelRepository.save(infoLevel);
return BeanMapper.INSTANCE.toDto(savedEntity);
}
public void removeInfoLevel(Long id) {
infoLevelRepository.deleteById(id);
}
}
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