diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/IndiceController.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/IndiceController.java index b1b88f6..63f97fb 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/IndiceController.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/IndiceController.java @@ -19,13 +19,15 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/api/v1/indices") public class IndiceController extends Controller { private final IndiceService indiceService; + private final IndiceRepository indiceRepository; private final ModelMapper modelMapper; @PatchMapping(value="/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) - public IndiceSimpleWithScientifiquesIdDTO patchIndice(@PathVariable("id") int id, @RequestBody @Valid IndiceWithoutIdAndScientifiqueIdOnlyForPatchDTO indiceInput) throws JsonMappingException { - Indice indice = this.modelMapper.map(indiceInput, Indice.class); - indice.setId(id); + public IndiceSimpleWithScientifiquesIdDTO patchIndice(@PathVariable("id") int id, @RequestBody @Valid IndiceWithoutIdAndScientifiqueIdOnlyForPatchDTO indiceInput) throws IllegalAccessException { + Indice indicePartial = this.modelMapper.map(indiceInput, Indice.class); + Indice indice = this.indiceRepository.getById(id); + indice = this.indiceService.patch(indicePartial, indice); return this.modelMapper.map(this.indiceService.update(indice), IndiceSimpleWithScientifiquesIdDTO.class); } diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/ThematiqueController.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/ThematiqueController.java index 1e1ce85..2b8fab0 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/ThematiqueController.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/controllers/ThematiqueController.java @@ -5,12 +5,15 @@ import fr.iut.sciencequest.sae.assemblers.IndiceModelAssembler; import fr.iut.sciencequest.sae.assemblers.ThematiqueModelAssembler; import fr.iut.sciencequest.sae.assemblers.ThematiqueSimpleModelAssembler; import fr.iut.sciencequest.sae.dto.thematique.ThematiqueDTO; +import fr.iut.sciencequest.sae.dto.thematique.ThematiqueLibelleOnlyDTO; import fr.iut.sciencequest.sae.dto.thematique.ThematiqueSimpleDTO; import fr.iut.sciencequest.sae.entities.Difficulte; import fr.iut.sciencequest.sae.entities.Thematique; import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService; +import jakarta.annotation.Nullable; import jakarta.validation.Valid; import lombok.AllArgsConstructor; +import org.modelmapper.ModelMapper; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.web.PageableDefault; @@ -32,6 +35,7 @@ public class ThematiqueController extends Controller { private final ThematiqueSimpleModelAssembler thematiqueSimpleModelAssembler; @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") private final PagedResourcesAssembler pagedResourcesAssembler; + private final ModelMapper modelMapper; @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) @@ -40,17 +44,17 @@ public class ThematiqueController extends Controller { } //TODO : gestion des erreurs remontées par @Valid - //TODO : ajouter liens hateos @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) - public ThematiqueDTO createThematique(@Valid @RequestBody Thematique thematique) { - return thematiqueModelAssembler.toModel(this.thematiqueService.create(thematique)); + public ThematiqueSimpleDTO createThematique(@Valid @RequestBody ThematiqueLibelleOnlyDTO thematique) { + return thematiqueSimpleModelAssembler.toModel(this.thematiqueService.create(this.modelMapper.map(thematique, Thematique.class))); } @PatchMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) - public ThematiqueDTO updateThematique(@PathVariable("id") int id, @Valid @RequestBody Thematique thematique) { + public ThematiqueSimpleDTO updateThematique(@PathVariable("id") int id, @Valid @RequestBody ThematiqueLibelleOnlyDTO partialThematique) { + Thematique thematique = this.modelMapper.map(partialThematique, Thematique.class); thematique.setId(id); - return thematiqueModelAssembler.toModel(this.thematiqueService.update(thematique)); + return thematiqueSimpleModelAssembler.toModel(this.thematiqueService.update(thematique)); } } diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/dto/thematique/ThematiqueLibelleOnlyDTO.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/dto/thematique/ThematiqueLibelleOnlyDTO.java new file mode 100644 index 0000000..78647db --- /dev/null +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/dto/thematique/ThematiqueLibelleOnlyDTO.java @@ -0,0 +1,17 @@ +package fr.iut.sciencequest.sae.dto.thematique; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.*; +import org.springframework.hateoas.RepresentationModel; + + +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode(callSuper = false) +public class ThematiqueLibelleOnlyDTO extends RepresentationModel { + @NotBlank + private String libelle; +} diff --git a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/ThematiqueService.java b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/ThematiqueService.java index 1df8fd8..889f78d 100644 --- a/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/ThematiqueService.java +++ b/SpringBootProject/src/main/java/fr/iut/sciencequest/sae/services/ThematiqueService.java @@ -32,7 +32,7 @@ public class ThematiqueService implements IThematiqueService { @Override public Thematique create(Thematique thematique){ - if(this.thematiqueRepository.existsById(thematique.getId())){ + if(thematique.getId() != null && this.thematiqueRepository.existsById(thematique.getId())){ throw new DuplicatedIdException(); } this.checkFieldsConstraints(thematique);