From 06b493b561580e6cdc596e04ab6c0c04f34df65c Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Mon, 25 Mar 2024 18:37:52 +0100 Subject: [PATCH] Ajout controller Jeu (routes GET /api/v1/jeux & /api/v1/jeux/{id}) --- .../sae/controllers/JeuController.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/JeuController.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/JeuController.java index 201caef..a171f62 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/JeuController.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/JeuController.java @@ -1,2 +1,39 @@ -package fr.iut.sciencequest.sae.controllers;public class JeuController { +package fr.iut.sciencequest.sae.controllers; + +import fr.iut.sciencequest.sae.dto.jeu.JeuDTO; +import fr.iut.sciencequest.sae.entities.Jeu; +import fr.iut.sciencequest.sae.exceptions.notFound.JeuNotFoundException; +import fr.iut.sciencequest.sae.repositories.JeuRepository; +import lombok.AllArgsConstructor; +import org.modelmapper.ModelMapper; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.List; + +import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo; + +@RestController +@AllArgsConstructor +@RequestMapping("/api/v1/jeux") +public class JeuController { + private final JeuRepository jeuRepository; + private final ModelMapper modelMapper; + + @RequestMapping(value = "/{id}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public Jeu getJeu(@PathVariable int id) { + return this.jeuRepository.findById(id).orElseThrow(() -> new JeuNotFoundException(id)); + } + + @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) + public Iterable getJeux() { + List jeuDTOList = new ArrayList<>(); + for(Jeu jeu : this.jeuRepository.findAll()) { + jeuDTOList.add(this.modelMapper.map(jeu, JeuDTO.class).add( + linkTo(JeuController.class).slash(jeu.getId()).withRel("self") + )); + } + return jeuDTOList; + } }