mirror of
https://gitea.gofwd.group/Forward_Group/ballistic-builder-spring.git
synced 2026-01-20 16:51:03 -05:00
Adding some mappers and dtos to clean up data issues in the controllers
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package group.goforward.battlbuilder.controllers.api.v1;
|
||||
|
||||
import group.goforward.battlbuilder.mapper.BrandMapper;
|
||||
import group.goforward.battlbuilder.model.Brand;
|
||||
import group.goforward.battlbuilder.repos.build.BrandRepository;
|
||||
import group.goforward.battlbuilder.services.BrandService;
|
||||
import group.goforward.battlbuilder.web.dto.brand.BrandDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
@@ -1,35 +1,35 @@
|
||||
package group.goforward.battlbuilder.controllers.api.v1;
|
||||
|
||||
import group.goforward.battlbuilder.mapper.BuildMapper;
|
||||
import group.goforward.battlbuilder.model.Build;
|
||||
import group.goforward.battlbuilder.repos.build.BuildRepository;
|
||||
import group.goforward.battlbuilder.web.dto.build.BuildDto;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/builds")
|
||||
@RequestMapping({"/api/builds", "/api/v1fix/builds"})
|
||||
public class BuildController {
|
||||
@Autowired
|
||||
private BuildRepository repo;
|
||||
@Autowired
|
||||
// private BuildsService service;
|
||||
//@Cacheable(value="getAllStates")
|
||||
// @Autowired
|
||||
// private BuildsService service;
|
||||
@Cacheable(value="getAllBuilds")
|
||||
@GetMapping("/all")
|
||||
public ResponseEntity<List<Build>> getAll() {
|
||||
public ResponseEntity<List<BuildDto>> getAll() {
|
||||
List<Build> builds = repo.findAll();
|
||||
return ResponseEntity.ok(builds);
|
||||
return ResponseEntity.ok(BuildMapper.toDtoList(builds));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Build> getAllBuildsById(@PathVariable Integer id) {
|
||||
public ResponseEntity<BuildDto> getAllBuildsById(@PathVariable Integer id) {
|
||||
return repo.findById(id)
|
||||
.map(ResponseEntity::ok)
|
||||
.map(build -> ResponseEntity.ok(BuildMapper.toDto(build)))
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package group.goforward.battlbuilder.controllers.api.v1;
|
||||
|
||||
import group.goforward.battlbuilder.dto.StateDto;
|
||||
import group.goforward.battlbuilder.mapper.StateMapper;
|
||||
import group.goforward.battlbuilder.model.State;
|
||||
import group.goforward.battlbuilder.repos.StateRepository;
|
||||
import group.goforward.battlbuilder.services.admin.StatesService;
|
||||
@@ -20,27 +22,34 @@ public class StateController {
|
||||
private StatesService statesService;
|
||||
//@Cacheable(value="getAllStates")
|
||||
@GetMapping("/all")
|
||||
public ResponseEntity<List<State>> getAllStates() {
|
||||
List<State> state = repo.findAll();
|
||||
return ResponseEntity.ok(state);
|
||||
public ResponseEntity<List<StateDto>> getAllStates() {
|
||||
List<State> states = repo.findAll();
|
||||
List<StateDto> result = states.stream()
|
||||
.map(StateMapper::toDto)
|
||||
.toList();
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<State> getAllStatesById(@PathVariable Integer id) {
|
||||
public ResponseEntity<StateDto> getAllStatesById(@PathVariable Integer id) {
|
||||
return repo.findById(id)
|
||||
.map(StateMapper::toDto)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
@GetMapping("/byAbbrev/{abbreviation}")
|
||||
public ResponseEntity<State> getAllStatesByAbbreviation(@PathVariable String abbreviation) {
|
||||
public ResponseEntity<StateDto> getAllStatesByAbbreviation(@PathVariable String abbreviation) {
|
||||
return repo.findByAbbreviation(abbreviation)
|
||||
.map(StateMapper::toDto)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
@PostMapping("/addState")
|
||||
public ResponseEntity<State> createState(@RequestBody State item) {
|
||||
State created = statesService.save(item);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(created);
|
||||
public ResponseEntity<StateDto> createState(@RequestBody StateDto item) {
|
||||
State toSave = StateMapper.fromDto(item);
|
||||
State created = statesService.save(toSave);
|
||||
StateDto createdDto = StateMapper.toDto(created);
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(createdDto);
|
||||
}
|
||||
|
||||
@DeleteMapping("/deleteState/{id}")
|
||||
|
||||
32
src/main/java/group/goforward/battlbuilder/dto/StateDto.java
Normal file
32
src/main/java/group/goforward/battlbuilder/dto/StateDto.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package group.goforward.battlbuilder.dto;
|
||||
|
||||
public class StateDto {
|
||||
|
||||
private Integer id;
|
||||
private String state;
|
||||
private String abbreviation;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void setState(String state) {
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public String getAbbreviation() {
|
||||
return abbreviation;
|
||||
}
|
||||
|
||||
public void setAbbreviation(String abbreviation) {
|
||||
this.abbreviation = abbreviation;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package group.goforward.battlbuilder.mapper;
|
||||
|
||||
import group.goforward.battlbuilder.dto.StateDto;
|
||||
import group.goforward.battlbuilder.model.State;
|
||||
|
||||
|
||||
public final class StateMapper {
|
||||
|
||||
private StateMapper() {
|
||||
// utility class
|
||||
}
|
||||
|
||||
public static StateDto toDto(State state) {
|
||||
if (state == null) {
|
||||
return null;
|
||||
}
|
||||
StateDto dto = new StateDto();
|
||||
dto.setId(state.getId());
|
||||
dto.setState(state.getState());
|
||||
dto.setAbbreviation(state.getAbbreviation());
|
||||
return dto;
|
||||
}
|
||||
|
||||
public static State fromDto(StateDto dto) {
|
||||
if (dto == null) {
|
||||
return null;
|
||||
}
|
||||
State state = new State();
|
||||
state.setId(dto.getId());
|
||||
state.setState(dto.getState());
|
||||
state.setAbbreviation(dto.getAbbreviation());
|
||||
return state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user