оно работает!!!
This commit is contained in:
parent
4f8b943108
commit
654f129de6
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.core.config;
|
package com.example.demo.config;
|
||||||
|
|
||||||
public class Constants {
|
public class Constants {
|
||||||
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
public static final String SEQUENCE_NAME = "hibernate_sequence";
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.core.config;
|
package com.example.demo.config;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.lang.NonNull;
|
import org.springframework.lang.NonNull;
|
@ -2,7 +2,7 @@ package com.example.demo.controllers;
|
|||||||
|
|
||||||
import java.util.List;
|
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.GameDto;
|
||||||
import com.example.demo.dtos.PlayDto;
|
import com.example.demo.dtos.PlayDto;
|
||||||
import com.example.demo.services.GameService;
|
import com.example.demo.services.GameService;
|
||||||
@ -55,14 +55,4 @@ public class CustomerController {
|
|||||||
public GameDto createGame(@PathVariable(name = "id") long id, @RequestBody GameDto game){
|
public GameDto createGame(@PathVariable(name = "id") long id, @RequestBody GameDto game){
|
||||||
return new GameDto(gameService.addGame(id, game.getName(), game.getDescription()));
|
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package com.example.demo.controllers;
|
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.GameDto;
|
||||||
import com.example.demo.dtos.PlayDto;
|
import com.example.demo.dtos.PlayDto;
|
||||||
import com.example.demo.services.GameService;
|
import com.example.demo.services.GameService;
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package com.example.demo.controllers;
|
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.CustomerDto;
|
||||||
import com.example.demo.dtos.PlayDto;
|
import com.example.demo.dtos.PlayDto;
|
||||||
import com.example.demo.models.Customer;
|
|
||||||
import com.example.demo.services.PlayService;
|
import com.example.demo.services.PlayService;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@ -46,9 +45,4 @@ public class PlayController {
|
|||||||
public boolean addCustomer(@PathVariable(name = "id") long id, @RequestParam long customerId){
|
public boolean addCustomer(@PathVariable(name = "id") long id, @RequestParam long customerId){
|
||||||
return playService.addCustomer(id, 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package com.example.demo.dtos;
|
package com.example.demo.dtos;
|
||||||
import com.example.demo.models.Customer;
|
import com.example.demo.models.Customer;
|
||||||
|
import com.example.demo.models.Game;
|
||||||
|
import com.example.demo.models.Play;
|
||||||
import jakarta.validation.constraints.*;
|
import jakarta.validation.constraints.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -13,9 +15,9 @@ public class CustomerDto {
|
|||||||
@Min(2)
|
@Min(2)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private List<GameDto> games;
|
private List<Long> gamesId;
|
||||||
|
|
||||||
private List<PlayDto> plays;
|
private List<Long> playsId;
|
||||||
|
|
||||||
public CustomerDto(){
|
public CustomerDto(){
|
||||||
|
|
||||||
@ -28,18 +30,18 @@ public class CustomerDto {
|
|||||||
|
|
||||||
var existGames = customer.getGames();
|
var existGames = customer.getGames();
|
||||||
if (existGames != null && !existGames.isEmpty()){
|
if (existGames != null && !existGames.isEmpty()){
|
||||||
games = existGames.stream().map(GameDto::new).toList();
|
gamesId = existGames.stream().map(Game::getId).toList();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
games = new ArrayList<>();
|
gamesId = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
var existPlays = customer.getPlays();
|
var existPlays = customer.getPlays();
|
||||||
if (existPlays != null && !existPlays.isEmpty()){
|
if (existPlays != null && !existPlays.isEmpty()){
|
||||||
plays = existPlays.stream().map(PlayDto::new).toList();
|
playsId = existPlays.stream().map(Play::getId).toList();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
plays = new ArrayList<>();
|
playsId = new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public String getName() {
|
public String getName() {
|
||||||
@ -49,18 +51,18 @@ public class CustomerDto {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<GameDto> getGames() {
|
public List<Long> getGamesId() {
|
||||||
return games;
|
return gamesId;
|
||||||
}
|
}
|
||||||
public void setGames(List<GameDto> games){
|
public void setGamesId(List<Long> gamesId){
|
||||||
this.games = games;
|
this.gamesId = gamesId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PlayDto> getPlays(){
|
public List<Long> getPlaysId(){
|
||||||
return plays;
|
return playsId;
|
||||||
}
|
}
|
||||||
public void setPlays(List<PlayDto> plays){
|
public void setPlaysId(List<Long> playsId){
|
||||||
this.plays = plays;
|
this.playsId = playsId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId(){
|
public Long getId(){
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.demo.dtos;
|
package com.example.demo.dtos;
|
||||||
|
|
||||||
import com.example.demo.models.Game;
|
import com.example.demo.models.Game;
|
||||||
|
import com.example.demo.models.Play;
|
||||||
import jakarta.validation.constraints.Min;
|
import jakarta.validation.constraints.Min;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
@ -15,9 +16,9 @@ public class GameDto {
|
|||||||
|
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
private CustomerDto customer;
|
private Long customerId;
|
||||||
|
|
||||||
private List<PlayDto> plays;
|
private List<Long> playsId;
|
||||||
|
|
||||||
public GameDto(){
|
public GameDto(){
|
||||||
|
|
||||||
@ -28,20 +29,14 @@ public class GameDto {
|
|||||||
name = game.getName();
|
name = game.getName();
|
||||||
description = game.getDescription();
|
description = game.getDescription();
|
||||||
|
|
||||||
var customerExist = game.getCustomer();
|
customerId = game.getCustomerId();
|
||||||
if (customerExist != null){
|
|
||||||
customer = new CustomerDto(customerExist);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
customer = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var playsExist = game.getPlays();
|
var playsExist = game.getPlays();
|
||||||
if (playsExist != null && !playsExist.isEmpty()){
|
if (playsExist != null && !playsExist.isEmpty()){
|
||||||
plays = playsExist.stream().map(PlayDto::new).toList();
|
playsId = playsExist.stream().map(Play::getId).toList();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
plays = new ArrayList<>();
|
playsId = new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,18 +54,18 @@ public class GameDto {
|
|||||||
this.description = description;
|
this.description = description;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomerDto getCustomer(){
|
public Long getCustomerId(){
|
||||||
return customer;
|
return customerId;
|
||||||
}
|
}
|
||||||
public void setCustomer(CustomerDto customer){
|
public void setCustomerId(Long customerId){
|
||||||
this.customer = customer;
|
this.customerId = customerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<PlayDto> getPlays(){
|
public List<Long> getPlaysId(){
|
||||||
return plays;
|
return playsId;
|
||||||
}
|
}
|
||||||
public void setPlays(List<PlayDto> plays){
|
public void setPlaysId(List<Long> playsId){
|
||||||
this.plays = plays;
|
this.playsId = playsId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId(){
|
public Long getId(){
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package com.example.demo.dtos;
|
package com.example.demo.dtos;
|
||||||
|
|
||||||
|
import com.example.demo.models.Customer;
|
||||||
import com.example.demo.models.Play;
|
import com.example.demo.models.Play;
|
||||||
import jakarta.validation.constraints.NotNull;
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
|
||||||
@ -16,9 +17,9 @@ public class PlayDto {
|
|||||||
private Date date;
|
private Date date;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
private GameDto game;
|
private Long gameId;
|
||||||
|
|
||||||
private List<CustomerDto> customers;
|
private List<Long> customersId;
|
||||||
|
|
||||||
public PlayDto(){
|
public PlayDto(){
|
||||||
|
|
||||||
@ -29,20 +30,14 @@ public class PlayDto {
|
|||||||
description = play.getDescription();
|
description = play.getDescription();
|
||||||
date = play.getDate();
|
date = play.getDate();
|
||||||
|
|
||||||
var existGame = play.getGame();
|
gameId = play.getGameId();
|
||||||
if (existGame != null){
|
|
||||||
game = new GameDto(existGame);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
game = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
var existCustomers = play.getCustomers();
|
var existCustomers = play.getCustomers();
|
||||||
if (existCustomers != null && !existCustomers.isEmpty()){
|
if (existCustomers != null && !existCustomers.isEmpty()){
|
||||||
customers = existCustomers.stream().map(CustomerDto::new).toList();
|
customersId = existCustomers.stream().map(Customer::getId).toList();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
customers = new ArrayList<>();
|
customersId = new ArrayList<>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,18 +55,18 @@ public class PlayDto {
|
|||||||
this.date = date;
|
this.date = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameDto getGame(){
|
public Long getGameId(){
|
||||||
return game;
|
return gameId;
|
||||||
}
|
}
|
||||||
public void setGame(GameDto game){
|
public void setGameId(Long gameId){
|
||||||
this.game = game;
|
this.gameId = gameId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CustomerDto> getCustomers(){
|
public List<Long> getCustomersId(){
|
||||||
return customers;
|
return customersId;
|
||||||
}
|
}
|
||||||
public void setCustomers(List<CustomerDto> customers){
|
public void setCustomersId(List<Long> customersId){
|
||||||
this.customers = customers;
|
this.customersId = customersId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId(){
|
public Long getId(){
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package com.example.demo.core.error;
|
package com.example.demo.errors;
|
||||||
|
|
||||||
public class NotFoundException extends RuntimeException {
|
public class NotFoundException extends RuntimeException {
|
||||||
public <T> NotFoundException(Class<T> clazz, Long id) {
|
public <T> NotFoundException(Class<T> clazz, Long id) {
|
@ -18,6 +18,9 @@ public class Customer {
|
|||||||
private List<Game> games = new ArrayList<>();
|
private List<Game> games = new ArrayList<>();
|
||||||
|
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
|
@JoinTable(name="plays_customers",
|
||||||
|
joinColumns = @JoinColumn(name="customerId", referencedColumnName="id"),
|
||||||
|
inverseJoinColumns= @JoinColumn(name="playId", referencedColumnName="id") )
|
||||||
@OrderBy("date desc")
|
@OrderBy("date desc")
|
||||||
private List<Play> plays = new ArrayList<>();
|
private List<Play> plays = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ public class Game {
|
|||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JoinColumn(name = "сustomerId", updatable = false, insertable = false)
|
@JoinColumn(name = "customerId", updatable = false, insertable = false)
|
||||||
private Customer customer;
|
private Customer customer;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@ -75,6 +75,10 @@ public class Game {
|
|||||||
this.customerId = customer.getId();
|
this.customerId = customer.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long getCustomerId(){
|
||||||
|
return customerId;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<Play> getPlays(){
|
public Set<Play> getPlays(){
|
||||||
return plays;
|
return plays;
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,10 @@ public class Play {
|
|||||||
@JoinColumn(name = "gameId", updatable = false, insertable = false)
|
@JoinColumn(name = "gameId", updatable = false, insertable = false)
|
||||||
private Game game;
|
private Game game;
|
||||||
|
|
||||||
@ManyToMany()
|
@ManyToMany
|
||||||
|
@JoinTable(name="plays_customers",
|
||||||
|
joinColumns = @JoinColumn(name="playId", referencedColumnName="id"),
|
||||||
|
inverseJoinColumns= @JoinColumn(name="customerId", referencedColumnName="id") )
|
||||||
@OrderBy("name asc")
|
@OrderBy("name asc")
|
||||||
private List<Customer> customers = new ArrayList<>();
|
private List<Customer> customers = new ArrayList<>();
|
||||||
|
|
||||||
@ -79,6 +82,10 @@ public class Play {
|
|||||||
this.gameId = game.getId();
|
this.gameId = game.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getGameId(){
|
||||||
|
return gameId;
|
||||||
|
}
|
||||||
|
|
||||||
public List<Customer> getCustomers(){
|
public List<Customer> getCustomers(){
|
||||||
return customers;
|
return customers;
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
@ -1,56 +1,53 @@
|
|||||||
package com.example.demo.services;
|
package com.example.demo.services;
|
||||||
|
|
||||||
|
import com.example.demo.errors.NotFoundException;
|
||||||
import com.example.demo.models.Customer;
|
import com.example.demo.models.Customer;
|
||||||
import jakarta.persistence.EntityManager;
|
import com.example.demo.repositories.CustomerRepository;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import jakarta.persistence.PersistenceContext;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CustomerService {
|
public class CustomerService {
|
||||||
@PersistenceContext
|
private final CustomerRepository repository;
|
||||||
private EntityManager em;
|
|
||||||
|
public CustomerService(CustomerRepository repository){
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Customer addCustomer(String name) {
|
public Customer addCustomer(String name) {
|
||||||
if(!StringUtils.hasText(name)){
|
Customer customer = new Customer(name);
|
||||||
throw new IllegalArgumentException("Empty name");
|
return repository.save(customer);
|
||||||
}
|
|
||||||
final Customer customer = new Customer(name);
|
|
||||||
em.persist(customer);
|
|
||||||
return customer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Customer findCustomer(Long id){
|
public Customer findCustomer(Long id){
|
||||||
final Customer customer = em.find(Customer.class, id);
|
var customer = repository.findByIdWidthPlays(id);
|
||||||
if (customer == null){
|
return customer != null ?
|
||||||
throw new EntityNotFoundException(String.format("customer with id [%s] is not found", id));
|
repository.findByIdWidthGames(id).orElseThrow(() -> new NotFoundException(Customer.class, id)) :
|
||||||
}
|
customer.orElseThrow(() -> new NotFoundException(Customer.class, id));
|
||||||
return customer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Customer> getAllCustomers(){
|
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
|
@Transactional
|
||||||
public Customer updateCustomer(Long id, String name){
|
public Customer updateCustomer(Long id, String name){
|
||||||
final Customer currentCustomer = findCustomer(id);
|
Customer customer = findCustomer(id);
|
||||||
if(StringUtils.hasText(name))
|
customer.setName(name);
|
||||||
currentCustomer.setName(name);
|
|
||||||
return currentCustomer;
|
return repository.save(customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Customer deleteCustomer(Long id){
|
public Customer deleteCustomer(Long id){
|
||||||
final Customer currentCustomer = findCustomer(id);
|
Customer customer = findCustomer(id);
|
||||||
em.remove(currentCustomer);
|
repository.delete(customer);
|
||||||
return currentCustomer;
|
return customer;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,69 +1,58 @@
|
|||||||
package com.example.demo.services;
|
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.Customer;
|
||||||
|
import com.example.demo.models.Game;
|
||||||
|
import com.example.demo.repositories.GameRepository;
|
||||||
import jakarta.persistence.EntityManager;
|
import jakarta.persistence.EntityManager;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import jakarta.persistence.PersistenceContext;
|
import jakarta.persistence.PersistenceContext;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class GameService {
|
public class GameService {
|
||||||
@PersistenceContext
|
private final GameRepository repository;
|
||||||
private EntityManager em;
|
|
||||||
|
|
||||||
private final CustomerService customerService;
|
private final CustomerService customerService;
|
||||||
|
|
||||||
public GameService(CustomerService customerService){
|
public GameService(GameRepository repository, CustomerService customerService){
|
||||||
|
this.repository = repository;
|
||||||
this.customerService = customerService;
|
this.customerService = customerService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Game addGame(Long customerId, String name, String description){
|
public Game addGame(Long customerId, String name, String description){
|
||||||
if(!StringUtils.hasText(name)) {
|
Customer customer = customerService.findCustomer(customerId);
|
||||||
throw new IllegalArgumentException("Empty name");
|
Game game = new Game(name, description, customer);
|
||||||
}
|
return repository.save(game);
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Game findGame(Long id){
|
public Game findGame(Long id){
|
||||||
final Game game = em.find(Game.class, id);
|
return repository.findById(id).orElseThrow(
|
||||||
if(game == null){
|
() -> new NotFoundException(Game.class, id));
|
||||||
throw new EntityNotFoundException(String.format("Game with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return game;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Game> getAllGames(){
|
public List<Game> getAllGames(){
|
||||||
return em.createQuery("SELECT g FROM Game g", Game.class).getResultList();
|
return repository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Game updateGame(Long id, String name, String description){
|
public Game updateGame(Long id, String name, String description){
|
||||||
final Game currentGame = findGame(id);
|
Game game = findGame(id);
|
||||||
if(StringUtils.hasText(name))
|
game.setName(name);
|
||||||
currentGame.setName(name);
|
game.setDescription(description);
|
||||||
if(StringUtils.hasText(description))
|
|
||||||
currentGame.setDescription(description);
|
return repository.save(game);
|
||||||
return currentGame;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Game deleteGame(Long id){
|
public Game deleteGame(Long id){
|
||||||
final Game currentGame = findGame(id);
|
Game game = findGame(id);
|
||||||
em.remove(currentGame);
|
repository.delete(game);
|
||||||
return currentGame;
|
return game;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,81 +1,74 @@
|
|||||||
package com.example.demo.services;
|
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.Game;
|
||||||
import com.example.demo.models.Play;
|
import com.example.demo.models.Play;
|
||||||
import com.example.demo.models.Customer;
|
import com.example.demo.repositories.PlayRepository;
|
||||||
import jakarta.persistence.EntityManager;
|
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import jakarta.persistence.PersistenceContext;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PlayService {
|
public class PlayService {
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager em;
|
|
||||||
|
|
||||||
|
private final PlayRepository repository;
|
||||||
private final GameService gameService;
|
private final GameService gameService;
|
||||||
private final CustomerService customerService;
|
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.gameService = gameService;
|
||||||
this.customerService = customerService;
|
this.customerService = customerService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Play addPlay(Long gameId, Date date, String description){
|
public Play addPlay(Long gameId, Date date, String description){
|
||||||
final Game game = gameService.findGame(gameId);
|
Game game = gameService.findGame(gameId);
|
||||||
|
|
||||||
if (game == null){
|
Play play = new Play(game, date, description);
|
||||||
throw new IllegalArgumentException("gameId invalid");
|
return repository.save(play);
|
||||||
}
|
|
||||||
|
|
||||||
final Play Play = new Play(game, date, description);
|
|
||||||
em.persist(Play);
|
|
||||||
return Play;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Play findPlay(Long id){
|
public Play findPlay(Long id){
|
||||||
final Play Play = em.find(Play.class, id);
|
return repository.findById(id).orElseThrow(
|
||||||
if (Play == null){
|
() -> new NotFoundException(Play.class, id));
|
||||||
throw new EntityNotFoundException(String.format("Play with id [%s] is not found", id));
|
|
||||||
}
|
|
||||||
return Play;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public List<Play> getAllPlays(){
|
public List<Play> getAllPlays(){
|
||||||
return em.createQuery("SELECT p FROM Play p", Play.class).getResultList();
|
return repository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Play updatePlay(Long id, Date date, String description){
|
public Play updatePlay(Long id, Date date, String description){
|
||||||
final Play currentPlay = findPlay(id);
|
Play play = findPlay(id);
|
||||||
|
|
||||||
if(StringUtils.hasText(description))
|
play.setDate(date);
|
||||||
currentPlay.setDescription(description);
|
play.setDescription(description);
|
||||||
if(date != null)
|
|
||||||
currentPlay.setDate(date);
|
return repository.save(play);
|
||||||
return currentPlay;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public Play deletePlay(Long id){
|
public Play deletePlay(Long id){
|
||||||
final Play currentPlay = findPlay(id);
|
Play play = findPlay(id);
|
||||||
em.remove(currentPlay);
|
repository.delete(play);
|
||||||
return currentPlay;
|
return play;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public boolean addCustomer(Long id, Long customerId){
|
public boolean addCustomer(Long id, Long customerId){
|
||||||
final Play currentPlay = findPlay(id);
|
final Play play = findPlay(id);
|
||||||
final Customer customer = customerService.findCustomer(customerId);
|
final Customer customer = customerService.findCustomer(customerId);
|
||||||
|
|
||||||
return customer.addPlay(currentPlay);
|
if (customer.addPlay(play)){
|
||||||
|
repository.save(play);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,3 +9,6 @@ spring.jpa.hibernate.ddl-auto=update
|
|||||||
spring.h2.console.enabled=true
|
spring.h2.console.enabled=true
|
||||||
spring.h2.console.settings.trace=false
|
spring.h2.console.settings.trace=false
|
||||||
spring.h2.console.settings.web-allow-others=false
|
spring.h2.console.settings.web-allow-others=false
|
||||||
|
|
||||||
|
spring.jpa.show-sql=true
|
||||||
|
spring.jpa.properties.hibernate.format_sql=true
|
||||||
|
Loading…
Reference in New Issue
Block a user