Some api cleaning. Reset deck. Exceptions.

This commit is contained in:
aminecmi 2021-10-30 14:54:13 +02:00
parent 01f1b6170c
commit 987097f7a1
3 changed files with 109 additions and 8 deletions

View File

@ -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": []
}
]
}

View File

@ -3,12 +3,14 @@ package fr.aminelouveaau.cartes.rest;
import fr.aminelouveaau.cartes.model.Card; import fr.aminelouveaau.cartes.model.Card;
import fr.aminelouveaau.cartes.model.api.SortingResult; import fr.aminelouveaau.cartes.model.api.SortingResult;
import fr.aminelouveaau.cartes.service.GameService; import fr.aminelouveaau.cartes.service.GameService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping({"/api/game", "/api/game/v0"}) @RequestMapping({"/api", "/api/v0"})
public class GameResource { public class GameResource {
private final GameService gameService; private final GameService gameService;
@ -17,13 +19,20 @@ public class GameResource {
this.gameService = gameService; 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 @ResponseBody
public List<Card> getCardHand() { public List<Card> getCardHand() {
return gameService.cardHand(); return gameService.cardHand();
} }
@RequestMapping(value = "/card-hand:sort", method = RequestMethod.PUT) @RequestMapping(value = "/deck/hand:sort", method = RequestMethod.PUT)
@ResponseBody @ResponseBody
public SortingResult sortHand(@RequestBody final List<Card> hand) { public SortingResult sortHand(@RequestBody final List<Card> hand) {
return gameService.sortHand(hand); return gameService.sortHand(hand);

View File

@ -5,7 +5,9 @@ import fr.aminelouveaau.cartes.model.Number;
import fr.aminelouveaau.cartes.model.Shape; import fr.aminelouveaau.cartes.model.Shape;
import fr.aminelouveaau.cartes.model.api.SortingResult; import fr.aminelouveaau.cartes.model.api.SortingResult;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.*; import java.util.*;
@ -40,7 +42,7 @@ public class GameService {
return deck; return deck;
} }
private void createDeck() { public void createDeck() {
Arrays.stream(Shape.values()).forEach(shape -> { Arrays.stream(Shape.values()).forEach(shape -> {
Arrays.stream(Number.values()).forEach(number -> { Arrays.stream(Number.values()).forEach(number -> {
deck.push(new Card(shape, number)); deck.push(new Card(shape, number));
@ -60,11 +62,15 @@ public class GameService {
} }
public List<Card> cardHand() { public List<Card> cardHand() {
List<Card> hand = new ArrayList<>(); if (this.deck.size() >= this.cardHandNumber) {
for (int i = 0; i < this.cardHandNumber; i++) { List<Card> hand = new ArrayList<>();
hand.add(this.deck.pop()); 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<Card> hand) { public SortingResult sortHand(List<Card> hand) {