fix dto utilisé

Springboot
Victor SOULIER 1 year ago
parent 81a33c9460
commit fd260b863d

@ -19,13 +19,15 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api/v1/indices") @RequestMapping("/api/v1/indices")
public class IndiceController extends Controller { public class IndiceController extends Controller {
private final IndiceService indiceService; private final IndiceService indiceService;
private final IndiceRepository indiceRepository;
private final ModelMapper modelMapper; private final ModelMapper modelMapper;
@PatchMapping(value="/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @PatchMapping(value="/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
public IndiceSimpleWithScientifiquesIdDTO patchIndice(@PathVariable("id") int id, @RequestBody @Valid IndiceWithoutIdAndScientifiqueIdOnlyForPatchDTO indiceInput) throws JsonMappingException { public IndiceSimpleWithScientifiquesIdDTO patchIndice(@PathVariable("id") int id, @RequestBody @Valid IndiceWithoutIdAndScientifiqueIdOnlyForPatchDTO indiceInput) throws IllegalAccessException {
Indice indice = this.modelMapper.map(indiceInput, Indice.class); Indice indicePartial = this.modelMapper.map(indiceInput, Indice.class);
indice.setId(id); Indice indice = this.indiceRepository.getById(id);
indice = this.indiceService.patch(indicePartial, indice);
return this.modelMapper.map(this.indiceService.update(indice), IndiceSimpleWithScientifiquesIdDTO.class); return this.modelMapper.map(this.indiceService.update(indice), IndiceSimpleWithScientifiquesIdDTO.class);
} }

@ -5,12 +5,15 @@ import fr.iut.sciencequest.sae.assemblers.IndiceModelAssembler;
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.ThematiqueDTO; 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.dto.thematique.ThematiqueSimpleDTO;
import fr.iut.sciencequest.sae.entities.Difficulte; import fr.iut.sciencequest.sae.entities.Difficulte;
import fr.iut.sciencequest.sae.entities.Thematique; import fr.iut.sciencequest.sae.entities.Thematique;
import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService; import fr.iut.sciencequest.sae.services.interfaces.IThematiqueService;
import jakarta.annotation.Nullable;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault; import org.springframework.data.web.PageableDefault;
@ -32,6 +35,7 @@ public class ThematiqueController extends Controller {
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;
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE) @GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ -40,17 +44,17 @@ public class ThematiqueController extends Controller {
} }
//TODO : gestion des erreurs remontées par @Valid //TODO : gestion des erreurs remontées par @Valid
//TODO : ajouter liens hateos
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE) @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED) @ResponseStatus(HttpStatus.CREATED)
public ThematiqueDTO createThematique(@Valid @RequestBody Thematique thematique) { public ThematiqueSimpleDTO createThematique(@Valid @RequestBody ThematiqueLibelleOnlyDTO thematique) {
return thematiqueModelAssembler.toModel(this.thematiqueService.create(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) @PatchMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED) @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); thematique.setId(id);
return thematiqueModelAssembler.toModel(this.thematiqueService.update(thematique)); return thematiqueSimpleModelAssembler.toModel(this.thematiqueService.update(thematique));
} }
} }

@ -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<ThematiqueLibelleOnlyDTO> {
@NotBlank
private String libelle;
}

@ -32,7 +32,7 @@ public class ThematiqueService implements IThematiqueService {
@Override @Override
public Thematique create(Thematique thematique){ public Thematique create(Thematique thematique){
if(this.thematiqueRepository.existsById(thematique.getId())){ if(thematique.getId() != null && this.thematiqueRepository.existsById(thematique.getId())){
throw new DuplicatedIdException(); throw new DuplicatedIdException();
} }
this.checkFieldsConstraints(thematique); this.checkFieldsConstraints(thematique);

Loading…
Cancel
Save