diff --git a/demo/src/main/java/com/example/demo/controllers/PlayController.java b/demo/src/main/java/com/example/demo/controllers/PlayController.java index 4a4f68c..8dfa64d 100644 --- a/demo/src/main/java/com/example/demo/controllers/PlayController.java +++ b/demo/src/main/java/com/example/demo/controllers/PlayController.java @@ -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 getCustomers(@PathVariable(name = "id") long id){ + return playService.findPlay(id).getCustomers().stream().map(CustomerDto::new).toList(); + } } diff --git a/demo/src/main/java/com/example/demo/dtos/GameDto.java b/demo/src/main/java/com/example/demo/dtos/GameDto.java index 480b849..365d65b 100644 --- a/demo/src/main/java/com/example/demo/dtos/GameDto.java +++ b/demo/src/main/java/com/example/demo/dtos/GameDto.java @@ -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() { diff --git a/demo/src/main/java/com/example/demo/dtos/PlayDto.java b/demo/src/main/java/com/example/demo/dtos/PlayDto.java index 959f5db..07b97d3 100644 --- a/demo/src/main/java/com/example/demo/dtos/PlayDto.java +++ b/demo/src/main/java/com/example/demo/dtos/PlayDto.java @@ -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(){ diff --git a/demo/src/main/java/com/example/demo/models/Game.java b/demo/src/main/java/com/example/demo/models/Game.java index 328afff..7492e29 100644 --- a/demo/src/main/java/com/example/demo/models/Game.java +++ b/demo/src/main/java/com/example/demo/models/Game.java @@ -75,10 +75,6 @@ public class Game { this.customerId = customer.getId(); } - public long getCustomerId() { - return customerId; - } - public Set getPlays(){ return plays; } diff --git a/demo/src/main/java/com/example/demo/models/Play.java b/demo/src/main/java/com/example/demo/models/Play.java index e89e3e5..fc1df56 100644 --- a/demo/src/main/java/com/example/demo/models/Play.java +++ b/demo/src/main/java/com/example/demo/models/Play.java @@ -85,4 +85,7 @@ public class Play { public void setCustomers(List customers){ this.customers = customers; } + public boolean addCustomer(Customer customer){ + return customers.add(customer); + } } diff --git a/demo/src/main/java/com/example/demo/services/GameService.java b/demo/src/main/java/com/example/demo/services/GameService.java index 4cfc9c6..67dd2b1 100644 --- a/demo/src/main/java/com/example/demo/services/GameService.java +++ b/demo/src/main/java/com/example/demo/services/GameService.java @@ -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