оно даже работает

This commit is contained in:
bekodeg 2024-06-06 20:33:04 +04:00
parent bcb8bfa8db
commit 4f8b943108
6 changed files with 35 additions and 11 deletions

View File

@ -1,7 +1,9 @@
package com.example.demo.controllers; package com.example.demo.controllers;
import com.example.demo.core.config.Constants; import com.example.demo.core.config.Constants;
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.*;
@ -41,7 +43,12 @@ public class PlayController {
} }
@PostMapping("/{id}/customers") @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); 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

@ -4,6 +4,7 @@ import com.example.demo.models.Game;
import jakarta.validation.constraints.Min; import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class GameDto { public class GameDto {
@ -23,6 +24,7 @@ public class GameDto {
} }
public GameDto(Game game){ public GameDto(Game game){
id = game.getId();
name = game.getName(); name = game.getName();
description = game.getDescription(); description = game.getDescription();
@ -30,11 +32,17 @@ public class GameDto {
if (customerExist != null){ if (customerExist != null){
customer = new CustomerDto(customerExist); 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(); plays = playsExist.stream().map(PlayDto::new).toList();
} }
else {
plays = new ArrayList<>();
}
} }
public String getName() { public String getName() {

View File

@ -3,6 +3,7 @@ package com.example.demo.dtos;
import com.example.demo.models.Play; import com.example.demo.models.Play;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -24,6 +25,7 @@ public class PlayDto {
} }
public PlayDto(Play play){ public PlayDto(Play play){
id = play.getId();
description = play.getDescription(); description = play.getDescription();
date = play.getDate(); date = play.getDate();
@ -34,6 +36,14 @@ public class PlayDto {
else { else {
game = null; 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(){ public String getDescription(){

View File

@ -75,10 +75,6 @@ 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;
} }

View File

@ -85,4 +85,7 @@ public class Play {
public void setCustomers(List<Customer> customers){ public void setCustomers(List<Customer> customers){
this.customers = customers; this.customers = customers;
} }
public boolean addCustomer(Customer customer){
return customers.add(customer);
}
} }

View File

@ -31,18 +31,18 @@ public class GameService {
if (customer == null){ if (customer == null){
throw new IllegalArgumentException("customerId invalid"); throw new IllegalArgumentException("customerId invalid");
} }
final Game Game = new Game(name, description, customer); final Game game = new Game(name, description, customer);
em.persist(Game); em.persist(game);
return Game; return game;
} }
@Transactional @Transactional
public Game findGame(Long id){ public Game findGame(Long id){
final Game Game = em.find(Game.class, id); final Game game = em.find(Game.class, id);
if(Game==null){ if(game == null){
throw new EntityNotFoundException(String.format("Game with id [%s] is not found", id)); throw new EntityNotFoundException(String.format("Game with id [%s] is not found", id));
} }
return Game; return game;
} }
@Transactional @Transactional