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 52a9d6c..73cc7b2 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 @@ -1,11 +1,13 @@ package fr.iut.sciencequest.sae.controllers; +import com.fasterxml.jackson.databind.ObjectMapper; import fr.iut.sciencequest.sae.ApplicationConfig; import fr.iut.sciencequest.sae.assemblers.ThematiqueModelAssembler; import fr.iut.sciencequest.sae.assemblers.ThematiqueSimpleModelAssembler; import fr.iut.sciencequest.sae.dto.thematique.ThematiqueLibelleOnlyDTO; import fr.iut.sciencequest.sae.dto.thematique.ThematiqueSimpleDTO; import fr.iut.sciencequest.sae.entities.Thematique; +import fr.iut.sciencequest.sae.services.ScientifiqueService; import fr.iut.sciencequest.sae.services.ThematiqueService; import jakarta.validation.Valid; import lombok.AllArgsConstructor; @@ -24,11 +26,13 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/api/v1/thematiques") public class ThematiqueController { private final ThematiqueService thematiqueService; + private final ScientifiqueService scientifiqueService; private final ThematiqueModelAssembler thematiqueModelAssembler; private final ThematiqueSimpleModelAssembler thematiqueSimpleModelAssembler; @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") private final PagedResourcesAssembler pagedResourcesAssembler; private final ModelMapper modelMapper; + private final ObjectMapper objectMapper; @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.OK) @@ -46,8 +50,10 @@ public class ThematiqueController { @PatchMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseStatus(HttpStatus.CREATED) public ThematiqueSimpleDTO updateThematique(@PathVariable("id") int id, @Valid @RequestBody ThematiqueLibelleOnlyDTO partialThematique) { - Thematique thematique = this.modelMapper.map(partialThematique, Thematique.class); - thematique.setId(id); - return thematiqueSimpleModelAssembler.toModel(this.thematiqueService.update(thematique)); + Thematique dstThematique = this.thematiqueService.findById(id); + if(partialThematique.getLibelle() != null){ + dstThematique.setLibelle(partialThematique.getLibelle()); + } + return thematiqueSimpleModelAssembler.toModel(this.thematiqueService.update(dstThematique)); } } 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 802fea7..8a96ed7 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 @@ -35,6 +35,11 @@ public class ThematiqueService { this.checkFieldsConstraints(thematique); return this.thematiqueRepository.save(thematique); } + public Thematique findById(int id) { + return this.thematiqueRepository.findById(id).orElseThrow(() -> + new ThematiqueNotFoundException(id) + ); + } public Page findAll(Pageable p){ return this.thematiqueRepository.findAll(p);