added card

This commit is contained in:
helori_ollivier
2025-12-04 19:47:59 +01:00
parent 524110216f
commit 783997098b
6 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
package bzh.sudchat.uno.controller;
import bzh.sudchat.uno.model.Card;
import bzh.sudchat.uno.repository.CardRepository;
import bzh.sudchat.uno.service.CardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("cards")
public class CardController {
CardService cardService;
@Autowired
public CardController(CardService cardService) {
this.cardService = cardService;
}
@GetMapping("")
public Iterable<Card> getCards() {
return cardService.getCards();
}
@GetMapping("/{id}")
public Card getCards(@PathVariable int id) {
return cardService.getCardByID(id);
}
}

View File

@@ -0,0 +1,12 @@
package bzh.sudchat.uno.exceptions;
public class CardNotFoundException extends RuntimeException {
public CardNotFoundException(String message) {
super(message);
}
public CardNotFoundException(int id) {
super("Card with id " + id + " not found");
}
}

View File

@@ -0,0 +1,14 @@
package bzh.sudchat.uno.model;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Data;
@Entity
@Data
public class Card {
@Id
private int id;
}

View File

@@ -0,0 +1,16 @@
package bzh.sudchat.uno.model;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.Data;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
@Data
public class Deck {
Queue<Card> cards = new LinkedList<>();
}

View File

@@ -0,0 +1,8 @@
package bzh.sudchat.uno.repository;
import bzh.sudchat.uno.model.Card;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
public interface CardRepository extends CrudRepository<Card, Integer> {
}

View File

@@ -0,0 +1,37 @@
package bzh.sudchat.uno.service;
import bzh.sudchat.uno.exceptions.CardNotFoundException;
import bzh.sudchat.uno.model.Card;
import bzh.sudchat.uno.repository.CardRepository;
import jakarta.persistence.Entity;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Data
@Service
public class CardService {
private final CardRepository cardRepository;
@Autowired
public CardService(CardRepository cardRepository) {
this.cardRepository = cardRepository;
}
public Iterable<Card> getCards() {
return cardRepository.findAll();
}
public Card getCardByID(int id) {
Optional<Card> card = cardRepository.findById(id);
if (card.isEmpty()) {
throw new CardNotFoundException(id);
}
return card.get();
}
}