вроде работает

This commit is contained in:
bekodeg 2024-05-21 15:10:33 +04:00
parent 0d207ca4ac
commit ddd0f77d9a
6 changed files with 49 additions and 8 deletions

View File

@ -16,7 +16,6 @@ public class GameDTO {
public void setName(String name){ public void setName(String name){
this.name = name; this.name = name;
} }
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){ public Long getId(){
return id; return id;
} }

View File

@ -1,8 +1,12 @@
package com.example.demo.tables.api; package com.example.demo.tables.api;
import com.example.demo.core.config.Constants; import com.example.demo.core.config.Constants;
import com.example.demo.games.api.GameDTO;
import com.example.demo.games.model.GameEntity;
import com.example.demo.tables.model.TableEntity; import com.example.demo.tables.model.TableEntity;
import com.example.demo.tables.service.TableService; import com.example.demo.tables.service.TableService;
import com.example.demo.users.api.UserDTO;
import com.example.demo.users.model.UserEntity;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.modelmapper.ModelMapper; import org.modelmapper.ModelMapper;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -22,10 +26,21 @@ public class TableController {
} }
private TableDTO toDTO(TableEntity entity) { private TableDTO toDTO(TableEntity entity) {
return modelMapper.map(entity, TableDTO.class); return new TableDTO(
entity.getDescription(),
entity.getDate(),
modelMapper.map(entity.getCreator(), UserDTO.class),
modelMapper.map(entity.getGame(), GameDTO.class),
entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList()
);
} }
private TableEntity toEntity(TableDTO dto){ private TableEntity toEntity(TableDTO dto){
return modelMapper.map(dto, TableEntity.class); TableEntity entity = modelMapper.map(dto, TableEntity.class);
entity.setCreator(modelMapper.map(dto.getCreator(), UserEntity.class));
entity.setGame(modelMapper.map(dto.getGame(), GameEntity.class));
entity.setGamers(dto.getGamers().stream()
.map(u -> modelMapper.map(u, UserEntity.class)).toList());
return entity;
} }
@GetMapping @GetMapping

View File

@ -23,6 +23,14 @@ public class TableDTO {
private GameDTO game; private GameDTO game;
private List<UserDTO> gamers; private List<UserDTO> gamers;
public TableDTO(String description, Date date, UserDTO creator, GameDTO game, List<UserDTO> gamers){
this.description = description;
this.date = date;
this.creator = creator;
this.game = game;
this.gamers = gamers;
}
public String getDescription(){ public String getDescription(){
return description; return description;
} }
@ -37,7 +45,7 @@ public class TableDTO {
this.date = date; this.date = date;
} }
public UserDTO geyCreator(){ public UserDTO getCreator(){
return creator; return creator;
} }
public void setCreator(UserDTO creator){ public void setCreator(UserDTO creator){
@ -57,7 +65,6 @@ public class TableDTO {
public void setGamers(List<UserDTO> gamers){ public void setGamers(List<UserDTO> gamers){
this.gamers = gamers; this.gamers = gamers;
} }
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){ public Long getId(){
return id; return id;
} }

View File

@ -4,7 +4,6 @@ import com.example.demo.core.model.BaseEntity;
import com.example.demo.games.model.GameEntity; import com.example.demo.games.model.GameEntity;
import com.example.demo.users.model.UserEntity; import com.example.demo.users.model.UserEntity;
import java.time.Period;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -38,7 +37,7 @@ public class TableEntity extends BaseEntity {
this.date = date; this.date = date;
} }
public UserEntity geyCreator(){ public UserEntity getCreator(){
return creator; return creator;
} }
public void setCreator(UserEntity creator){ public void setCreator(UserEntity creator){

View File

@ -35,6 +35,23 @@ public class TableService {
.orElseThrow(() -> new NotFoundException(id)); .orElseThrow(() -> new NotFoundException(id));
} }
public TableEntity create(TableEntity entity){ public TableEntity create(TableEntity entity){
entity.setCreator(
entity.getCreator() == null ?
entity.getCreator() :
userService.get(entity.getCreator().getId()));
entity.setGamers(
entity.getGamers().isEmpty() ?
entity.getGamers() :
entity.getGamers()
.stream()
.map(g -> userService.get(g.getId()))
.toList()
);
entity.setGame(
entity.getGame().getId() == null ?
entity.getGame() :
(gameService.get(entity.getGame().getId())));
return repository.create(entity); return repository.create(entity);
} }
@ -46,6 +63,11 @@ public class TableService {
entity.getDescription().isEmpty() ? existEntity.getDescription() : entity.getDescription() entity.getDescription().isEmpty() ? existEntity.getDescription() : entity.getDescription()
); );
existEntity.setCreator(
entity.getCreator() == null ?
existEntity.getCreator() :
userService.get(entity.getCreator().getId()));
existEntity.setGamers( existEntity.setGamers(
entity.getGamers().isEmpty() ? entity.getGamers().isEmpty() ?
existEntity.getGamers() : existEntity.getGamers() :

View File

@ -34,7 +34,6 @@ public class UserDTO {
public void setPassword(String password){ public void setPassword(String password){
this.password = password; this.password = password;
} }
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Long getId(){ public Long getId(){
return id; return id;
} }