support patch thematiques/{id}

Springboot
Victor SOULIER 1 year ago
parent 368d1bda0c
commit 1c8b8605f9

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

@ -35,6 +35,11 @@ public class ThematiqueService {
this.checkFieldsConstraints(thematique); this.checkFieldsConstraints(thematique);
return this.thematiqueRepository.save(thematique); return this.thematiqueRepository.save(thematique);
} }
public Thematique findById(int id) {
return this.thematiqueRepository.findById(id).orElseThrow(() ->
new ThematiqueNotFoundException(id)
);
}
public Page<Thematique> findAll(Pageable p){ public Page<Thematique> findAll(Pageable p){
return this.thematiqueRepository.findAll(p); return this.thematiqueRepository.findAll(p);

Loading…
Cancel
Save