From ddd0f77d9a88fb35333979b2cae14443282e8fd7 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Tue, 21 May 2024 15:10:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D1=80=D0=BE=D0=B4=D0=B5=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/demo/games/api/GameDTO.java | 1 - .../demo/tables/api/TableController.java | 19 ++++++++++++++-- .../com/example/demo/tables/api/TableDTO.java | 11 ++++++++-- .../demo/tables/model/TableEntity.java | 3 +-- .../demo/tables/service/TableService.java | 22 +++++++++++++++++++ .../com/example/demo/users/api/UserDTO.java | 1 - 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/demo/src/main/java/com/example/demo/games/api/GameDTO.java b/demo/src/main/java/com/example/demo/games/api/GameDTO.java index 71709d7..2d74e9b 100644 --- a/demo/src/main/java/com/example/demo/games/api/GameDTO.java +++ b/demo/src/main/java/com/example/demo/games/api/GameDTO.java @@ -16,7 +16,6 @@ public class GameDTO { public void setName(String name){ this.name = name; } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; } diff --git a/demo/src/main/java/com/example/demo/tables/api/TableController.java b/demo/src/main/java/com/example/demo/tables/api/TableController.java index 4279fa3..cf2de89 100644 --- a/demo/src/main/java/com/example/demo/tables/api/TableController.java +++ b/demo/src/main/java/com/example/demo/tables/api/TableController.java @@ -1,8 +1,12 @@ package com.example.demo.tables.api; 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.service.TableService; +import com.example.demo.users.api.UserDTO; +import com.example.demo.users.model.UserEntity; import jakarta.validation.Valid; import org.modelmapper.ModelMapper; import org.springframework.web.bind.annotation.*; @@ -22,10 +26,21 @@ public class TableController { } 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){ - 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 diff --git a/demo/src/main/java/com/example/demo/tables/api/TableDTO.java b/demo/src/main/java/com/example/demo/tables/api/TableDTO.java index 9bb19d2..2c60a57 100644 --- a/demo/src/main/java/com/example/demo/tables/api/TableDTO.java +++ b/demo/src/main/java/com/example/demo/tables/api/TableDTO.java @@ -23,6 +23,14 @@ public class TableDTO { private GameDTO game; private List gamers; + public TableDTO(String description, Date date, UserDTO creator, GameDTO game, List gamers){ + this.description = description; + this.date = date; + this.creator = creator; + this.game = game; + this.gamers = gamers; + } + public String getDescription(){ return description; } @@ -37,7 +45,7 @@ public class TableDTO { this.date = date; } - public UserDTO geyCreator(){ + public UserDTO getCreator(){ return creator; } public void setCreator(UserDTO creator){ @@ -57,7 +65,6 @@ public class TableDTO { public void setGamers(List gamers){ this.gamers = gamers; } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; } diff --git a/demo/src/main/java/com/example/demo/tables/model/TableEntity.java b/demo/src/main/java/com/example/demo/tables/model/TableEntity.java index 6a28ad5..90d19de 100644 --- a/demo/src/main/java/com/example/demo/tables/model/TableEntity.java +++ b/demo/src/main/java/com/example/demo/tables/model/TableEntity.java @@ -4,7 +4,6 @@ import com.example.demo.core.model.BaseEntity; import com.example.demo.games.model.GameEntity; import com.example.demo.users.model.UserEntity; -import java.time.Period; import java.util.Date; import java.util.List; @@ -38,7 +37,7 @@ public class TableEntity extends BaseEntity { this.date = date; } - public UserEntity geyCreator(){ + public UserEntity getCreator(){ return creator; } public void setCreator(UserEntity creator){ diff --git a/demo/src/main/java/com/example/demo/tables/service/TableService.java b/demo/src/main/java/com/example/demo/tables/service/TableService.java index 50ebc6f..a5b68cc 100644 --- a/demo/src/main/java/com/example/demo/tables/service/TableService.java +++ b/demo/src/main/java/com/example/demo/tables/service/TableService.java @@ -35,6 +35,23 @@ public class TableService { .orElseThrow(() -> new NotFoundException(id)); } 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); } @@ -46,6 +63,11 @@ public class TableService { entity.getDescription().isEmpty() ? existEntity.getDescription() : entity.getDescription() ); + existEntity.setCreator( + entity.getCreator() == null ? + existEntity.getCreator() : + userService.get(entity.getCreator().getId())); + existEntity.setGamers( entity.getGamers().isEmpty() ? existEntity.getGamers() : diff --git a/demo/src/main/java/com/example/demo/users/api/UserDTO.java b/demo/src/main/java/com/example/demo/users/api/UserDTO.java index e000f5b..5c5745e 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserDTO.java +++ b/demo/src/main/java/com/example/demo/users/api/UserDTO.java @@ -34,7 +34,6 @@ public class UserDTO { public void setPassword(String password){ this.password = password; } - @JsonProperty(access = JsonProperty.Access.READ_ONLY) public Long getId(){ return id; }