From e58d7117cf9bbc6e7741bbc223924daa73fc5dfc Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 2 Jun 2024 21:56:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB=D0=B5=D1=80?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/games/api/GameController.java | 37 +++++----- .../com/example/demo/games/api/GameDTO.java | 25 ------- .../com/example/demo/games/api/GameDto.java | 54 ++++++++++++++ .../demo/plays/api/PlayController.java | 63 ++++++++++++++++ .../com/example/demo/plays/api/PlayDto.java | 55 ++++++++++++++ .../demo/plays/api/TableController.java | 74 ------------------- .../com/example/demo/plays/api/TableDTO.java | 70 ------------------ .../demo/plays/service/PlayService.java | 8 +- .../demo/users/api/UserController.java | 69 ++++------------- .../users/api/{UserDTO.java => UserDto.java} | 28 ++++++- 10 files changed, 237 insertions(+), 246 deletions(-) delete mode 100644 demo/src/main/java/com/example/demo/games/api/GameDTO.java create mode 100644 demo/src/main/java/com/example/demo/games/api/GameDto.java create mode 100644 demo/src/main/java/com/example/demo/plays/api/PlayController.java create mode 100644 demo/src/main/java/com/example/demo/plays/api/PlayDto.java delete mode 100644 demo/src/main/java/com/example/demo/plays/api/TableController.java delete mode 100644 demo/src/main/java/com/example/demo/plays/api/TableDTO.java rename demo/src/main/java/com/example/demo/users/api/{UserDTO.java => UserDto.java} (59%) diff --git a/demo/src/main/java/com/example/demo/games/api/GameController.java b/demo/src/main/java/com/example/demo/games/api/GameController.java index e1dedd6..303af9f 100644 --- a/demo/src/main/java/com/example/demo/games/api/GameController.java +++ b/demo/src/main/java/com/example/demo/games/api/GameController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping(Constants.API_URL + "/games") +@RequestMapping(Constants.API_URL + "game") public class GameController { private final GameService gameService; private final ModelMapper modelMapper; @@ -20,39 +20,42 @@ public class GameController { this.modelMapper = modelMapper; } - private GameDTO toDTO(GameEntity entity){ - return modelMapper.map(entity, GameDTO.class); + private GameDto toDto(GameEntity entity){ + return modelMapper.map(entity, GameDto.class); } - private GameEntity toEntity(GameDTO dto){ + private GameEntity toEntity(GameDto dto){ return modelMapper.map(dto, GameEntity.class); } @GetMapping - public List getAll() - { - return gameService.getAll().stream().map(this::toDTO).toList(); + public List getAll() { + return gameService.getAll().stream().map(this::toDto).toList(); + } + + @GetMapping("/user/{userId}") + public List getByUser(@PathVariable Long userId) { + return gameService.getByUser(userId).stream().map(this::toDto).toList(); } @GetMapping("/{id}") - public GameDTO get(@PathVariable(name = "id") long id) - { - return toDTO(gameService.get(id)); + public GameDto get(@PathVariable(name = "id") long id) { + return toDto(gameService.get(id)); } - @PostMapping("/") - public GameDTO create(@RequestBody @Valid GameDTO dto) { - return toDTO(gameService.create(toEntity(dto))); + @PostMapping + public GameDto create(@RequestBody @Valid GameDto dto, @RequestParam Long userId) { + return toDto(gameService.create(userId, toEntity(dto))); } @PutMapping("/{id}") - public GameDTO update(@PathVariable(name = "id") long id, @RequestBody GameDTO dto) { - return toDTO(gameService.update(id, toEntity(dto))); + public GameDto update(@PathVariable(name = "id") long id, @RequestBody GameDto dto) { + return toDto(gameService.update(id, toEntity(dto))); } @DeleteMapping("/{id}") - public GameDTO delete(@PathVariable(name = "id") long id) + public GameDto delete(@PathVariable(name = "id") long id) { - return toDTO(gameService.delete(id)); + return toDto(gameService.delete(id)); } } \ No newline at end of file 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 deleted file mode 100644 index 2d74e9b..0000000 --- a/demo/src/main/java/com/example/demo/games/api/GameDTO.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.example.demo.games.api; - -import com.example.demo.users.api.UserDTO; -import com.fasterxml.jackson.annotation.JsonProperty; -import jakarta.validation.constraints.Min; -import jakarta.validation.constraints.NotNull; - -public class GameDTO { - private Long id; - @NotNull - @Min(2) - private String name; - public String getName() { - return name; - } - public void setName(String name){ - this.name = name; - } - public Long getId(){ - return id; - } - public void setId(Long id){ - this.id = id; - } -} 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 new file mode 100644 index 0000000..48b2930 --- /dev/null +++ b/demo/src/main/java/com/example/demo/games/api/GameDto.java @@ -0,0 +1,54 @@ +package com.example.demo.games.api; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; + +import java.util.List; + +public class GameDto { + private Long id; + @NotNull + @Min(2) + private String name; + + private String description; + + private Long user; + + private List plays; + + public String getName() { + return name; + } + public void setName(String name){ + this.name = name; + } + + public String getDescription(){ + return description; + } + public void setDescription(String description){ + this.description = description; + } + + public Long getUser(){ + return user; + } + public void setUser(Long user){ + this.user = user; + } + + public List getPlays(){ + return plays; + } + public void setPlays(List plays){ + this.plays = plays; + } + + public Long getId(){ + return id; + } + public void setId(Long id){ + this.id = id; + } +} diff --git a/demo/src/main/java/com/example/demo/plays/api/PlayController.java b/demo/src/main/java/com/example/demo/plays/api/PlayController.java new file mode 100644 index 0000000..f6593f1 --- /dev/null +++ b/demo/src/main/java/com/example/demo/plays/api/PlayController.java @@ -0,0 +1,63 @@ +package com.example.demo.plays.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.plays.model.PlayEntity; +import com.example.demo.plays.service.PlayService; +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.*; + +import java.util.List; + +@RestController +@RequestMapping(Constants.API_URL + "/tables") +public class PlayController { + private final PlayService playService; + private final ModelMapper modelMapper; + + public PlayController(PlayService playService, ModelMapper modelMapper){ + + this.playService = playService; + this.modelMapper = modelMapper; + } + + private PlayDto toDto(PlayEntity entity) { + return modelMapper.map(entity, PlayDto.class); + } + private PlayEntity toEntity(PlayDto dto){ + return modelMapper.map(dto, PlayEntity.class); + } + + @GetMapping + public List getAll() + { + return playService.getAll().stream().map(this::toDto).toList(); + } + + @GetMapping("/{id}") + public PlayDto get(@PathVariable(name = "id") long id) + { + return toDto(playService.get(id)); + } + + @PostMapping("/") + public PlayDto create(@RequestBody @Valid PlayDto dto) { + + return toDto(playService.create(toEntity(dto))); + } + + @PutMapping("/{id}") + public PlayDto update(@PathVariable(name = "id") long id, @RequestBody PlayDto dto) { + return toDto(playService.update(id, toEntity(dto))); + } + + @DeleteMapping("/{id}") + public PlayDto delete(@PathVariable(name = "id") long id) + { + return toDto(playService.delete(id)); + } +} diff --git a/demo/src/main/java/com/example/demo/plays/api/PlayDto.java b/demo/src/main/java/com/example/demo/plays/api/PlayDto.java new file mode 100644 index 0000000..58632e5 --- /dev/null +++ b/demo/src/main/java/com/example/demo/plays/api/PlayDto.java @@ -0,0 +1,55 @@ +package com.example.demo.plays.api; + +import jakarta.validation.constraints.NotNull; + +import java.util.Date; +import java.util.List; + +public class PlayDto { + private Long id; + + private String description; + + @NotNull + private Date date; + + @NotNull + private Long game; + + private List users; + + public String getDescription(){ + return description; + } + public void setDescription(String description){ + this.description = description; + } + + public Date getDate(){ + return date; + } + public void setDate(Date date){ + this.date = date; + } + + public Long getGame(){ + return game; + } + public void setGame(Long game){ + this.game = game; + } + + public List getUsers(){ + return users; + } + public void setUsers(List users){ + this.users = users; + } + + public Long getId(){ + return id; + } + public void setId(Long id){ + this.id = id; + } +} diff --git a/demo/src/main/java/com/example/demo/plays/api/TableController.java b/demo/src/main/java/com/example/demo/plays/api/TableController.java deleted file mode 100644 index 18deeca..0000000 --- a/demo/src/main/java/com/example/demo/plays/api/TableController.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.example.demo.plays.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.plays.model.PlayEntity; -import com.example.demo.plays.service.PlayService; -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.*; - -import java.util.List; - -@RestController -@RequestMapping(Constants.API_URL + "/tables") -public class TableController { - private final PlayService playService; - private final ModelMapper modelMapper; - - public TableController(PlayService playService, ModelMapper modelMapper){ - - this.playService = playService; - this.modelMapper = modelMapper; - } - - private TableDTO toDTO(PlayEntity entity) { - return new TableDTO( - entity.getId(), - 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 PlayEntity toEntity(TableDTO dto){ - PlayEntity entity = modelMapper.map(dto, PlayEntity.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 - public List getAll() - { - return playService.getAll().stream().map(this::toDTO).toList(); - } - - @GetMapping("/{id}") - public TableDTO get(@PathVariable(name = "id") long id) - { - return toDTO(playService.get(id)); - } - - @PostMapping("/") - public TableDTO create(@RequestBody @Valid TableDTO dto) { - return toDTO(playService.create(toEntity(dto))); - } - - @PutMapping("/{id}") - public TableDTO update(@PathVariable(name = "id") long id, @RequestBody TableDTO dto) { - return toDTO(playService.update(id, toEntity(dto))); - } - - @DeleteMapping("/{id}") - public TableDTO delete(@PathVariable(name = "id") long id) - { - return toDTO(playService.delete(id)); - } -} diff --git a/demo/src/main/java/com/example/demo/plays/api/TableDTO.java b/demo/src/main/java/com/example/demo/plays/api/TableDTO.java deleted file mode 100644 index 147b818..0000000 --- a/demo/src/main/java/com/example/demo/plays/api/TableDTO.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.example.demo.plays.api; - -import com.example.demo.games.api.GameDTO; -import com.example.demo.users.api.UserDTO; -import jakarta.validation.constraints.NotNull; - -import java.util.Date; -import java.util.List; - -public class TableDTO { - private Long id; - private String description; - @NotNull - private Date date; - @NotNull - private UserDTO creator; - @NotNull - private GameDTO game; - private List gamers; - - public TableDTO(Long id, String description, Date date, UserDTO creator, GameDTO game, List gamers){ - this.id = id; - this.description = description; - this.date = date; - this.creator = creator; - this.game = game; - this.gamers = gamers; - } - - public String getDescription(){ - return description; - } - public void setDescription(String description){ - this.description = description; - } - - public Date getDate(){ - return date; - } - public void setDate(Date date){ - this.date = date; - } - - public UserDTO getCreator(){ - return creator; - } - public void setCreator(UserDTO creator){ - this.creator = creator; - } - - public GameDTO getGame(){ - return game; - } - public void setGame(GameDTO game){ - this.game = game; - } - - public List getGamers(){ - return gamers; - } - public void setGamers(List gamers){ - this.gamers = gamers; - } - public Long getId(){ - return id; - } - public void setId(Long id){ - this.id = id; - } -} diff --git a/demo/src/main/java/com/example/demo/plays/service/PlayService.java b/demo/src/main/java/com/example/demo/plays/service/PlayService.java index 40a90bd..b8cc0ba 100644 --- a/demo/src/main/java/com/example/demo/plays/service/PlayService.java +++ b/demo/src/main/java/com/example/demo/plays/service/PlayService.java @@ -51,18 +51,18 @@ public class PlayService { } @Transactional - public PlayEntity create(long gameId, Set usersId, PlayEntity entity){ + public PlayEntity create(PlayEntity entity){ if (entity == null) { throw new IllegalArgumentException("Entity is null"); } - final GameEntity existGame = gameService.get(gameId); + final GameEntity existGame = gameService.get(entity.getGame().getId()); entity.setGame(existGame); Set users = new HashSet() { }; - usersId.forEach(userId -> { - final UserEntity existUser = userService.get(userId); + entity.getUsers().forEach(user -> { + final UserEntity existUser = userService.get(user.getId()); users.add(existUser); }); diff --git a/demo/src/main/java/com/example/demo/users/api/UserController.java b/demo/src/main/java/com/example/demo/users/api/UserController.java index 6a85d35..cced88d 100644 --- a/demo/src/main/java/com/example/demo/users/api/UserController.java +++ b/demo/src/main/java/com/example/demo/users/api/UserController.java @@ -20,81 +20,42 @@ public class UserController { this.modelMapper = modelMapper; } - private UserDTO toDTO(UserEntity entity){ - UserDTO dto = new UserDTO(); - dto.setId(entity.getId()); - dto.setName(entity.getName()); - dto.setLogin(entity.getLogin()); - dto.setPassword(entity.getPassword()); - return dto; + private UserDto toDto(UserEntity entity) { + return modelMapper.map(entity, UserDto.class); } - private UserEntity toEntity(UserDTO dto){ - return new UserEntity(dto.getId(), dto.getName(), dto.getLogin(), dto.getPassword()); + private UserEntity toEntity(UserDto dto) { + return modelMapper.map(dto, UserEntity.class); } @GetMapping - public List getAll() + public List getAll() { return userService.getAll() .stream() - .map(this::toDTO) + .map(this::toDto) .toList(); } @GetMapping("/{id}") - public UserDTO get(@PathVariable(name = "id") long id) - { - return toDTO(userService.get(id)); + public UserDto get(@PathVariable(name = "id") long id) { + return toDto(userService.get(id)); } @PostMapping - public UserDTO create(@RequestBody @Valid UserDTO dto) { - return toDTO(userService.create(toEntity(dto))); + public UserDto create(@RequestBody @Valid UserDto dto) { + return toDto(userService.create(toEntity(dto))); } @PutMapping("/{id}") - public UserDTO update( + public UserDto update( @PathVariable(name = "id") long id, - @RequestBody UserDTO dto) { - return toDTO(userService.update(id, toEntity(dto))); + @RequestBody UserDto dto) { + return toDto(userService.update(id, toEntity(dto))); } @DeleteMapping("/{id}") - public UserDTO delete(@PathVariable(name = "id") long id) - { - return toDTO(userService.delete(id)); - } - - @GetMapping("/{id}/subscription") - public List getUserSubscriptions(@PathVariable(name = "id") Long id) { - return userService.getUserSubscriptions(id).stream() - .map(this::toSubscriptionDto) - .toList(); - } - - @PostMapping("/{id}/subscription") - public List enableUserSubscriptions( - @PathVariable(name = "id") Long id, - @RequestParam(name = "subscriptions", defaultValue = "") List subscriptionsIds) { - return userService.enableUserSubscriptions(id, subscriptionsIds).stream() - .map(this::toSubscriptionDto) - .toList(); - } - - @DeleteMapping("/{id}/subscription") - public List disableUserSubscriptions( - @PathVariable(name = "id") Long id, - @RequestParam(name = "subscriptions", defaultValue = "") List subscriptionsIds) { - return userService.disableUserSubscriptions(id, subscriptionsIds).stream() - .map(this::toSubscriptionDto) - .toList(); - } - - @DeleteMapping("/{id}/subscription/all") - public List deleteAllUserSubscriptions(@PathVariable(name = "id") Long id) { - return userService.deleteAllUserSubscriptions(id).stream() - .map(this::toSubscriptionDto) - .toList(); + public UserDto delete(@PathVariable(name = "id") long id) { + return toDto(userService.delete(id)); } } 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 similarity index 59% rename from demo/src/main/java/com/example/demo/users/api/UserDTO.java rename to demo/src/main/java/com/example/demo/users/api/UserDto.java index a7fcfb4..335aae9 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 @@ -1,22 +1,31 @@ package com.example.demo.users.api; - import jakarta.validation.constraints.*; -public class UserDTO { +import java.util.List; + + +public class UserDto { private Long id; + @NotNull @Min(2) private String name; + @NotBlank @Min(4) private String login; + private List games; + + private List plays; + public String getName() { return name; } public void setName(String name){ this.name = name; } + public String getLogin() { return login; @@ -24,6 +33,21 @@ public class UserDTO { public void setLogin(String login){ this.login = login; } + + public List getGames() { + return games; + } + public void setGames(List games){ + this.games = games; + } + + public List getPlays(){ + return plays; + } + public void setPlays(List plays){ + this.plays = plays; + } + public Long getId(){ return id; }