оно даже работает
This commit is contained in:
parent
bcb8bfa8db
commit
4f8b943108
@ -1,7 +1,9 @@
|
||||
package com.example.demo.controllers;
|
||||
|
||||
import com.example.demo.core.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.*;
|
||||
|
||||
@ -41,7 +43,12 @@ public class PlayController {
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/customers")
|
||||
public boolean addCustomer(@PathVariable(name = "id") long id, long customerId){
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import com.example.demo.models.Game;
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GameDto {
|
||||
@ -23,6 +24,7 @@ public class GameDto {
|
||||
}
|
||||
|
||||
public GameDto(Game game){
|
||||
id = game.getId();
|
||||
name = game.getName();
|
||||
description = game.getDescription();
|
||||
|
||||
@ -30,11 +32,17 @@ public class GameDto {
|
||||
if (customerExist != null){
|
||||
customer = new CustomerDto(customerExist);
|
||||
}
|
||||
else {
|
||||
customer = null;
|
||||
}
|
||||
|
||||
var playsExist = game.getPlays();
|
||||
if (playsExist != null && !playsExist.isEmpty()){
|
||||
plays = playsExist.stream().map(PlayDto::new).toList();
|
||||
}
|
||||
else {
|
||||
plays = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
@ -3,6 +3,7 @@ package com.example.demo.dtos;
|
||||
import com.example.demo.models.Play;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@ -24,6 +25,7 @@ public class PlayDto {
|
||||
}
|
||||
|
||||
public PlayDto(Play play){
|
||||
id = play.getId();
|
||||
description = play.getDescription();
|
||||
date = play.getDate();
|
||||
|
||||
@ -34,6 +36,14 @@ public class PlayDto {
|
||||
else {
|
||||
game = null;
|
||||
}
|
||||
|
||||
var existCustomers = play.getCustomers();
|
||||
if (existCustomers != null && !existCustomers.isEmpty()){
|
||||
customers = existCustomers.stream().map(CustomerDto::new).toList();
|
||||
}
|
||||
else {
|
||||
customers = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public String getDescription(){
|
||||
|
@ -75,10 +75,6 @@ public class Game {
|
||||
this.customerId = customer.getId();
|
||||
}
|
||||
|
||||
public long getCustomerId() {
|
||||
return customerId;
|
||||
}
|
||||
|
||||
public Set<Play> getPlays(){
|
||||
return plays;
|
||||
}
|
||||
|
@ -85,4 +85,7 @@ public class Play {
|
||||
public void setCustomers(List<Customer> customers){
|
||||
this.customers = customers;
|
||||
}
|
||||
public boolean addCustomer(Customer customer){
|
||||
return customers.add(customer);
|
||||
}
|
||||
}
|
||||
|
@ -31,18 +31,18 @@ public class GameService {
|
||||
if (customer == null){
|
||||
throw new IllegalArgumentException("customerId invalid");
|
||||
}
|
||||
final Game Game = new Game(name, description, customer);
|
||||
em.persist(Game);
|
||||
return Game;
|
||||
final Game game = new Game(name, description, customer);
|
||||
em.persist(game);
|
||||
return game;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Game findGame(Long id){
|
||||
final Game Game = em.find(Game.class, id);
|
||||
if(Game==null){
|
||||
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 game;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
Loading…
Reference in New Issue
Block a user