From 3a149f97fa2ff97ae5dae4e94b5da880e3055613 Mon Sep 17 00:00:00 2001 From: bekodeg Date: Sun, 2 Jun 2024 16:11:54 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=81=D0=B5=D1=80=D0=B2=D0=B8=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/demo/games/model/GameEntity.java | 33 +++++-- .../demo/games/repository/GameRepository.java | 4 +- .../demo/games/service/GameService.java | 56 ++++++++--- .../api/TableController.java | 28 +++--- .../demo/{tables => plays}/api/TableDTO.java | 7 +- .../model/PlayEntity.java} | 36 +++----- .../demo/plays/repository/PlayRepository.java | 13 +++ .../demo/plays/service/PlayService.java | 92 +++++++++++++++++++ .../tables/repository/TableRepository.java | 8 -- .../demo/tables/service/TableService.java | 87 ------------------ .../example/demo/users/model/UserEntity.java | 30 +++++- .../demo/users/repository/UserRepository.java | 2 + .../demo/users/service/UserService.java | 26 ++++-- 13 files changed, 250 insertions(+), 172 deletions(-) rename demo/src/main/java/com/example/demo/{tables => plays}/api/TableController.java (69%) rename demo/src/main/java/com/example/demo/{tables => plays}/api/TableDTO.java (84%) rename demo/src/main/java/com/example/demo/{tables/model/BookingEntity.java => plays/model/PlayEntity.java} (55%) create mode 100644 demo/src/main/java/com/example/demo/plays/repository/PlayRepository.java create mode 100644 demo/src/main/java/com/example/demo/plays/service/PlayService.java delete mode 100644 demo/src/main/java/com/example/demo/tables/repository/TableRepository.java delete mode 100644 demo/src/main/java/com/example/demo/tables/service/TableService.java diff --git a/demo/src/main/java/com/example/demo/games/model/GameEntity.java b/demo/src/main/java/com/example/demo/games/model/GameEntity.java index 8d364fb..448fa71 100644 --- a/demo/src/main/java/com/example/demo/games/model/GameEntity.java +++ b/demo/src/main/java/com/example/demo/games/model/GameEntity.java @@ -1,7 +1,7 @@ package com.example.demo.games.model; import com.example.demo.core.model.BaseEntity; -import com.example.demo.tables.model.BookingEntity; +import com.example.demo.plays.model.PlayEntity; import com.example.demo.users.model.UserEntity; import jakarta.persistence.*; @@ -13,12 +13,16 @@ public class GameEntity extends BaseEntity { @Column(nullable = false, unique = true, length = 100) private String name; - @ManyToMany(mappedBy = "user_game") - private List users; + @Column(length = 500) + private String description; + + @ManyToOne + @JoinColumn(name = "userId") + private UserEntity user; @OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true) @OrderBy("date desc") - List bookings; + List plays; public GameEntity() { } @@ -34,11 +38,24 @@ public class GameEntity extends BaseEntity { this.name = name; } - public List getUsers(){ - return users; + public String getDescription(){ + return description; + } + public void setDescription(String description){ + this.description = description; } - public List getBookings(){ - return bookings; + public UserEntity getUser(){ + return user; + } + public void setUser(UserEntity user){ + this.user = user; + } + + public List getPlays(){ + return plays; + } + public void setPlays(List plays){ + this.plays = plays; } } diff --git a/demo/src/main/java/com/example/demo/games/repository/GameRepository.java b/demo/src/main/java/com/example/demo/games/repository/GameRepository.java index c09c98b..b3de906 100644 --- a/demo/src/main/java/com/example/demo/games/repository/GameRepository.java +++ b/demo/src/main/java/com/example/demo/games/repository/GameRepository.java @@ -2,6 +2,8 @@ package com.example.demo.games.repository; import com.example.demo.games.model.GameEntity; import org.springframework.data.repository.CrudRepository; -public interface GameRepository extends CrudRepository { +import java.util.List; +public interface GameRepository extends CrudRepository { + List findByUserId(long UserId); } diff --git a/demo/src/main/java/com/example/demo/games/service/GameService.java b/demo/src/main/java/com/example/demo/games/service/GameService.java index 91d14d7..9e87328 100644 --- a/demo/src/main/java/com/example/demo/games/service/GameService.java +++ b/demo/src/main/java/com/example/demo/games/service/GameService.java @@ -5,39 +5,71 @@ import com.example.demo.games.model.GameEntity; import com.example.demo.games.repository.GameRepository; import com.example.demo.users.model.UserEntity; import com.example.demo.users.repository.UserRepository; +import com.example.demo.users.service.UserService; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.stream.StreamSupport; @Service public class GameService { private final GameRepository repository; - public GameService(GameRepository repository){ + private final UserService userService; + public GameService( + GameRepository repository, + UserService userService){ this.repository = repository; + this.userService = userService; } - public List getAll(){ - return repository.findAll(); + @Transactional(readOnly = true) + public List getAll() + { + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); } + @Transactional(readOnly = true) + public List getByUser(long userId) + { + return repository.findByUserId(userId); + } + + @Transactional(readOnly = true) public GameEntity get(long id){ - return Optional - .ofNullable(repository.get(id)) - .orElseThrow(() -> new NotFoundException(id)); - } - public GameEntity create(GameEntity entity){ - return repository.create(entity); + return repository.findById(id) + .orElseThrow(() -> new NotFoundException(UserEntity.class, id)); } + @Transactional + public GameEntity create(long userId, GameEntity entity){ + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + + final UserEntity existsUser = userService.get(userId); + entity.setUser(existsUser); + + return repository.save(entity); + } + + @Transactional public GameEntity update(long id, GameEntity entity){ final GameEntity existEntity = get(id); - existEntity.setName(entity.getName().isEmpty() ? existEntity.getName() : entity.getName()); - return repository.update(existEntity); + + existEntity.setName(entity.getName()); + existEntity.setDescription(entity.getDescription()); + existEntity.setUser(entity.getUser()); + + return repository.save(existEntity); } + @Transactional public GameEntity delete(Long id){ - return repository.delete(repository.get(id)); + final GameEntity existEntity = get(id); + repository.delete(existEntity); + return existEntity; } } diff --git a/demo/src/main/java/com/example/demo/tables/api/TableController.java b/demo/src/main/java/com/example/demo/plays/api/TableController.java similarity index 69% rename from demo/src/main/java/com/example/demo/tables/api/TableController.java rename to demo/src/main/java/com/example/demo/plays/api/TableController.java index a61c40b..18deeca 100644 --- a/demo/src/main/java/com/example/demo/tables/api/TableController.java +++ b/demo/src/main/java/com/example/demo/plays/api/TableController.java @@ -1,10 +1,10 @@ -package com.example.demo.tables.api; +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.tables.model.BookingEntity; -import com.example.demo.tables.service.TableService; +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; @@ -16,16 +16,16 @@ import java.util.List; @RestController @RequestMapping(Constants.API_URL + "/tables") public class TableController { - private final TableService tableService; + private final PlayService playService; private final ModelMapper modelMapper; - public TableController(TableService tableService, ModelMapper modelMapper){ + public TableController(PlayService playService, ModelMapper modelMapper){ - this.tableService = tableService; + this.playService = playService; this.modelMapper = modelMapper; } - private TableDTO toDTO(BookingEntity entity) { + private TableDTO toDTO(PlayEntity entity) { return new TableDTO( entity.getId(), entity.getDescription(), @@ -35,8 +35,8 @@ public class TableController { entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList() ); } - private BookingEntity toEntity(TableDTO dto){ - BookingEntity entity = modelMapper.map(dto, BookingEntity.class); + 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() @@ -47,28 +47,28 @@ public class TableController { @GetMapping public List getAll() { - return tableService.getAll().stream().map(this::toDTO).toList(); + return playService.getAll().stream().map(this::toDTO).toList(); } @GetMapping("/{id}") public TableDTO get(@PathVariable(name = "id") long id) { - return toDTO(tableService.get(id)); + return toDTO(playService.get(id)); } @PostMapping("/") public TableDTO create(@RequestBody @Valid TableDTO dto) { - return toDTO(tableService.create(toEntity(dto))); + return toDTO(playService.create(toEntity(dto))); } @PutMapping("/{id}") public TableDTO update(@PathVariable(name = "id") long id, @RequestBody TableDTO dto) { - return toDTO(tableService.update(id, toEntity(dto))); + return toDTO(playService.update(id, toEntity(dto))); } @DeleteMapping("/{id}") public TableDTO delete(@PathVariable(name = "id") long id) { - return toDTO(tableService.delete(id)); + return toDTO(playService.delete(id)); } } diff --git a/demo/src/main/java/com/example/demo/tables/api/TableDTO.java b/demo/src/main/java/com/example/demo/plays/api/TableDTO.java similarity index 84% rename from demo/src/main/java/com/example/demo/tables/api/TableDTO.java rename to demo/src/main/java/com/example/demo/plays/api/TableDTO.java index 480c9d3..147b818 100644 --- a/demo/src/main/java/com/example/demo/tables/api/TableDTO.java +++ b/demo/src/main/java/com/example/demo/plays/api/TableDTO.java @@ -1,12 +1,7 @@ -package com.example.demo.tables.api; +package com.example.demo.plays.api; import com.example.demo.games.api.GameDTO; -import com.example.demo.games.model.GameEntity; -import com.example.demo.games.repository.GameRepository; import com.example.demo.users.api.UserDTO; -import com.example.demo.users.model.UserEntity; -import com.fasterxml.jackson.annotation.JsonProperty; -import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import java.util.Date; diff --git a/demo/src/main/java/com/example/demo/tables/model/BookingEntity.java b/demo/src/main/java/com/example/demo/plays/model/PlayEntity.java similarity index 55% rename from demo/src/main/java/com/example/demo/tables/model/BookingEntity.java rename to demo/src/main/java/com/example/demo/plays/model/PlayEntity.java index 3503699..a020e7f 100644 --- a/demo/src/main/java/com/example/demo/tables/model/BookingEntity.java +++ b/demo/src/main/java/com/example/demo/plays/model/PlayEntity.java @@ -1,4 +1,4 @@ -package com.example.demo.tables.model; +package com.example.demo.plays.model; import com.example.demo.core.model.BaseEntity; import com.example.demo.games.model.GameEntity; @@ -6,33 +6,28 @@ import com.example.demo.users.model.UserEntity; import jakarta.persistence.*; import java.util.Date; -import java.util.List; +import java.util.Set; @Entity -@Table(name = "booking") -public class BookingEntity extends BaseEntity { - @Column(nullable = false, length = 500) +@Table(name = "play") +public class PlayEntity extends BaseEntity { + @Column(length = 500) private String description; @Column(nullable = false) private Date date; @ManyToOne - @JoinColumn(name = "creator_id") - private UserEntity creator; - - @ManyToOne - @JoinColumn(name = "game_id") + @JoinColumn(name = "gameId") private GameEntity game; - @ManyToMany(mappedBy = "book_user") - private List users; + @ManyToMany + private Set users; - public BookingEntity() { + public PlayEntity() { } - public BookingEntity(String description, - Date date){ + public PlayEntity(Date date, String description){ this.description = description; this.date = date; } @@ -51,13 +46,6 @@ public class BookingEntity extends BaseEntity { this.date = date; } - public UserEntity getCreator(){ - return creator; - } - public void setCreator(UserEntity creator){ - this.creator = creator; - } - public GameEntity getGame(){ return game; } @@ -65,10 +53,10 @@ public class BookingEntity extends BaseEntity { this.game = game; } - public List getGamers(){ + public Set getUsers(){ return users; } - public void setGamers(List gamers){ + public void setUsers(Set gamers){ this.users = gamers; } } diff --git a/demo/src/main/java/com/example/demo/plays/repository/PlayRepository.java b/demo/src/main/java/com/example/demo/plays/repository/PlayRepository.java new file mode 100644 index 0000000..2960f1a --- /dev/null +++ b/demo/src/main/java/com/example/demo/plays/repository/PlayRepository.java @@ -0,0 +1,13 @@ +package com.example.demo.plays.repository; + +import com.example.demo.plays.model.PlayEntity; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; + +import java.util.List; + +public interface PlayRepository extends CrudRepository { + List findByUserId(long userId); + + List findByGameId(long gameId); +} 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 new file mode 100644 index 0000000..40a90bd --- /dev/null +++ b/demo/src/main/java/com/example/demo/plays/service/PlayService.java @@ -0,0 +1,92 @@ +package com.example.demo.plays.service; + +import com.example.demo.core.error.NotFoundException; +import com.example.demo.games.model.GameEntity; +import com.example.demo.games.service.GameService; +import com.example.demo.plays.model.PlayEntity; +import com.example.demo.plays.repository.PlayRepository; +import com.example.demo.users.model.UserEntity; +import com.example.demo.users.service.UserService; +import jakarta.persistence.Id; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.StreamSupport; + +@Service +public class PlayService { + private final PlayRepository repository; + private final UserService userService; + private final GameService gameService; + public PlayService(PlayRepository repository, UserService userService, GameService gameService) + { + this.repository = repository; + this.userService = userService; + this.gameService = gameService; + } + + @Transactional(readOnly = true) + public List getAll(){ + return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); + } + + @Transactional(readOnly = true) + public List getByUserId(long userId){ + return repository.findByUserId(userId); + } + + @Transactional(readOnly = true) + public List getByGameId(long gameId){ + return repository.findByGameId(gameId); + } + + @Transactional + public PlayEntity get(Long id){ + return repository.findById(id) + .orElseThrow(() -> new NotFoundException(UserEntity.class, id)); + } + + @Transactional + public PlayEntity create(long gameId, Set usersId, PlayEntity entity){ + if (entity == null) { + throw new IllegalArgumentException("Entity is null"); + } + + final GameEntity existGame = gameService.get(gameId); + entity.setGame(existGame); + + Set users = new HashSet() { + }; + usersId.forEach(userId -> { + final UserEntity existUser = userService.get(userId); + users.add(existUser); + }); + + entity.setUsers(users); + + return repository.save(entity); + } + + @Transactional + public PlayEntity update(Long id, PlayEntity entity){ + final PlayEntity existEntity = get(id); + + existEntity.setDescription(entity.getDescription()); + existEntity.setDate(entity.getDate()); + existEntity.setGame(entity.getGame()); + existEntity.setUsers(entity.getUsers()); + + return repository.save(existEntity); + } + + @Transactional + public PlayEntity delete(Long id){ + final PlayEntity existEntity = get(id); + repository.delete(existEntity); + return existEntity; + } +} diff --git a/demo/src/main/java/com/example/demo/tables/repository/TableRepository.java b/demo/src/main/java/com/example/demo/tables/repository/TableRepository.java deleted file mode 100644 index 8394aa2..0000000 --- a/demo/src/main/java/com/example/demo/tables/repository/TableRepository.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.example.demo.tables.repository; - -import com.example.demo.tables.model.BookingEntity; -import org.springframework.data.repository.CrudRepository; - -public interface TableRepository extends CrudRepository { - -} 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 deleted file mode 100644 index 634393e..0000000 --- a/demo/src/main/java/com/example/demo/tables/service/TableService.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.example.demo.tables.service; - -import com.example.demo.core.error.NotFoundException; -import com.example.demo.games.service.GameService; -import com.example.demo.tables.model.BookingEntity; -import com.example.demo.tables.repository.TableRepository; -import com.example.demo.users.service.UserService; -import org.springframework.stereotype.Service; - -import java.util.List; -import java.util.Optional; - -@Service -public class TableService { - private final TableRepository repository; - private final UserService userService; - private final GameService gameService; - public TableService(TableRepository repository, UserService userService, GameService gameService) - { - this.repository = repository; - this.userService = userService; - this.gameService = gameService; - } - - public List getAll(){ - return repository.getAll(); - } - - public BookingEntity get(Long id){ - return Optional - .ofNullable(repository.get(id)) - .orElseThrow(() -> new NotFoundException(id)); - } - public BookingEntity create(BookingEntity 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); - } - - public BookingEntity update(Long id, BookingEntity entity){ - final BookingEntity existEntity = get(id); - - existEntity.setDate(entity.getDate() == null ? existEntity.getDate() : entity.getDate()); - existEntity.setDescription( - 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() : - entity.getGamers() - .stream() - .map(g -> userService.get(g.getId())) - .toList() - ); - existEntity.setGame( - entity.getGame().getId() == null ? - existEntity.getGame() : - (gameService.get(entity.getGame().getId()))); - - return repository.update(existEntity); - } - - public BookingEntity delete(Long id){ - return repository.delete(repository.get(id)); - } -} diff --git a/demo/src/main/java/com/example/demo/users/model/UserEntity.java b/demo/src/main/java/com/example/demo/users/model/UserEntity.java index 06cb483..7ac52cf 100644 --- a/demo/src/main/java/com/example/demo/users/model/UserEntity.java +++ b/demo/src/main/java/com/example/demo/users/model/UserEntity.java @@ -1,11 +1,11 @@ package com.example.demo.users.model; import com.example.demo.core.model.BaseEntity; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Table; +import com.example.demo.games.model.GameEntity; +import com.example.demo.plays.model.PlayEntity; +import jakarta.persistence.*; -import java.util.Date; +import java.util.List; @Entity @Table(name = "users") @@ -14,6 +14,15 @@ public class UserEntity extends BaseEntity { private String name; @Column(nullable = false, unique = true, length = 50) private String login; + + @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) + @OrderBy("name asc") + private List games; + + @ManyToMany(mappedBy = "usersPlays") + @OrderBy("date desc") + private List plays; + public UserEntity() { } @@ -37,4 +46,17 @@ public class UserEntity extends BaseEntity { 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; + } } diff --git a/demo/src/main/java/com/example/demo/users/repository/UserRepository.java b/demo/src/main/java/com/example/demo/users/repository/UserRepository.java index 748f6a6..9b009a5 100644 --- a/demo/src/main/java/com/example/demo/users/repository/UserRepository.java +++ b/demo/src/main/java/com/example/demo/users/repository/UserRepository.java @@ -7,4 +7,6 @@ import java.util.Optional; public interface UserRepository extends CrudRepository { Optional findByLoginIgnoreCase(String login); + + Optional findByGameId(long gameId); } diff --git a/demo/src/main/java/com/example/demo/users/service/UserService.java b/demo/src/main/java/com/example/demo/users/service/UserService.java index 338dafe..bf8d9fe 100644 --- a/demo/src/main/java/com/example/demo/users/service/UserService.java +++ b/demo/src/main/java/com/example/demo/users/service/UserService.java @@ -1,21 +1,28 @@ package com.example.demo.users.service; import com.example.demo.core.error.NotFoundException; +import com.example.demo.games.repository.GameRepository; +import com.example.demo.games.service.GameService; +import com.example.demo.plays.repository.PlayRepository; +import com.example.demo.plays.service.PlayService; import com.example.demo.users.model.UserEntity; import com.example.demo.users.repository.UserRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.awt.event.ItemEvent; import java.util.List; -import java.util.Objects; import java.util.Optional; import java.util.stream.StreamSupport; @Service public class UserService { private final UserRepository repository; - public UserService(UserRepository repository){ + + public UserService( + UserRepository repository, + GameService gameService, + PlayService playService + ){ this.repository = repository; } @@ -28,13 +35,13 @@ public class UserService { @Transactional(readOnly = true) public List getAll() { - return StreamSupport.stream(repository.findAll().spliterator(), false).toList(); + return StreamSupport.stream( + repository.findAll().spliterator(), false).toList(); } public UserEntity get(Long id){ - return Optional - .ofNullable(repository.get(id)) - .orElseThrow(() -> new NotFoundException(id)); + return repository.findById(id) + .orElseThrow(() -> new NotFoundException(UserEntity.class, id)); } @Transactional public UserEntity create(UserEntity entity) { @@ -42,15 +49,18 @@ public class UserService { throw new IllegalArgumentException("Entity is null"); } checkLogin(entity.getLogin()); - repository.save(entity); + return repository.save(entity); } @Transactional public UserEntity update(long id, UserEntity entity) { final UserEntity existsEntity = get(id); + checkLogin(entity.getLogin()); existsEntity.setLogin(entity.getLogin()); + existsEntity.setName(entity.getName()); + repository.save(existsEntity); return existsEntity; }