get by codeInvitation or ID

Springboot
Victor SOULIER 1 year ago
parent cbebbe2cf2
commit 0d6c771c26

@ -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);
}

@ -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);
}
}

@ -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);
}
}

@ -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());

Loading…
Cancel
Save