Skip to content
Snippets Groups Projects
Commit 3486cec7 authored by Skander Ayadi's avatar Skander Ayadi
Browse files

Merge branch 'feature/correction-bugs' into 'develop'

[FEAT] Refactor addPersonalVehicle method to support both adding and updating personal vehicles

See merge request madeline.carpentier.etu/miage_glop_user_management!22
parents 3cd3faa9 57734f7e
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ package com.miage.glop.client;
import com.miage.glop.beneficiary.Beneficiary;
import com.miage.glop.beneficiary.BeneficiaryService;
import com.miage.glop.exception.ResourceNotFoundException;
import com.miage.glop.personalVehicle.PersonalVehicle;
import com.miage.glop.personalVehicle.PersonalVehicleService;
import jakarta.validation.Valid;
......@@ -121,8 +122,40 @@ public class ClientController {
}
@PostMapping("/{idClient}/addPersonalVehicle")
public ResponseEntity<Client> addPersonalVehicle(@PathVariable String idClient, @RequestBody @Valid PersonalVehicle personalVehicle) {
public ResponseEntity<Client> addOrUpdatePersonalVehicle(
@PathVariable String idClient,
@RequestBody @Valid PersonalVehicle personalVehicle) {
// 1. Récupère le client
Client client = clientService.findClientById(idClient);
// 2. Vérifie si le véhicule existe déjà (si un ID est présent)
PersonalVehicle existingVehicle = null;
try {
existingVehicle = personalVehicleService.findPersonalVehicleById(personalVehicle.getId());
} catch (ResourceNotFoundException e) {
existingVehicle = null;
}
// 3a. Cas UPDATE: le véhicule existe déjà
if (existingVehicle != null) {
// Met à jour les champs du véhicule existant
PersonalVehicle updatedVehicle = existingVehicle;
updatedVehicle.setId(personalVehicle.getId());
updatedVehicle.setHidden(personalVehicle.isHidden());
updatedVehicle.setLabel(personalVehicle.getLabel());
updatedVehicle.setType(personalVehicle.getType());
updatedVehicle.setYear(personalVehicle.getYear());
updatedVehicle.setModelId(personalVehicle.getModelId());
// Sauvegarde les modifications
personalVehicleService.update(updatedVehicle);
// Retourne le client mis à jour
return ResponseEntity.ok(client);
}
// 3b. Cas INSERT: nouveau véhicule
client.getPersonalVehicles().add(personalVehicleService.insert(personalVehicle));
clientService.update(client);
return ResponseEntity.ok(client);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment