| 1 | package fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.controllers; | |
| 2 | ||
| 3 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.dtos.SondageDto; | |
| 4 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.exception.NoUpdateException; | |
| 5 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.models.Sondage; | |
| 6 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.services.DateSondeeService; | |
| 7 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.services.SondageService; | |
| 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.Date; | |
| 17 | import java.util.List; | |
| 18 | import java.util.NoSuchElementException; | |
| 19 | ||
| 20 | @RestController | |
| 21 | @RequestMapping(value = "/api/sondage") | |
| 22 | public class SondageController { | |
| 23 | ||
| 24 | @Autowired | |
| 25 | private SondageService service; | |
| 26 | ||
| 27 | @Autowired | |
| 28 | private DateSondeeService request; | |
| 29 | ||
| 30 | @Autowired | |
| 31 | private ModelMapper mapper; | |
| 32 | ||
| 33 | /** | |
| 34 | * Create a sondage | |
| 35 | * @param sondageDto sondage à créer | |
| 36 | * @return sondage créé | |
| 37 | */ | |
| 38 | @PostMapping(value = "/") | |
| 39 | @ResponseStatus(HttpStatus.CREATED) | |
| 40 | @Operation(summary = "Créer un sondage", description = "Retourne le sondage créé.") | |
| 41 | public SondageDto create(@RequestBody SondageDto sondageDto) { | |
| 42 |
2
1. create : negated conditional → KILLED 2. create : negated conditional → KILLED |
if(sondageDto.getFin() == null || sondageDto.getFin().before(new Date())) |
| 43 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "La date de fin doit être supérieure à la date de début."); | |
| 44 |
1
1. create : negated conditional → KILLED |
if(sondageDto.getCreateBy() == null) |
| 45 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le sondage doit avoir un créateur."); | |
| 46 |
1
1. create : negated conditional → KILLED |
if(sondageDto.getNom() == null) |
| 47 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le sondage doit avoir un nom."); | |
| 48 |
1
1. create : negated conditional → KILLED |
if(sondageDto.getDescription() == null) |
| 49 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le sondage doit avoir une description."); | |
| 50 |
2
1. create : negated conditional → KILLED 2. create : negated conditional → KILLED |
if(sondageDto.getCloture() == null || sondageDto.getCloture()) |
| 51 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Le sondage doit être ouvert."); | |
| 52 | try { | |
| 53 | var model = mapper.map(sondageDto, Sondage.class); | |
| 54 | var result = service.create(sondageDto.getCreateBy(), model); | |
| 55 |
1
1. create : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::create → KILLED |
return mapper.map(result, SondageDto.class); |
| 56 | } | |
| 57 | catch(NoSuchElementException e) { | |
| 58 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 59 | } | |
| 60 | catch (Exception e) { | |
| 61 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la création du sondage."); | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Get ALl sondages | |
| 67 | * @return Liste des sondages existants | |
| 68 | */ | |
| 69 | @GetMapping(value = "/") | |
| 70 | @ResponseStatus(HttpStatus.OK) | |
| 71 | @Operation(summary = "Récupérer tous les sondages", description = "Retourne tous les sondages existants.") | |
| 72 | public List<SondageDto> getAllSondages() { | |
| 73 | try { | |
| 74 | var models = service.getAll(); | |
| 75 |
2
1. getAllSondages : replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::getAllSondages → KILLED 2. lambda$getAllSondages$0 : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::lambda$getAllSondages$0 → KILLED |
return models.stream().map(model -> mapper.map(model, SondageDto.class)).toList(); |
| 76 | } | |
| 77 | catch(NoResultException e) { | |
| 78 | throw new ResponseStatusException(HttpStatus.NO_CONTENT, e.getMessage()); | |
| 79 | } | |
| 80 | catch(Exception e) { | |
| 81 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération des sondages."); | |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Get sondage by id | |
| 87 | * @param id du sondage | |
| 88 | * @return Sondage correspondant à l'id | |
| 89 | */ | |
| 90 | @GetMapping(value = "/{id}") | |
| 91 | @ResponseStatus(HttpStatus.OK) | |
| 92 | @Operation(summary = "Récupérer un sondage par son id", description = "Retourne le sondage correspondant.") | |
| 93 | public SondageDto getSondageById(@PathVariable("id") Long id) { | |
| 94 | try{ | |
| 95 | var model = service.getById(id); | |
| 96 |
1
1. getSondageById : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::getSondageById → KILLED |
return mapper.map(model, SondageDto.class); |
| 97 | } | |
| 98 | catch(NoResultException e) { | |
| 99 | throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage()); | |
| 100 | } | |
| 101 | catch (Exception e) { | |
| 102 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération du sondage."); | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Get Meilleur date d'un sondage | |
| 108 | * @param id du sondage | |
| 109 | * @return Liste des meilleures dates | |
| 110 | */ | |
| 111 | @GetMapping(value = "/{id}/best") | |
| 112 | @ResponseStatus(HttpStatus.OK) | |
| 113 | @Operation(summary = "Récupérer les meilleures dates pour un sondage", description = "Retourne la liste des meilleures dates pour un sondage donné.") | |
| 114 | public List<Date> getBestDateBySondageId(@PathVariable("id") Long id) { | |
| 115 | try { | |
| 116 |
1
1. getBestDateBySondageId : replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::getBestDateBySondageId → KILLED |
return request.getBestDateBySondageId(id); |
| 117 | } | |
| 118 | catch(NoSuchElementException e) { | |
| 119 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 120 | } | |
| 121 | catch (Exception e) { | |
| 122 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération du sondage."); | |
| 123 | } | |
| 124 | } | |
| 125 | ||
| 126 | /** | |
| 127 | * Get Possible meilleur date d'un sondage | |
| 128 | * @param id du sondage | |
| 129 | * @return Liste des potentielles meilleures dates | |
| 130 | */ | |
| 131 | @GetMapping(value = "/{id}/maybe") | |
| 132 | @ResponseStatus(HttpStatus.OK) | |
| 133 | @Operation(summary = "Récupérer les potentielles meilleures dates d'un sondage", description = "Retourne la liste des potentielles meilleures dates pour un sondage donné.") | |
| 134 | public List<Date> getMaybeBestBySondageId(@PathVariable("id") Long id) { | |
| 135 | try { | |
| 136 |
1
1. getMaybeBestBySondageId : replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::getMaybeBestBySondageId → KILLED |
return request.getMaybeBestDateBySondageId(id); |
| 137 | } | |
| 138 | catch(NoSuchElementException e) { | |
| 139 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 140 | } | |
| 141 | catch (Exception e) { | |
| 142 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la récupération du sondage."); | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | /** | |
| 147 | * Update a sondage | |
| 148 | * @param id du sondage à modifier | |
| 149 | * @param sondageDto sondage à modifier | |
| 150 | * @return sondage modifié | |
| 151 | */ | |
| 152 | @PutMapping(value = "/{id}") | |
| 153 | @ResponseStatus(HttpStatus.OK) | |
| 154 | @Operation(summary = "Modifier un sondage", description = "Retourne le sondage modifié.") | |
| 155 | public SondageDto update(@PathVariable("id") Long id, @RequestBody SondageDto sondageDto) { | |
| 156 |
2
1. update : negated conditional → KILLED 2. update : negated conditional → KILLED |
if(sondageDto.getFin() == null || sondageDto.getFin().before(new Date())) |
| 157 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "La date de fin doit être supérieure à la date de début."); | |
| 158 | try { | |
| 159 |
1
1. update : negated conditional → KILLED |
if (service.getById(id).equals(mapper.map(sondageDto, Sondage.class))) |
| 160 | throw new NoUpdateException("Le sondage n'a pas été modifié."); | |
| 161 | Sondage model = mapper.map(sondageDto, Sondage.class); | |
| 162 | var result = service.update(id, model); | |
| 163 |
1
1. update : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/SondageController::update → KILLED |
return mapper.map(result, SondageDto.class); |
| 164 | } | |
| 165 | catch(NoUpdateException e) { | |
| 166 | throw new ResponseStatusException(HttpStatus.NO_CONTENT, e.getMessage()); | |
| 167 | } | |
| 168 | catch(NoResultException e){ | |
| 169 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 170 | } | |
| 171 | catch (Exception e) { | |
| 172 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la mise à jour du sondage."); | |
| 173 | } | |
| 174 | } | |
| 175 | ||
| 176 | /** | |
| 177 | * Delete a sondage | |
| 178 | * @param id du sondage à supprimer | |
| 179 | */ | |
| 180 | @DeleteMapping(value = "/{id}") | |
| 181 | @ResponseStatus(HttpStatus.NO_CONTENT) | |
| 182 | @Operation(summary = "Supprimer un sondage", description = "Ne retourne rien.") | |
| 183 | public void delete(@PathVariable("id") Long id) { | |
| 184 | try { | |
| 185 |
1
1. delete : removed call to fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/services/SondageService::delete → KILLED |
service.delete(id); |
| 186 | } | |
| 187 | catch(NoSuchElementException e) { | |
| 188 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 189 | } | |
| 190 | catch (Exception e) { | |
| 191 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la suppression du sondage."); | |
| 192 | } | |
| 193 | } | |
| 194 | } | |
Mutations | ||
| 42 |
1.1 2.2 |
|
| 44 |
1.1 |
|
| 46 |
1.1 |
|
| 48 |
1.1 |
|
| 50 |
1.1 2.2 |
|
| 55 |
1.1 |
|
| 75 |
1.1 2.2 |
|
| 96 |
1.1 |
|
| 116 |
1.1 |
|
| 136 |
1.1 |
|
| 156 |
1.1 2.2 |
|
| 159 |
1.1 |
|
| 163 |
1.1 |
|
| 185 |
1.1 |