| 1 | package fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.controllers; | |
| 2 | ||
| 3 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.dtos.DateSondageDto; | |
| 4 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.exception.DateSondageAlreadyExistsException; | |
| 5 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.models.DateSondage; | |
| 6 | import fr.univ.lorraine.ufr.mim.m2.gi.mysurvey.services.DateSondageService; | |
| 7 | import io.swagger.v3.oas.annotations.Operation; | |
| 8 | import jakarta.persistence.NoResultException; | |
| 9 | import org.modelmapper.ModelMapper; | |
| 10 | import org.springframework.beans.factory.annotation.Autowired; | |
| 11 | import org.springframework.http.HttpStatus; | |
| 12 | import org.springframework.web.bind.annotation.*; | |
| 13 | import org.springframework.web.server.ResponseStatusException; | |
| 14 | ||
| 15 | import java.util.Date; | |
| 16 | import java.util.List; | |
| 17 | import java.util.NoSuchElementException; | |
| 18 | import java.util.TimeZone; | |
| 19 | ||
| 20 | @RestController | |
| 21 | @RequestMapping(value = "/api/datesondage") | |
| 22 | public class DateSondageController { | |
| 23 | ||
| 24 | @Autowired | |
| 25 | private DateSondageService service; | |
| 26 | ||
| 27 | @Autowired | |
| 28 | private ModelMapper mapper; | |
| 29 | ||
| 30 | /** | |
| 31 | * Création d'une date de sondage pour un sondage | |
| 32 | * @param id d'un sondage | |
| 33 | * @param dto du sondage à créer | |
| 34 | * @return sondage créé | |
| 35 | */ | |
| 36 | @PostMapping(value = "/{id}") | |
| 37 | @ResponseStatus(HttpStatus.CREATED) | |
| 38 | @Operation(summary = "Créer un sondage", description = "Retourne le sondage créé.") | |
| 39 | public DateSondageDto create(@PathVariable("id") Long id, @RequestBody DateSondageDto dto) { | |
| 40 | TimeZone tz = TimeZone.getDefault(); // pour enlever le fuseau horaire set par le système local | |
| 41 |
1
1. create : negated conditional → KILLED |
if (dto.getDate() == null) |
| 42 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Précisez une date valide."); | |
| 43 |
1
1. create : Replaced long subtraction with addition → SURVIVED |
Date localDTO = new Date(dto.getDate().getTime() - tz.getRawOffset()); |
| 44 |
1
1. create : negated conditional → KILLED |
if(localDTO.before(new Date())) |
| 45 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Vous ne pouvez pas donner une date dans le passé"); | |
| 46 | try{ | |
| 47 |
1
1. create : removed call to fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/services/DateSondageService::checkIfDateAlreadyExists → KILLED |
service.checkIfDateAlreadyExists(id, dto); |
| 48 | var model = mapper.map(dto, DateSondage.class); | |
| 49 | var result = service.create(id, model); | |
| 50 |
1
1. create : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/DateSondageController::create → KILLED |
return mapper.map(result, DateSondageDto.class); |
| 51 | } catch(DateSondageAlreadyExistsException | NoSuchElementException e){ | |
| 52 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 53 | } | |
| 54 | catch (Exception e) { | |
| 55 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la création de la date."); | |
| 56 | } | |
| 57 | } | |
| 58 | ||
| 59 | /** | |
| 60 | * Récupération des dates d'un sondage | |
| 61 | * @param id du sondage | |
| 62 | * @return liste des dates récupérées | |
| 63 | */ | |
| 64 | @GetMapping(value = "/{id}") | |
| 65 | @ResponseStatus(HttpStatus.OK) | |
| 66 | @Operation(summary = "Récupérer la liste des dates d'un sondage", description = "Retourne la liste récupérée.") | |
| 67 | public List<DateSondageDto> getAllDatesBySondageId(@PathVariable("id") Long id) { | |
| 68 | try { | |
| 69 | var models = service.getBySondageId(id); | |
| 70 |
1
1. getAllDatesBySondageId : replaced return value with Collections.emptyList for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/DateSondageController::getAllDatesBySondageId → KILLED |
return models.stream() |
| 71 |
1
1. lambda$getAllDatesBySondageId$0 : replaced return value with null for fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/controllers/DateSondageController::lambda$getAllDatesBySondageId$0 → KILLED |
.map(model -> mapper.map(model, DateSondageDto.class)) |
| 72 | .toList(); | |
| 73 | } | |
| 74 | catch(NoSuchElementException e){ | |
| 75 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 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 dates."); | |
| 82 | } | |
| 83 | } | |
| 84 | ||
| 85 | /** | |
| 86 | * Suppression d'une date d'un sondage | |
| 87 | * @param id de la date à supprimer | |
| 88 | */ | |
| 89 | @DeleteMapping(value = "/{id}") | |
| 90 | @ResponseStatus(HttpStatus.NO_CONTENT) | |
| 91 | @Operation(summary = "Supprimer une date de sondage", description = "Ne retourne rien.") | |
| 92 | public void delete(@PathVariable("id") Long id) { | |
| 93 | try { | |
| 94 |
1
1. delete : removed call to fr/univ/lorraine/ufr/mim/m2/gi/mysurvey/services/DateSondageService::delete → KILLED |
service.delete(id); |
| 95 | } | |
| 96 | catch(NoSuchElementException e){ | |
| 97 | throw new ResponseStatusException(HttpStatus.BAD_REQUEST, e.getMessage()); | |
| 98 | } | |
| 99 | catch (Exception e) { | |
| 100 | throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Erreur lors de la suppression de la date."); | |
| 101 | } | |
| 102 | } | |
| 103 | } | |
Mutations | ||
| 41 |
1.1 |
|
| 43 |
1.1 |
|
| 44 |
1.1 |
|
| 47 |
1.1 |
|
| 50 |
1.1 |
|
| 70 |
1.1 |
|
| 71 |
1.1 |
|
| 94 |
1.1 |