написал сервисы
This commit is contained in:
parent
7b44e6f770
commit
3a149f97fa
@ -1,7 +1,7 @@
|
|||||||
package com.example.demo.games.model;
|
package com.example.demo.games.model;
|
||||||
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
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 com.example.demo.users.model.UserEntity;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
@ -13,12 +13,16 @@ public class GameEntity extends BaseEntity {
|
|||||||
@Column(nullable = false, unique = true, length = 100)
|
@Column(nullable = false, unique = true, length = 100)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "user_game")
|
@Column(length = 500)
|
||||||
private List<UserEntity> users;
|
private String description;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
@JoinColumn(name = "userId")
|
||||||
|
private UserEntity user;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@OrderBy("date desc")
|
@OrderBy("date desc")
|
||||||
List<BookingEntity> bookings;
|
List<PlayEntity> plays;
|
||||||
|
|
||||||
public GameEntity() {
|
public GameEntity() {
|
||||||
}
|
}
|
||||||
@ -34,11 +38,24 @@ public class GameEntity extends BaseEntity {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UserEntity> getUsers(){
|
public String getDescription(){
|
||||||
return users;
|
return description;
|
||||||
|
}
|
||||||
|
public void setDescription(String description){
|
||||||
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BookingEntity> getBookings(){
|
public UserEntity getUser(){
|
||||||
return bookings;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package com.example.demo.games.repository;
|
|||||||
import com.example.demo.games.model.GameEntity;
|
import com.example.demo.games.model.GameEntity;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
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);
|
||||||
}
|
}
|
||||||
|
@ -5,39 +5,71 @@ import com.example.demo.games.model.GameEntity;
|
|||||||
import com.example.demo.games.repository.GameRepository;
|
import com.example.demo.games.repository.GameRepository;
|
||||||
import com.example.demo.users.model.UserEntity;
|
import com.example.demo.users.model.UserEntity;
|
||||||
import com.example.demo.users.repository.UserRepository;
|
import com.example.demo.users.repository.UserRepository;
|
||||||
|
import com.example.demo.users.service.UserService;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class GameService {
|
public class GameService {
|
||||||
private final GameRepository repository;
|
private final GameRepository repository;
|
||||||
public GameService(GameRepository repository){
|
private final UserService userService;
|
||||||
|
public GameService(
|
||||||
|
GameRepository repository,
|
||||||
|
UserService userService){
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<GameEntity> getAll(){
|
@Transactional(readOnly = true)
|
||||||
return repository.findAll();
|
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){
|
public GameEntity get(long id){
|
||||||
return Optional
|
return repository.findById(id)
|
||||||
.ofNullable(repository.get(id))
|
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
|
||||||
}
|
|
||||||
public GameEntity create(GameEntity entity){
|
|
||||||
return repository.create(entity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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){
|
public GameEntity update(long id, GameEntity entity){
|
||||||
final GameEntity existEntity = get(id);
|
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){
|
public GameEntity delete(Long id){
|
||||||
return repository.delete(repository.get(id));
|
final GameEntity existEntity = get(id);
|
||||||
|
repository.delete(existEntity);
|
||||||
|
return existEntity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.core.config.Constants;
|
||||||
import com.example.demo.games.api.GameDTO;
|
import com.example.demo.games.api.GameDTO;
|
||||||
import com.example.demo.games.model.GameEntity;
|
import com.example.demo.games.model.GameEntity;
|
||||||
import com.example.demo.tables.model.BookingEntity;
|
import com.example.demo.plays.model.PlayEntity;
|
||||||
import com.example.demo.tables.service.TableService;
|
import com.example.demo.plays.service.PlayService;
|
||||||
import com.example.demo.users.api.UserDTO;
|
import com.example.demo.users.api.UserDTO;
|
||||||
import com.example.demo.users.model.UserEntity;
|
import com.example.demo.users.model.UserEntity;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
@ -16,16 +16,16 @@ import java.util.List;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping(Constants.API_URL + "/tables")
|
@RequestMapping(Constants.API_URL + "/tables")
|
||||||
public class TableController {
|
public class TableController {
|
||||||
private final TableService tableService;
|
private final PlayService playService;
|
||||||
private final ModelMapper modelMapper;
|
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;
|
this.modelMapper = modelMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TableDTO toDTO(BookingEntity entity) {
|
private TableDTO toDTO(PlayEntity entity) {
|
||||||
return new TableDTO(
|
return new TableDTO(
|
||||||
entity.getId(),
|
entity.getId(),
|
||||||
entity.getDescription(),
|
entity.getDescription(),
|
||||||
@ -35,8 +35,8 @@ public class TableController {
|
|||||||
entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList()
|
entity.getGamers().stream().map(e -> modelMapper.map(e, UserDTO.class)).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
private BookingEntity toEntity(TableDTO dto){
|
private PlayEntity toEntity(TableDTO dto){
|
||||||
BookingEntity entity = modelMapper.map(dto, BookingEntity.class);
|
PlayEntity entity = modelMapper.map(dto, PlayEntity.class);
|
||||||
entity.setCreator(modelMapper.map(dto.getCreator(), UserEntity.class));
|
entity.setCreator(modelMapper.map(dto.getCreator(), UserEntity.class));
|
||||||
entity.setGame(modelMapper.map(dto.getGame(), GameEntity.class));
|
entity.setGame(modelMapper.map(dto.getGame(), GameEntity.class));
|
||||||
entity.setGamers(dto.getGamers().stream()
|
entity.setGamers(dto.getGamers().stream()
|
||||||
@ -47,28 +47,28 @@ public class TableController {
|
|||||||
@GetMapping
|
@GetMapping
|
||||||
public List<TableDTO> getAll()
|
public List<TableDTO> getAll()
|
||||||
{
|
{
|
||||||
return tableService.getAll().stream().map(this::toDTO).toList();
|
return playService.getAll().stream().map(this::toDTO).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public TableDTO get(@PathVariable(name = "id") long id)
|
public TableDTO get(@PathVariable(name = "id") long id)
|
||||||
{
|
{
|
||||||
return toDTO(tableService.get(id));
|
return toDTO(playService.get(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/")
|
@PostMapping("/")
|
||||||
public TableDTO create(@RequestBody @Valid TableDTO dto) {
|
public TableDTO create(@RequestBody @Valid TableDTO dto) {
|
||||||
return toDTO(tableService.create(toEntity(dto)));
|
return toDTO(playService.create(toEntity(dto)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public TableDTO update(@PathVariable(name = "id") long id, @RequestBody TableDTO dto) {
|
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}")
|
@DeleteMapping("/{id}")
|
||||||
public TableDTO delete(@PathVariable(name = "id") long id)
|
public TableDTO delete(@PathVariable(name = "id") long id)
|
||||||
{
|
{
|
||||||
return toDTO(tableService.delete(id));
|
return toDTO(playService.delete(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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.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.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 jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
@ -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.core.model.BaseEntity;
|
||||||
import com.example.demo.games.model.GameEntity;
|
import com.example.demo.games.model.GameEntity;
|
||||||
@ -6,33 +6,28 @@ import com.example.demo.users.model.UserEntity;
|
|||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.Set;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "booking")
|
@Table(name = "play")
|
||||||
public class BookingEntity extends BaseEntity {
|
public class PlayEntity extends BaseEntity {
|
||||||
@Column(nullable = false, length = 500)
|
@Column(length = 500)
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private Date date;
|
private Date date;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "creator_id")
|
@JoinColumn(name = "gameId")
|
||||||
private UserEntity creator;
|
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
@JoinColumn(name = "game_id")
|
|
||||||
private GameEntity game;
|
private GameEntity game;
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "book_user")
|
@ManyToMany
|
||||||
private List<UserEntity> users;
|
private Set<UserEntity> users;
|
||||||
|
|
||||||
public BookingEntity() {
|
public PlayEntity() {
|
||||||
|
|
||||||
}
|
}
|
||||||
public BookingEntity(String description,
|
public PlayEntity(Date date, String description){
|
||||||
Date date){
|
|
||||||
this.description = description;
|
this.description = description;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
}
|
}
|
||||||
@ -51,13 +46,6 @@ public class BookingEntity extends BaseEntity {
|
|||||||
this.date = date;
|
this.date = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity getCreator(){
|
|
||||||
return creator;
|
|
||||||
}
|
|
||||||
public void setCreator(UserEntity creator){
|
|
||||||
this.creator = creator;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GameEntity getGame(){
|
public GameEntity getGame(){
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
@ -65,10 +53,10 @@ public class BookingEntity extends BaseEntity {
|
|||||||
this.game = game;
|
this.game = game;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<UserEntity> getGamers(){
|
public Set<UserEntity> getUsers(){
|
||||||
return users;
|
return users;
|
||||||
}
|
}
|
||||||
public void setGamers(List<UserEntity> gamers){
|
public void setUsers(Set<UserEntity> gamers){
|
||||||
this.users = gamers;
|
this.users = gamers;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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);
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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> {
|
|
||||||
|
|
||||||
}
|
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +1,11 @@
|
|||||||
package com.example.demo.users.model;
|
package com.example.demo.users.model;
|
||||||
|
|
||||||
import com.example.demo.core.model.BaseEntity;
|
import com.example.demo.core.model.BaseEntity;
|
||||||
import jakarta.persistence.Column;
|
import com.example.demo.games.model.GameEntity;
|
||||||
import jakarta.persistence.Entity;
|
import com.example.demo.plays.model.PlayEntity;
|
||||||
import jakarta.persistence.Table;
|
import jakarta.persistence.*;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.List;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@ -14,6 +14,15 @@ public class UserEntity extends BaseEntity {
|
|||||||
private String name;
|
private String name;
|
||||||
@Column(nullable = false, unique = true, length = 50)
|
@Column(nullable = false, unique = true, length = 50)
|
||||||
private String login;
|
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() {
|
public UserEntity() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -37,4 +46,17 @@ public class UserEntity extends BaseEntity {
|
|||||||
this.login = login;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,4 +7,6 @@ import java.util.Optional;
|
|||||||
|
|
||||||
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
public interface UserRepository extends CrudRepository<UserEntity, Long> {
|
||||||
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
Optional<UserEntity> findByLoginIgnoreCase(String login);
|
||||||
|
|
||||||
|
Optional<UserEntity> findByGameId(long gameId);
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,28 @@
|
|||||||
package com.example.demo.users.service;
|
package com.example.demo.users.service;
|
||||||
|
|
||||||
import com.example.demo.core.error.NotFoundException;
|
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.model.UserEntity;
|
||||||
import com.example.demo.users.repository.UserRepository;
|
import com.example.demo.users.repository.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.awt.event.ItemEvent;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.stream.StreamSupport;
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
private final UserRepository repository;
|
private final UserRepository repository;
|
||||||
public UserService(UserRepository repository){
|
|
||||||
|
public UserService(
|
||||||
|
UserRepository repository,
|
||||||
|
GameService gameService,
|
||||||
|
PlayService playService
|
||||||
|
){
|
||||||
this.repository = repository;
|
this.repository = repository;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,13 +35,13 @@ public class UserService {
|
|||||||
|
|
||||||
@Transactional(readOnly = true)
|
@Transactional(readOnly = true)
|
||||||
public List<UserEntity> getAll() {
|
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){
|
public UserEntity get(Long id){
|
||||||
return Optional
|
return repository.findById(id)
|
||||||
.ofNullable(repository.get(id))
|
.orElseThrow(() -> new NotFoundException(UserEntity.class, id));
|
||||||
.orElseThrow(() -> new NotFoundException(id));
|
|
||||||
}
|
}
|
||||||
@Transactional
|
@Transactional
|
||||||
public UserEntity create(UserEntity entity) {
|
public UserEntity create(UserEntity entity) {
|
||||||
@ -42,15 +49,18 @@ public class UserService {
|
|||||||
throw new IllegalArgumentException("Entity is null");
|
throw new IllegalArgumentException("Entity is null");
|
||||||
}
|
}
|
||||||
checkLogin(entity.getLogin());
|
checkLogin(entity.getLogin());
|
||||||
repository.save(entity);
|
|
||||||
return repository.save(entity);
|
return repository.save(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public UserEntity update(long id, UserEntity entity) {
|
public UserEntity update(long id, UserEntity entity) {
|
||||||
final UserEntity existsEntity = get(id);
|
final UserEntity existsEntity = get(id);
|
||||||
|
|
||||||
checkLogin(entity.getLogin());
|
checkLogin(entity.getLogin());
|
||||||
existsEntity.setLogin(entity.getLogin());
|
existsEntity.setLogin(entity.getLogin());
|
||||||
|
existsEntity.setName(entity.getName());
|
||||||
|
|
||||||
repository.save(existsEntity);
|
repository.save(existsEntity);
|
||||||
return existsEntity;
|
return existsEntity;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user