From 987097f7a157de0acf9259be102bde94ad399cc4 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 30 Oct 2021 14:54:13 +0200 Subject: [PATCH] Some api cleaning. Reset deck. Exceptions. --- postman/Cards.postman_collection.json | 86 +++++++++++++++++++ .../cartes/rest/GameResource.java | 15 +++- .../cartes/service/GameService.java | 16 ++-- 3 files changed, 109 insertions(+), 8 deletions(-) create mode 100644 postman/Cards.postman_collection.json diff --git a/postman/Cards.postman_collection.json b/postman/Cards.postman_collection.json new file mode 100644 index 0000000..35a01e5 --- /dev/null +++ b/postman/Cards.postman_collection.json @@ -0,0 +1,86 @@ +{ + "info": { + "_postman_id": "f6e0a899-d4c7-49a4-9b03-034938dacde1", + "name": "Cards", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "Get Hand", + "request": { + "method": "GET", + "header": [], + "url": { + "raw": "localhost:8080/api/deck/hand", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "api", + "deck", + "hand" + ] + } + }, + "response": [] + }, + { + "name": "Sort hand", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "[\n {\n \"shape\": \"CLUBS\",\n \"number\": \"SIX\"\n },\n {\n \"shape\": \"SPADES\",\n \"number\": \"SEVEN\"\n },\n {\n \"shape\": \"HEARTS\",\n \"number\": \"SIX\"\n },\n {\n \"shape\": \"CLUBS\",\n \"number\": \"JACK\"\n },\n {\n \"shape\": \"SPADES\",\n \"number\": \"FOUR\"\n },\n {\n \"shape\": \"DIMONDS\",\n \"number\": \"SIX\"\n },\n {\n \"shape\": \"DIMONDS\",\n \"number\": \"FIVE\"\n },\n {\n \"shape\": \"DIMONDS\",\n \"number\": \"THREE\"\n },\n {\n \"shape\": \"HEARTS\",\n \"number\": \"TWO\"\n },\n {\n \"shape\": \"SPADES\",\n \"number\": \"TWO\"\n }\n]", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:8080/api/deck/hand:sort", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "api", + "deck", + "hand:sort" + ] + } + }, + "response": [] + }, + { + "name": "Reset game", + "request": { + "method": "PUT", + "header": [], + "body": { + "mode": "raw", + "raw": "[\n {\n \"shape\": \"CLUBS\",\n \"number\": \"SIX\"\n },\n {\n \"shape\": \"SPADES\",\n \"number\": \"SEVEN\"\n },\n {\n \"shape\": \"HEARTS\",\n \"number\": \"SIX\"\n },\n {\n \"shape\": \"CLUBS\",\n \"number\": \"JACK\"\n },\n {\n \"shape\": \"SPADES\",\n \"number\": \"FOUR\"\n },\n {\n \"shape\": \"DIMONDS\",\n \"number\": \"SIX\"\n },\n {\n \"shape\": \"DIMONDS\",\n \"number\": \"FIXE\"\n },\n {\n \"shape\": \"DIMONDS\",\n \"number\": \"THREE\"\n },\n {\n \"shape\": \"HEARTS\",\n \"number\": \"TWO\"\n },\n {\n \"shape\": \"SPADES\",\n \"number\": \"TWO\"\n }\n]", + "options": { + "raw": { + "language": "json" + } + } + }, + "url": { + "raw": "localhost:8080/api/deck:reset", + "host": [ + "localhost" + ], + "port": "8080", + "path": [ + "api", + "deck:reset" + ] + } + }, + "response": [] + } + ] +} \ No newline at end of file diff --git a/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java b/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java index b034a07..9bd2f77 100644 --- a/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java +++ b/src/main/java/fr/aminelouveaau/cartes/rest/GameResource.java @@ -3,12 +3,14 @@ package fr.aminelouveaau.cartes.rest; import fr.aminelouveaau.cartes.model.Card; import fr.aminelouveaau.cartes.model.api.SortingResult; import fr.aminelouveaau.cartes.service.GameService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping({"/api/game", "/api/game/v0"}) +@RequestMapping({"/api", "/api/v0"}) public class GameResource { private final GameService gameService; @@ -17,13 +19,20 @@ public class GameResource { this.gameService = gameService; } - @RequestMapping(value = "/card-hand", method = RequestMethod.GET) + @RequestMapping(value = "deck:reset", method = RequestMethod.PUT) + @ResponseBody + public ResponseEntity resetGame() { + gameService.createDeck(); + return new ResponseEntity<>(HttpStatus.OK); + } + + @RequestMapping(value = "/deck/hand", method = RequestMethod.GET) @ResponseBody public List getCardHand() { return gameService.cardHand(); } - @RequestMapping(value = "/card-hand:sort", method = RequestMethod.PUT) + @RequestMapping(value = "/deck/hand:sort", method = RequestMethod.PUT) @ResponseBody public SortingResult sortHand(@RequestBody final List hand) { return gameService.sortHand(hand); diff --git a/src/main/java/fr/aminelouveaau/cartes/service/GameService.java b/src/main/java/fr/aminelouveaau/cartes/service/GameService.java index 4f218e5..4bc5d87 100644 --- a/src/main/java/fr/aminelouveaau/cartes/service/GameService.java +++ b/src/main/java/fr/aminelouveaau/cartes/service/GameService.java @@ -5,7 +5,9 @@ import fr.aminelouveaau.cartes.model.Number; import fr.aminelouveaau.cartes.model.Shape; import fr.aminelouveaau.cartes.model.api.SortingResult; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; +import org.springframework.web.client.HttpClientErrorException; import javax.annotation.PostConstruct; import java.util.*; @@ -40,7 +42,7 @@ public class GameService { return deck; } - private void createDeck() { + public void createDeck() { Arrays.stream(Shape.values()).forEach(shape -> { Arrays.stream(Number.values()).forEach(number -> { deck.push(new Card(shape, number)); @@ -60,11 +62,15 @@ public class GameService { } public List cardHand() { - List hand = new ArrayList<>(); - for (int i = 0; i < this.cardHandNumber; i++) { - hand.add(this.deck.pop()); + if (this.deck.size() >= this.cardHandNumber) { + List hand = new ArrayList<>(); + for (int i = 0; i < this.cardHandNumber; i++) { + hand.add(this.deck.pop()); + } + return hand; + } else { + throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS); } - return hand; } public SortingResult sortHand(List hand) {