From 0d6c771c26f2d34f46bd471bfb93543467294874 Mon Sep 17 00:00:00 2001 From: visoulier Date: Wed, 27 Mar 2024 20:57:28 +0100 Subject: [PATCH] get by codeInvitation or ID --- .../sae/controllers/PartieController.java | 19 ++++++++++++++++--- .../notFound/EntityNotFoundException.java | 4 ++++ .../notFound/PartieNotFoundException.java | 7 ++++++- .../sae/services/PartieService.java | 4 ++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieController.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieController.java index f075efe..ddeadaa 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieController.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/PartieController.java @@ -5,6 +5,7 @@ import fr.iut.sciencequest.sae.controllers.request.PartieRequest; import fr.iut.sciencequest.sae.dto.partie.PartieDTO; import fr.iut.sciencequest.sae.entities.Joueur; import fr.iut.sciencequest.sae.entities.Partie; +import fr.iut.sciencequest.sae.exceptions.notFound.PartieNotFoundException; import fr.iut.sciencequest.sae.services.JeuService; import fr.iut.sciencequest.sae.services.JoueurService; import fr.iut.sciencequest.sae.services.PartieService; @@ -19,6 +20,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import static org.springframework.data.jpa.domain.AbstractPersistable_.id; + @RestController @AllArgsConstructor @RequestMapping("/api/v1/partie") @@ -29,9 +32,19 @@ public class PartieController { private final JeuService jeuService; private final ModelMapper modelMapper; - @RequestMapping(value = "/{id}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) - public PartieDTO getPartie(@PathVariable int id) { - Partie partie = this.partieService.findById(id); + @RequestMapping(value = "/{codeInvitation}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public PartieDTO getPartie(@PathVariable String codeInvitation) { + //try to get invitation from codeInvitation, if not : try with id + Partie partie; + try{ + partie = this.partieService.getPartieByCodeInvitation(codeInvitation); + }catch (PartieNotFoundException exceptionNotFoundByCodeInvitation){ + try{ + partie = this.partieService.findById(Integer.parseInt(codeInvitation)); + }catch (PartieNotFoundException | NumberFormatException e2){ + throw exceptionNotFoundByCodeInvitation; + } + } return partieModelAssembler.toModel(partie); } diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/EntityNotFoundException.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/EntityNotFoundException.java index 25e37c9..c07e1cf 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/EntityNotFoundException.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/EntityNotFoundException.java @@ -12,4 +12,8 @@ public abstract class EntityNotFoundException extends RuntimeException { public EntityNotFoundException(String entityName, Object id){ super(entityName + " not found with id : " + id); } + + public EntityNotFoundException(String entityName, String fieldName, Object value){ + super(entityName + " not found with " + fieldName + " : " + value); + } } diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/PartieNotFoundException.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/PartieNotFoundException.java index 43f00be..c4fc3f3 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/PartieNotFoundException.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/exceptions/notFound/PartieNotFoundException.java @@ -6,7 +6,12 @@ import org.springframework.web.bind.annotation.ResponseStatus; @ResponseStatus(HttpStatus.NOT_FOUND) public class PartieNotFoundException extends EntityNotFoundException{ + private static String entityName = "partie"; public PartieNotFoundException(int id) { - super("partie", id); + super(PartieNotFoundException.entityName, id); + } + + public PartieNotFoundException(String field, Object value){ + super(PartieNotFoundException.entityName, field, value); } } diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieService.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieService.java index f3501b9..c758c9b 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieService.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/PartieService.java @@ -36,6 +36,10 @@ public class PartieService { ); } + public Partie getPartieByCodeInvitation(String codeInvitation){ + return this.partieRepository.getPartieByCodeInvitation(codeInvitation).orElseThrow(() -> new PartieNotFoundException("codeInvitation", codeInvitation)); + } + public Partie update(Partie partie){ if(!this.partieRepository.existsById(partie.getId())){ throw new PartieNotFoundException(partie.getId());