оно работает!!!

This commit is contained in:
bekodeg 2024-06-07 18:14:45 +04:00
parent 4f8b943108
commit 654f129de6
19 changed files with 195 additions and 166 deletions

View File

@ -1,4 +1,4 @@
package com.example.demo.core.config;
package com.example.demo.config;
public class Constants {
public static final String SEQUENCE_NAME = "hibernate_sequence";

View File

@ -1,4 +1,4 @@
package com.example.demo.core.config;
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;

View File

@ -2,7 +2,7 @@ package com.example.demo.controllers;
import java.util.List;
import com.example.demo.core.config.Constants;
import com.example.demo.config.Constants;
import com.example.demo.dtos.GameDto;
import com.example.demo.dtos.PlayDto;
import com.example.demo.services.GameService;
@ -55,14 +55,4 @@ public class CustomerController {
public GameDto createGame(@PathVariable(name = "id") long id, @RequestBody GameDto game){
return new GameDto(gameService.addGame(id, game.getName(), game.getDescription()));
}
@GetMapping("/{id}/games")
public List<GameDto> getGames(@PathVariable(name = "id") long id){
return customerService.findCustomer(id).getGames().stream().map(GameDto::new).toList();
}
@GetMapping("/{id}/plays")
public List<PlayDto> getPlays(@PathVariable(name = "id") long id){
return customerService.findCustomer(id).getPlays().stream().map(PlayDto::new).toList();
}
}

View File

@ -1,6 +1,6 @@
package com.example.demo.controllers;
import com.example.demo.core.config.Constants;
import com.example.demo.config.Constants;
import com.example.demo.dtos.GameDto;
import com.example.demo.dtos.PlayDto;
import com.example.demo.services.GameService;

View File

@ -1,9 +1,8 @@
package com.example.demo.controllers;
import com.example.demo.core.config.Constants;
import com.example.demo.config.Constants;
import com.example.demo.dtos.CustomerDto;
import com.example.demo.dtos.PlayDto;
import com.example.demo.models.Customer;
import com.example.demo.services.PlayService;
import org.springframework.web.bind.annotation.*;
@ -46,9 +45,4 @@ public class PlayController {
public boolean addCustomer(@PathVariable(name = "id") long id, @RequestParam long customerId){
return playService.addCustomer(id, customerId);
}
@GetMapping("/{id}/customers")
public List<CustomerDto> getCustomers(@PathVariable(name = "id") long id){
return playService.findPlay(id).getCustomers().stream().map(CustomerDto::new).toList();
}
}

View File

@ -1,5 +1,7 @@
package com.example.demo.dtos;
import com.example.demo.models.Customer;
import com.example.demo.models.Game;
import com.example.demo.models.Play;
import jakarta.validation.constraints.*;
import java.util.ArrayList;
@ -13,9 +15,9 @@ public class CustomerDto {
@Min(2)
private String name;
private List<GameDto> games;
private List<Long> gamesId;
private List<PlayDto> plays;
private List<Long> playsId;
public CustomerDto(){
@ -28,18 +30,18 @@ public class CustomerDto {
var existGames = customer.getGames();
if (existGames != null && !existGames.isEmpty()){
games = existGames.stream().map(GameDto::new).toList();
gamesId = existGames.stream().map(Game::getId).toList();
}
else {
games = new ArrayList<>();
gamesId = new ArrayList<>();
}
var existPlays = customer.getPlays();
if (existPlays != null && !existPlays.isEmpty()){
plays = existPlays.stream().map(PlayDto::new).toList();
playsId = existPlays.stream().map(Play::getId).toList();
}
else {
plays = new ArrayList<>();
playsId = new ArrayList<>();
}
}
public String getName() {
@ -49,18 +51,18 @@ public class CustomerDto {
this.name = name;
}
public List<GameDto> getGames() {
return games;
public List<Long> getGamesId() {
return gamesId;
}
public void setGames(List<GameDto> games){
this.games = games;
public void setGamesId(List<Long> gamesId){
this.gamesId = gamesId;
}
public List<PlayDto> getPlays(){
return plays;
public List<Long> getPlaysId(){
return playsId;
}
public void setPlays(List<PlayDto> plays){
this.plays = plays;
public void setPlaysId(List<Long> playsId){
this.playsId = playsId;
}
public Long getId(){

View File

@ -1,6 +1,7 @@
package com.example.demo.dtos;
import com.example.demo.models.Game;
import com.example.demo.models.Play;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
@ -15,9 +16,9 @@ public class GameDto {
private String description;
private CustomerDto customer;
private Long customerId;
private List<PlayDto> plays;
private List<Long> playsId;
public GameDto(){
@ -28,20 +29,14 @@ public class GameDto {
name = game.getName();
description = game.getDescription();
var customerExist = game.getCustomer();
if (customerExist != null){
customer = new CustomerDto(customerExist);
}
else {
customer = null;
}
customerId = game.getCustomerId();
var playsExist = game.getPlays();
if (playsExist != null && !playsExist.isEmpty()){
plays = playsExist.stream().map(PlayDto::new).toList();
playsId = playsExist.stream().map(Play::getId).toList();
}
else {
plays = new ArrayList<>();
playsId = new ArrayList<>();
}
}
@ -59,18 +54,18 @@ public class GameDto {
this.description = description;
}
public CustomerDto getCustomer(){
return customer;
public Long getCustomerId(){
return customerId;
}
public void setCustomer(CustomerDto customer){
this.customer = customer;
public void setCustomerId(Long customerId){
this.customerId = customerId;
}
public List<PlayDto> getPlays(){
return plays;
public List<Long> getPlaysId(){
return playsId;
}
public void setPlays(List<PlayDto> plays){
this.plays = plays;
public void setPlaysId(List<Long> playsId){
this.playsId = playsId;
}
public Long getId(){

View File

@ -1,5 +1,6 @@
package com.example.demo.dtos;
import com.example.demo.models.Customer;
import com.example.demo.models.Play;
import jakarta.validation.constraints.NotNull;
@ -16,9 +17,9 @@ public class PlayDto {
private Date date;
@NotNull
private GameDto game;
private Long gameId;
private List<CustomerDto> customers;
private List<Long> customersId;
public PlayDto(){
@ -29,20 +30,14 @@ public class PlayDto {
description = play.getDescription();
date = play.getDate();
var existGame = play.getGame();
if (existGame != null){
game = new GameDto(existGame);
}
else {
game = null;
}
gameId = play.getGameId();
var existCustomers = play.getCustomers();
if (existCustomers != null && !existCustomers.isEmpty()){
customers = existCustomers.stream().map(CustomerDto::new).toList();
customersId = existCustomers.stream().map(Customer::getId).toList();
}
else {
customers = new ArrayList<>();
customersId = new ArrayList<>();
}
}
@ -60,18 +55,18 @@ public class PlayDto {
this.date = date;
}
public GameDto getGame(){
return game;
public Long getGameId(){
return gameId;
}
public void setGame(GameDto game){
this.game = game;
public void setGameId(Long gameId){
this.gameId = gameId;
}
public List<CustomerDto> getCustomers(){
return customers;
public List<Long> getCustomersId(){
return customersId;
}
public void setCustomers(List<CustomerDto> customers){
this.customers = customers;
public void setCustomersId(List<Long> customersId){
this.customersId = customersId;
}
public Long getId(){

View File

@ -1,4 +1,4 @@
package com.example.demo.core.error;
package com.example.demo.errors;
public class NotFoundException extends RuntimeException {
public <T> NotFoundException(Class<T> clazz, Long id) {

View File

@ -18,6 +18,9 @@ public class Customer {
private List<Game> games = new ArrayList<>();
@ManyToMany
@JoinTable(name="plays_customers",
joinColumns = @JoinColumn(name="customerId", referencedColumnName="id"),
inverseJoinColumns= @JoinColumn(name="playId", referencedColumnName="id") )
@OrderBy("date desc")
private List<Play> plays = new ArrayList<>();

View File

@ -19,7 +19,7 @@ public class Game {
private String description;
@ManyToOne
@JoinColumn(name = "сustomerId", updatable = false, insertable = false)
@JoinColumn(name = "customerId", updatable = false, insertable = false)
private Customer customer;
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
@ -75,6 +75,10 @@ public class Game {
this.customerId = customer.getId();
}
public long getCustomerId(){
return customerId;
}
public Set<Play> getPlays(){
return plays;
}

View File

@ -25,7 +25,10 @@ public class Play {
@JoinColumn(name = "gameId", updatable = false, insertable = false)
private Game game;
@ManyToMany()
@ManyToMany
@JoinTable(name="plays_customers",
joinColumns = @JoinColumn(name="playId", referencedColumnName="id"),
inverseJoinColumns= @JoinColumn(name="customerId", referencedColumnName="id") )
@OrderBy("name asc")
private List<Customer> customers = new ArrayList<>();
@ -79,6 +82,10 @@ public class Play {
this.gameId = game.getId();
}
public Long getGameId(){
return gameId;
}
public List<Customer> getCustomers(){
return customers;
}

View File

@ -0,0 +1,22 @@
package com.example.demo.repositories;
import com.example.demo.models.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
import java.util.Optional;
public interface CustomerRepository extends JpaRepository<Customer, Long> {
@Query("select distinct c from Customer c left join fetch c.plays where c.id = ?1")
Optional<Customer> findByIdWidthPlays(Long id);
@Query("select distinct c from Customer c left join fetch c.games where c.id = ?1")
Optional<Customer> findByIdWidthGames(Long id);
@Query("select distinct c from Customer c left join fetch c.plays")
List<Customer> findAllWidthPlays();
@Query("select distinct c from Customer c left join fetch c.games")
List<Customer> findAllWidthGames();
}

View File

@ -0,0 +1,17 @@
package com.example.demo.repositories;
import com.example.demo.models.Game;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
import java.util.Optional;
public interface GameRepository extends CrudRepository<Game, Long> {
@Query("select distinct g from Game g left join fetch g.customer left join fetch g.plays where g.id = ?1")
Optional<Game> findById(Long id);
@Query("select distinct g from Game g left join fetch g.customer left join fetch g.plays")
List<Game> findAll();
}

View File

@ -0,0 +1,18 @@
package com.example.demo.repositories;
import com.example.demo.models.Game;
import com.example.demo.models.Play;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
import java.util.Optional;
public interface PlayRepository extends CrudRepository<Play, Long> {
@Query("select distinct p from Play p left join fetch p.customers c where p.id = ?1")
Optional<Play> findById(Long id);
@Query("select distinct p from Play p left join fetch p.customers c")
List<Play> findAll();
}

View File

@ -1,56 +1,53 @@
package com.example.demo.services;
import com.example.demo.errors.NotFoundException;
import com.example.demo.models.Customer;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import com.example.demo.repositories.CustomerRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class CustomerService {
@PersistenceContext
private EntityManager em;
private final CustomerRepository repository;
public CustomerService(CustomerRepository repository){
this.repository = repository;
}
@Transactional
public Customer addCustomer(String name) {
if(!StringUtils.hasText(name)){
throw new IllegalArgumentException("Empty name");
}
final Customer customer = new Customer(name);
em.persist(customer);
return customer;
Customer customer = new Customer(name);
return repository.save(customer);
}
@Transactional
public Customer findCustomer(Long id){
final Customer customer = em.find(Customer.class, id);
if (customer == null){
throw new EntityNotFoundException(String.format("customer with id [%s] is not found", id));
}
return customer;
var customer = repository.findByIdWidthPlays(id);
return customer != null ?
repository.findByIdWidthGames(id).orElseThrow(() -> new NotFoundException(Customer.class, id)) :
customer.orElseThrow(() -> new NotFoundException(Customer.class, id));
}
@Transactional
public List<Customer> getAllCustomers(){
return em.createQuery("SELECT u FROM Customer u", Customer.class).getResultList();
var customers = repository.findAllWidthGames();
return !customers.isEmpty() ? repository.findAllWidthPlays() : customers;
}
@Transactional
public Customer updateCustomer(Long id, String name){
final Customer currentCustomer = findCustomer(id);
if(StringUtils.hasText(name))
currentCustomer.setName(name);
return currentCustomer;
Customer customer = findCustomer(id);
customer.setName(name);
return repository.save(customer);
}
@Transactional
public Customer deleteCustomer(Long id){
final Customer currentCustomer = findCustomer(id);
em.remove(currentCustomer);
return currentCustomer;
Customer customer = findCustomer(id);
repository.delete(customer);
return customer;
}
}

View File

@ -1,69 +1,58 @@
package com.example.demo.services;
import com.example.demo.models.Game;
import com.example.demo.errors.NotFoundException;
import com.example.demo.models.Customer;
import com.example.demo.models.Game;
import com.example.demo.repositories.GameRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
public class GameService {
@PersistenceContext
private EntityManager em;
private final GameRepository repository;
private final CustomerService customerService;
public GameService(CustomerService customerService){
public GameService(GameRepository repository, CustomerService customerService){
this.repository = repository;
this.customerService = customerService;
}
@Transactional
public Game addGame(Long customerId, String name, String description){
if(!StringUtils.hasText(name)) {
throw new IllegalArgumentException("Empty name");
}
final Customer customer = customerService.findCustomer(customerId);
if (customer == null){
throw new IllegalArgumentException("customerId invalid");
}
final Game game = new Game(name, description, customer);
em.persist(game);
return game;
Customer customer = customerService.findCustomer(customerId);
Game game = new Game(name, description, customer);
return repository.save(game);
}
@Transactional
public Game findGame(Long id){
final Game game = em.find(Game.class, id);
if(game == null){
throw new EntityNotFoundException(String.format("Game with id [%s] is not found", id));
}
return game;
return repository.findById(id).orElseThrow(
() -> new NotFoundException(Game.class, id));
}
@Transactional
public List<Game> getAllGames(){
return em.createQuery("SELECT g FROM Game g", Game.class).getResultList();
return repository.findAll();
}
@Transactional
public Game updateGame(Long id, String name, String description){
final Game currentGame = findGame(id);
if(StringUtils.hasText(name))
currentGame.setName(name);
if(StringUtils.hasText(description))
currentGame.setDescription(description);
return currentGame;
Game game = findGame(id);
game.setName(name);
game.setDescription(description);
return repository.save(game);
}
@Transactional
public Game deleteGame(Long id){
final Game currentGame = findGame(id);
em.remove(currentGame);
return currentGame;
Game game = findGame(id);
repository.delete(game);
return game;
}
}

View File

@ -1,81 +1,74 @@
package com.example.demo.services;
import com.example.demo.errors.NotFoundException;
import com.example.demo.models.Customer;
import com.example.demo.models.Game;
import com.example.demo.models.Play;
import com.example.demo.models.Customer;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityNotFoundException;
import jakarta.persistence.PersistenceContext;
import com.example.demo.repositories.PlayRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.List;
@Service
public class PlayService {
@PersistenceContext
private EntityManager em;
private final PlayRepository repository;
private final GameService gameService;
private final CustomerService customerService;
public PlayService(GameService gameService, CustomerService customerService){
public PlayService(PlayRepository repository, GameService gameService, CustomerService customerService){
this.repository = repository;
this.gameService = gameService;
this.customerService = customerService;
}
@Transactional
public Play addPlay(Long gameId, Date date, String description){
final Game game = gameService.findGame(gameId);
Game game = gameService.findGame(gameId);
if (game == null){
throw new IllegalArgumentException("gameId invalid");
}
final Play Play = new Play(game, date, description);
em.persist(Play);
return Play;
Play play = new Play(game, date, description);
return repository.save(play);
}
@Transactional
public Play findPlay(Long id){
final Play Play = em.find(Play.class, id);
if (Play == null){
throw new EntityNotFoundException(String.format("Play with id [%s] is not found", id));
}
return Play;
return repository.findById(id).orElseThrow(
() -> new NotFoundException(Play.class, id));
}
@Transactional
public List<Play> getAllPlays(){
return em.createQuery("SELECT p FROM Play p", Play.class).getResultList();
return repository.findAll();
}
@Transactional
public Play updatePlay(Long id, Date date, String description){
final Play currentPlay = findPlay(id);
Play play = findPlay(id);
if(StringUtils.hasText(description))
currentPlay.setDescription(description);
if(date != null)
currentPlay.setDate(date);
return currentPlay;
play.setDate(date);
play.setDescription(description);
return repository.save(play);
}
@Transactional
public Play deletePlay(Long id){
final Play currentPlay = findPlay(id);
em.remove(currentPlay);
return currentPlay;
Play play = findPlay(id);
repository.delete(play);
return play;
}
@Transactional
public boolean addCustomer(Long id, Long customerId){
final Play currentPlay = findPlay(id);
final Play play = findPlay(id);
final Customer customer = customerService.findCustomer(customerId);
return customer.addPlay(currentPlay);
if (customer.addPlay(play)){
repository.save(play);
return true;
}
return false;
}
}

View File

@ -9,3 +9,6 @@ spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true
spring.h2.console.settings.trace=false
spring.h2.console.settings.web-allow-others=false
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true