Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
783997098b |
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
14
api-uno/src/main/java/bzh/sudchat/uno/model/Card.java
Normal file
14
api-uno/src/main/java/bzh/sudchat/uno/model/Card.java
Normal 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;
|
||||
|
||||
}
|
||||
16
api-uno/src/main/java/bzh/sudchat/uno/model/Deck.java
Normal file
16
api-uno/src/main/java/bzh/sudchat/uno/model/Deck.java
Normal 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<>();
|
||||
|
||||
}
|
||||
@@ -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> {
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user