|
|
@ -1,11 +1,13 @@
|
|
|
|
package fr.iut.sciencequest.sae.controllers;
|
|
|
|
package fr.iut.sciencequest.sae.controllers;
|
|
|
|
|
|
|
|
|
|
|
|
import fr.iut.sciencequest.sae.assemblers.PartieModelAssembler;
|
|
|
|
import fr.iut.sciencequest.sae.assemblers.PartieModelAssembler;
|
|
|
|
|
|
|
|
import fr.iut.sciencequest.sae.controllers.request.PartieAddJoueurRequest;
|
|
|
|
import fr.iut.sciencequest.sae.controllers.request.PartieRequest;
|
|
|
|
import fr.iut.sciencequest.sae.controllers.request.PartieRequest;
|
|
|
|
import fr.iut.sciencequest.sae.dto.partie.PartieDTO;
|
|
|
|
import fr.iut.sciencequest.sae.dto.partie.PartieDTO;
|
|
|
|
import fr.iut.sciencequest.sae.entities.Joueur;
|
|
|
|
import fr.iut.sciencequest.sae.entities.Joueur;
|
|
|
|
import fr.iut.sciencequest.sae.entities.Partie;
|
|
|
|
import fr.iut.sciencequest.sae.entities.Partie;
|
|
|
|
import fr.iut.sciencequest.sae.exceptions.notFound.PartieNotFoundException;
|
|
|
|
import fr.iut.sciencequest.sae.exceptions.notFound.PartieNotFoundException;
|
|
|
|
|
|
|
|
import fr.iut.sciencequest.sae.exceptions.partie.PartyAlreadyStartedException;
|
|
|
|
import fr.iut.sciencequest.sae.services.JeuService;
|
|
|
|
import fr.iut.sciencequest.sae.services.JeuService;
|
|
|
|
import fr.iut.sciencequest.sae.services.JoueurService;
|
|
|
|
import fr.iut.sciencequest.sae.services.JoueurService;
|
|
|
|
import fr.iut.sciencequest.sae.services.PartieService;
|
|
|
|
import fr.iut.sciencequest.sae.services.PartieService;
|
|
|
@ -34,18 +36,7 @@ public class PartieController {
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping(value = "/{codeInvitation}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
@RequestMapping(value = "/{codeInvitation}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
public PartieDTO getPartie(@PathVariable String codeInvitation) {
|
|
|
|
public PartieDTO getPartie(@PathVariable String codeInvitation) {
|
|
|
|
//try to get invitation from codeInvitation, if not : try with id
|
|
|
|
return partieModelAssembler.toModel(this.partieService.getPartieByIdOrCodeInvitation(codeInvitation));
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
@ -58,4 +49,16 @@ public class PartieController {
|
|
|
|
return this.modelMapper.map(partie, PartieDTO.class);
|
|
|
|
return this.modelMapper.map(partie, PartieDTO.class);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PutMapping(value= "/{codeInvitation}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
|
|
|
|
|
@ResponseStatus(HttpStatus.OK)
|
|
|
|
|
|
|
|
public PartieDTO addPlayerToPartie(@PathVariable String codeInvitation, @RequestBody @Valid PartieAddJoueurRequest request){
|
|
|
|
|
|
|
|
Joueur joueur = this.joueurService.findById(request.getIdJoueur());
|
|
|
|
|
|
|
|
Partie partie = this.partieService.getPartieByIdOrCodeInvitation(codeInvitation);
|
|
|
|
|
|
|
|
if(!partie.getStatus().equals("pending")) throw new PartyAlreadyStartedException();
|
|
|
|
|
|
|
|
if(!partie.getJoueurs().contains(joueur)){
|
|
|
|
|
|
|
|
partie.getJoueurs().add(joueur);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.modelMapper.map(this.partieService.update(partie), PartieDTO.class);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|