reformat controller

This commit is contained in:
mepiphana2023
2025-07-16 11:48:43 +02:00
parent bb927ff282
commit 6bbf347101
8 changed files with 153 additions and 144 deletions

View File

@@ -2,9 +2,14 @@ package fr.eni.demo.bll;
import fr.eni.demo.bo.Adresse;
import java.util.List;
public interface AdresseService {
Adresse add(Adresse adresse);
Adresse findById(Long id);
List<Adresse> findAll();
Adresse update(Long id, Adresse adresse);
void delete(Long id);
Adresse findAdresseByClientId(Long clientId);
}

View File

@@ -4,22 +4,56 @@ import fr.eni.demo.bo.Adresse;
import fr.eni.demo.bo.Client;
import fr.eni.demo.dal.AdresseRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@RequiredArgsConstructor
public class AdresseServiceImpl implements AdresseService {
private AdresseRepository adresseRepo;
@Autowired
private ClientService clientService;
public AdresseServiceImpl(AdresseRepository adresseRepo) {
this.adresseRepo = adresseRepo;
}
private final AdresseRepository adresseRepo;
private final ClientService clientService;
@Override
public Adresse add(Adresse adresse) {
validateAdresse(adresse);
return adresseRepo.save(adresse);
}
@Override
public Adresse findById(Long id) {
return adresseRepo.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Adresse non trouvée"));
}
@Override
public List<Adresse> findAll() {
return adresseRepo.findAll();
}
@Override
public Adresse update(Long id, Adresse updatedAdresse) {
Adresse existing = findById(id);
updatedAdresse.setId(existing.getId()); // conserve l'id existant
validateAdresse(updatedAdresse);
return adresseRepo.save(updatedAdresse);
}
@Override
public void delete(Long id) {
Adresse adresse = findById(id);
adresseRepo.delete(adresse);
}
@Override
public Adresse findAdresseByClientId(Long clientId) {
Client client = clientService.findById(clientId);
return client.getAdresse();
}
private void validateAdresse(Adresse adresse) {
if (adresse == null) {
throw new IllegalArgumentException("Adresse is null");
}
@@ -32,24 +66,5 @@ public class AdresseServiceImpl implements AdresseService{
if (adresse.getVille() == null || adresse.getVille().isBlank()) {
throw new IllegalArgumentException("Ville is null or empty");
}
adresseRepo.save(adresse);
return adresse;
}
@Override
public Adresse findAdresseByClientId(Long clientId) {
if (clientId == null) {
throw new IllegalArgumentException("clientId is null");
}
System.out.println(clientId);
Client client = clientService.findById(clientId);
System.out.println(client);
return client.getAdresse();
}
@Override
public Adresse findById(Long id) {
return adresseRepo.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Adresse non trouvée"));
}
}

View File

@@ -1,16 +1,14 @@
package fr.eni.demo.controller;
import fr.eni.demo.bll.AdresseService;
import fr.eni.demo.bll.StockService;
import fr.eni.demo.bo.Adresse;
import fr.eni.demo.bo.Stock;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/api/address")
@@ -19,37 +17,53 @@ public class AddressController {
private final AdresseService adresseService;
// Creer nouvelle adresse
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Adresse adresse) {
adresseService.add(adresse);
Map<String, Object> response = new HashMap<>();
response.put("message", "Adresse added");
response.put("status", true);
response.put("data", new HashMap<>());
return ResponseEntity.ok(response);
Adresse saved = adresseService.add(adresse);
return buildResponse("Adresse added", true, saved);
}
// Recuperer adresse par id
@GetMapping("/{id}")
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) {
Adresse result = adresseService.findById(id);
Map<String, Object> response = new HashMap<>();
response.put("message", "Adresse find");
response.put("status", true);
response.put("data", result);
public ResponseEntity<Map<String, Object>> findById(@PathVariable String id) {
Adresse result = adresseService.findById(Long.valueOf(id));
return buildResponse("Adresse found", true, result);
}
// Recuperer toutes les adresses
@GetMapping
public ResponseEntity<Map<String, Object>> findAll() {
List<Adresse> result = adresseService.findAll();
return buildResponse("All adresses fetched", true, result);
}
// Maj adresse par id
@PutMapping("/{id}")
public ResponseEntity<Map<String, Object>> update(@PathVariable String id, @RequestBody Adresse adresse) {
Adresse updated = adresseService.update(Long.valueOf(id), adresse);
return buildResponse("Adresse updated", true, updated);
}
// Supprimer adresse par id
@DeleteMapping("/{id}")
public ResponseEntity<Map<String, Object>> delete(@PathVariable String id) {
adresseService.delete(Long.valueOf(id));
return buildResponse("Adresse deleted", true, null);
}
// Recuperer adresse associee à un client via son id
@GetMapping("/client/{clientId}")
public ResponseEntity<Map<String, Object>> findByClientId(@PathVariable String clientId) {
Adresse result = adresseService.findAdresseByClientId(Long.valueOf(clientId));
return buildResponse("Adresse found for client", true, result);
}
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
Map<String, Object> response = new HashMap<>();
response.put("message", message);
response.put("status", status);
response.put("data", data);
return ResponseEntity.ok(response);
}
@GetMapping("/client/{id}")
public ResponseEntity<Map<String, Object>> findByIdClient(@PathVariable Long id) {
Adresse result = adresseService.findAdresseByClientId(id);
Map<String, Object> response = new HashMap<>();
response.put("message", "Adresse find");
response.put("status", true);
response.put("data", result);
return ResponseEntity.ok(response);
}
}

View File

@@ -18,64 +18,46 @@ public class ClientController {
private final ClientService clientService;
// Ajouter un client
// Creer un client
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Client client) {
clientService.add(client);
Map<String, Object> response = new HashMap<>();
response.put("message", "Client created successfully");
response.put("status", 201);
response.put("data", new HashMap<>());
return ResponseEntity.ok(response);
return buildResponse("Client created", true, new HashMap<>());
}
// Chercher des clients par nom
// Chercher par nom
@GetMapping("/name/{name}")
public ResponseEntity<Map<String, Object>> findByEmail(@PathVariable String name) {
public ResponseEntity<Map<String, Object>> findByName(@PathVariable String name) {
List<Client> result = clientService.findByName(name);
Map<String, Object> response = new HashMap<>();
response.put("message", "List of Clients found");
response.put("status", 200);
response.put("data", result);
return ResponseEntity.ok(response);
return buildResponse("Clients found", true, result);
}
// Chercher des clients par nom
// Chercher par id
@GetMapping("/{id}")
public ResponseEntity<Map<String, Object>> findByEmail(@PathVariable Long id) {
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) {
Client result = clientService.findById(id);
Map<String, Object> response = new HashMap<>();
response.put("message", "Client found");
response.put("status", true);
response.put("data", result);
return ResponseEntity.ok(response);
return buildResponse("Client found", true, result);
}
// Modifier un client
// MAJ client complet
@PutMapping("/{id}")
public ResponseEntity<Map<String, Object>> fullUpdate(@PathVariable Long id, @RequestBody Client client, @RequestBody Adresse adresse) {
clientService.fullUpdate(id, client, adresse);
Map<String, Object> response = new HashMap<>();
response.put("message", "Client updated successfully");
response.put("status", true);
response.put("data", new HashMap<>());
return ResponseEntity.ok(response);
public ResponseEntity<Map<String, Object>> fullUpdate(@PathVariable Long id, @RequestBody Client client) {
clientService.fullUpdate(id, client, client.getAdresse());
return buildResponse("Client updated", true, new HashMap<>());
}
// Modifier l'address d'un client
// MAJ adresse seule
@PutMapping("/address/{id}")
public ResponseEntity<Map<String, Object>> updateAddress(@PathVariable Long id, @RequestBody Adresse adresse) {
clientService.updateLocation(id, adresse);
Map<String, Object> response = new HashMap<>();
response.put("message", "Client address updated successfully");
response.put("status", true);
response.put("data", new HashMap<>());
return buildResponse("Address updated", true, new HashMap<>());
}
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
Map<String, Object> response = new HashMap<>();
response.put("message", message);
response.put("status", status);
response.put("data", data);
return ResponseEntity.ok(response);
}
}

View File

@@ -2,7 +2,6 @@ package fr.eni.demo.controller;
import fr.eni.demo.bll.FactureService;
import fr.eni.demo.bo.Facture;
import fr.eni.demo.bo.Location;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@@ -18,28 +17,29 @@ public class FactureController {
private final FactureService factureService;
@GetMapping()
// Toutes les factures
@GetMapping
public ResponseEntity<Map<String, Object>> getAllFactures() {
List<Facture> result = factureService.getAll();
Map<String, Object> response = new HashMap<>();
response.put("data", result);
response.put("status", 200);
return ResponseEntity.ok(response);
return buildResponse(result);
}
@GetMapping("/client/{id}")
// Factures par client
@GetMapping("/client/{clientId}")
public ResponseEntity<Map<String, Object>> getFactureByClient(@PathVariable Long clientId) {
Map<String, Object> response = new HashMap<>();
response.put("data", factureService.getByClient(clientId));
response.put("status", 200);
return ResponseEntity.ok(response);
return buildResponse(factureService.getByClient(clientId));
}
// Factures impayees
@GetMapping("/unpayed")
public ResponseEntity<Map<String, Object>> getUnpayedFactures() {
List<Facture> result = factureService.getUnpayed();
return buildResponse(result);
}
private ResponseEntity<Map<String, Object>> buildResponse(Object data) {
Map<String, Object> response = new HashMap<>();
response.put("data", result);
response.put("data", data);
response.put("status", 200);
return ResponseEntity.ok(response);
}

View File

@@ -2,13 +2,9 @@ package fr.eni.demo.controller;
import fr.eni.demo.bll.GameTypeService;
import fr.eni.demo.bo.GameType;
import fr.eni.demo.bo.Stock;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@@ -20,15 +16,18 @@ public class GameTypeController {
private final GameTypeService gameTypeService;
// Creer un type de jeu
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody GameType gameType) {
gameTypeService.add(gameType);
Map<String, Object> response = new HashMap<>();
response.put("message", "Game type create");
response.put("status", true);
response.put("data", new HashMap<>());
return buildResponse("Game type created", true, new HashMap<>());
}
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
Map<String, Object> response = new HashMap<>();
response.put("message", message);
response.put("status", status);
response.put("data", data);
return ResponseEntity.ok(response);
}
}

View File

@@ -1,15 +1,10 @@
package fr.eni.demo.controller;
import fr.eni.demo.bll.ClientService;
import fr.eni.demo.bll.LocationService;
import fr.eni.demo.bo.Client;
import fr.eni.demo.bo.Location;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@@ -25,12 +20,14 @@ public class LocationController {
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Location location) {
locationService.add(location);
Map<String, Object> response = new HashMap<>();
response.put("message", "Location added");
response.put("status", true);
response.put("data", new HashMap<>());
return buildResponse("Location added", true, new HashMap<>());
}
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
Map<String, Object> response = new HashMap<>();
response.put("message", message);
response.put("status", status);
response.put("data", data);
return ResponseEntity.ok(response);
}
}

View File

@@ -21,25 +21,22 @@ public class StockController {
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) {
stockService.add(stock);
Map<String, Object> response = new HashMap<>();
response.put("message", "Stock added");
response.put("status", true);
response.put("data", new HashMap<>());
return ResponseEntity.ok(response);
return buildResponse("Stock added", true, new HashMap<>());
}
// Chercher des clients par nom
// Chercher un stock par id
@GetMapping("/{id}")
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) {
Optional<Stock> result = stockService.findById(id);
Map<String, Object> response = new HashMap<>();
response.put("message", "List of Clients found");
response.put("status", true);
response.put("data", result);
return buildResponse("Stock found", true, result);
}
// Reponse formatée
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
Map<String, Object> response = new HashMap<>();
response.put("message", message);
response.put("status", status);
response.put("data", data);
return ResponseEntity.ok(response);
}
}