| 1 | package fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.controllers; | |
| 2 | ||
| 3 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.dtos.ParticipantDto; | |
| 4 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.exception.NoUpdateException; | |
| 5 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.models.Participant; | |
| 6 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.services.ParticipantService; | |
| 7 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.utils.StringUtils; | |
| 8 | import io.swagger.v3.oas.annotations.Operation; | |
| 9 | import jakarta.persistence.NoResultException; | |
| 10 | import org.modelmapper.ModelMapper; | |
| 11 | import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | import org.springframework.http.HttpStatus; | |
| 13 | import org.springframework.web.bind.annotation.*; | |
| 14 | import org.springframework.web.server.ResponseStatusException; | |
| 15 | ||
| 16 | import java.util.List; | |
| 17 | import java.util.NoSuchElementException; | |
| 18 | ||
| 19 | @RestController | |
| 20 | @RequestMapping(value = "/api/participant") | |
| 21 | public class ParticipantController { | |
| 22 | ||
| 23 | @Autowired | |
| 24 | private ParticipantService service; | |
| 25 | ||
| 26 | @Autowired | |
| 27 | private ModelMapper mapper; | |
| 28 | ||
| 29 | /** | |
| 30 | * Create a participant | |
| 31 | * @param participantDto du participant à créer | |
| 32 | * @return participant créé | |
| 33 | */ | |
| 34 | @PostMapping(value = "/") | |
| 35 | @ResponseStatus(HttpStatus.CREATED) | |
| 36 | @Operation(summary = "Créer un participant", description = "Retourne le participant créé.") | |
| 37 | public ParticipantDto create(@RequestBody ParticipantDto participantDto) { | |
| 38 |
2
1. create : negated conditional → KILLED 2. create : negated conditional → KILLED |
if(StringUtils.isNullOrEmpty(participantDto.getNom()) || StringUtils.isNullOrEmpty(participantDto.getPrenom())) |
| 39 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Précisez un nom et un prénom."); | |
| 40 | try { | |
| 41 | var model = mapper.map(participantDto, Participant.class); | |
| 42 | var result = service.create(model); | |
| 43 |
1
1. create : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/ParticipantController::create → KILLED |
return mapper.map(result, ParticipantDto.class); |
| 44 | } | |
| 45 | catch (Exception e) { | |
| 46 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la création du participant."); | |
| 47 | } | |
| 48 | } | |
| 49 | ||
| 50 | /** | |
| 51 | * Get all participants | |
| 52 | * @return liste de tous les participants existants | |
| 53 | */ | |
| 54 | @GetMapping(value = "/") | |
| 55 | @ResponseStatus(HttpStatus.OK) | |
| 56 | @Operation(summary = "Récupérer tous les participants", description = "Retourne tous les participants existants.") | |
| 57 | public List<ParticipantDto> getAllParticipants() { | |
| 58 | try { | |
| 59 | var models = service.getAll(); | |
| 60 |
2
1. lambda$getAllParticipants$0 : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/ParticipantController::lambda$getAllParticipants$0 → KILLED 2. getAllParticipants : replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/ParticipantController::getAllParticipants → KILLED |
return models.stream().map(model -> mapper.map(model, ParticipantDto.class)).toList(); |
| 61 | } | |
| 62 | catch(NoResultException e) { | |
| 63 | throw new ResponseStatusException(HttpStatus.NO_CONTENT, e.getMessage()); | |
| 64 | } | |
| 65 | catch (Exception e) { | |
| 66 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération des participants."); | |
| 67 | } | |
| 68 | } | |
| 69 | /** | |
| 70 | * Get participant by id | |
| 71 | * @param id du participant à récupérer | |
| 72 | * @return participant correspondant à l'id | |
| 73 | */ | |
| 74 | @GetMapping(value = "/{id}") | |
| 75 | @ResponseStatus(HttpStatus.OK) | |
| 76 | @Operation(summary = "Récupérer un participant par son id", description = "Retourne le participant s'il existe.") | |
| 77 | public ParticipantDto getParticipantById(@PathVariable("id") Long id) { | |
| 78 | try{ | |
| 79 | var model = service.getById(id); | |
| 80 |
1
1. getParticipantById : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/ParticipantController::getParticipantById → KILLED |
return mapper.map(model, ParticipantDto.class); |
| 81 | } | |
| 82 | catch(NoResultException e) { | |
| 83 | throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | |
| 84 | } | |
| 85 | catch (Exception e) { | |
| 86 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération du participant."); | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | /** | |
| 91 | * Update a participant | |
| 92 | * @param id du participant à modifier | |
| 93 | * @param participantDto du participant à modifier | |
| 94 | * @return participant modifié | |
| 95 | */ | |
| 96 | @PutMapping(value = "/{id}") | |
| 97 | @ResponseStatus(HttpStatus.OK) | |
| 98 | @Operation(summary = "Modifier un participant", description = "Retourne le participant modifié.") | |
| 99 | public ParticipantDto update(@PathVariable("id") Long id, @RequestBody ParticipantDto participantDto) { | |
| 100 |
2
1. update : negated conditional → KILLED 2. update : negated conditional → KILLED |
if(StringUtils.isNullOrEmpty(participantDto.getNom()) || StringUtils.isNullOrEmpty(participantDto.getPrenom())) |
| 101 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Précisez au moins un nom ou un prénom."); | |
| 102 | try { | |
| 103 |
1
1. update : negated conditional → KILLED |
if (mapper.map(service.getById(id), ParticipantDto.class).equals(participantDto)) |
| 104 | throw new NoUpdateException("Le participant n'a pas été modifié."); | |
| 105 | var model = mapper.map(participantDto, Participant.class); | |
| 106 | var result = service.update(id, model); | |
| 107 |
1
1. update : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/ParticipantController::update → KILLED |
return mapper.map(result, ParticipantDto.class); |
| 108 | } | |
| 109 | catch(NoUpdateException e) { | |
| 110 | throw new ResponseStatusException(HttpStatus.NO_CONTENT, e.getMessage()); | |
| 111 | } | |
| 112 | catch(NoResultException e){ | |
| 113 | throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | |
| 114 | } | |
| 115 | catch(NoSuchElementException e){ | |
| 116 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 117 | } | |
| 118 | catch (Exception e) { | |
| 119 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la mise à jour du participant."); | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * Delete a participant | |
| 125 | * @param id du participant à supprimer | |
| 126 | */ | |
| 127 | @DeleteMapping(value = "/{id}") | |
| 128 | @ResponseStatus(HttpStatus.NO_CONTENT) | |
| 129 | @Operation(summary = "Supprimer un participant", description = "Ne retourne rien.") | |
| 130 | public void delete(@PathVariable("id") Long id) { | |
| 131 | try { | |
| 132 |
1
1. delete : removed call to fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/services/ParticipantService::delete → KILLED |
service.delete(id); |
| 133 | } | |
| 134 | catch(NoSuchElementException e){ | |
| 135 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 136 | } | |
| 137 | catch (Exception e) { | |
| 138 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la suppression du commentaire."); | |
| 139 | } | |
| 140 | } | |
| 141 | } | |
Mutations | ||
| 38 |
1.1 2.2 |
|
| 43 |
1.1 |
|
| 60 |
1.1 2.2 |
|
| 80 |
1.1 |
|
| 100 |
1.1 2.2 |
|
| 103 |
1.1 |
|
| 107 |
1.1 |
|
| 132 |
1.1 |