написал сервисы

This commit is contained in:
bekodeg 2024-06-02 16:11:54 +04:00
parent 7b44e6f770
commit 3a149f97fa
13 changed files with 250 additions and 172 deletions

View File

@ -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<UserEntity> 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<BookingEntity> bookings;
List<PlayEntity> plays;
public GameEntity() {
}
@ -34,11 +38,24 @@ public class GameEntity extends BaseEntity {
this.name = name;
}
public List<UserEntity> getUsers(){
return users;
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public List<BookingEntity> getBookings(){
return bookings;
public UserEntity getUser(){
return user;
}
public void setUser(UserEntity user){
this.user = user;
}
public List<PlayEntity> getPlays(){
return plays;
}
public void setPlays(List<PlayEntity> plays){
this.plays = plays;
}
}

View File

@ -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<GameEntity, Long> {
import java.util.List;
public interface GameRepository extends CrudRepository<GameEntity, Long> {
List<GameEntity> findByUserId(long UserId);
}

View File

@ -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<GameEntity> getAll(){
return repository.findAll();
@Transactional(readOnly = true)
public List<GameEntity> getAll()
{
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
}
@Transactional(readOnly = true)
public List<GameEntity> 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;
}
}

View File

@ -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<TableDTO> 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));
}
}

View File

@ -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;

View File

@ -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<UserEntity> users;
@ManyToMany
private Set<UserEntity> 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<UserEntity> getGamers(){
public Set<UserEntity> getUsers(){
return users;
}
public void setGamers(List<UserEntity> gamers){
public void setUsers(Set<UserEntity> gamers){
this.users = gamers;
}
}

View File

@ -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<PlayEntity, Long> {
List<PlayEntity> findByUserId(long userId);
List<PlayEntity> findByGameId(long gameId);
}

View File

@ -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<PlayEntity> getAll(){
return StreamSupport.stream(repository.findAll().spliterator(), false).toList();
}
@Transactional(readOnly = true)
public List<PlayEntity> getByUserId(long userId){
return repository.findByUserId(userId);
}
@Transactional(readOnly = true)
public List<PlayEntity> 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<Long> usersId, PlayEntity entity){
if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
final GameEntity existGame = gameService.get(gameId);
entity.setGame(existGame);
Set<UserEntity> users = new HashSet<UserEntity>() {
};
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;
}
}

View File

@ -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<BookingEntity, Long> {
}

View File

@ -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<BookingEntity> 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));
}
}

View File

@ -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<GameEntity> games;
@ManyToMany(mappedBy = "usersPlays")
@OrderBy("date desc")
private List<PlayEntity> plays;
public UserEntity() {
}
@ -37,4 +46,17 @@ public class UserEntity extends BaseEntity {
this.login = login;
}
public List<GameEntity> getGames(){
return games;
}
public void setGames(List<GameEntity> games){
this.games = games;
}
public List<PlayEntity> getPlays(){
return plays;
}
public void setPlays(List<PlayEntity> plays){
this.plays = plays;
}
}

View File

@ -7,4 +7,6 @@ import java.util.Optional;
public interface UserRepository extends CrudRepository<UserEntity, Long> {
Optional<UserEntity> findByLoginIgnoreCase(String login);
Optional<UserEntity> findByGameId(long gameId);
}

View File

@ -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<UserEntity> 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;
}