merge stock service

This commit is contained in:
Olivier PARPAILLON
2025-07-16 12:04:58 +02:00
12 changed files with 186 additions and 139 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
public class AdresseServiceImpl implements AdresseService{
@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

@@ -4,6 +4,7 @@ import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import java.security.Key;

View File

@@ -20,6 +20,6 @@ public class User {
private String username;
@Field(name = "PASSWORD")
private String pasword;
private String password;
}

View File

@@ -7,6 +7,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
@@ -16,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);
return ResponseEntity.ok(response);
public ResponseEntity<Map<String, Object>> findById(@PathVariable String id) {
Adresse result = adresseService.findById(Long.valueOf(id));
return buildResponse("Adresse found", true, result);
}
@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);
// 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);
}
}

View File

@@ -31,7 +31,7 @@ public class AuthenticationController {
public ResponseEntity<?> login(@RequestBody User user) {
try {
Authentication auth = authManager.authenticate(
new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPasword())
new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword())
);
String token = jwtService.generateToken(user.getUsername());

View File

@@ -18,73 +18,51 @@ 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 ResponseEntity.ok(response);
return buildResponse("Address updated", true, new HashMap<>());
}
@DeleteMapping("/{id]")
@DeleteMapping("/{id}")
public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
clientService.delete(id);
return buildResponse("Address deleted", true, new HashMap<>());
}
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) {
Map<String, Object> response = new HashMap<>();
response.put("message", "Client deleted successfully");
response.put("status", 200);
response.put("data", new HashMap<>());
response.put("message", message);
response.put("status", status);
response.put("data", data);
return ResponseEntity.ok(response);
}

View File

@@ -17,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

@@ -4,10 +4,7 @@ import fr.eni.demo.bll.GameTypeService;
import fr.eni.demo.bo.GameType;
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;
@@ -19,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 ResponseEntity.ok(response);
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

@@ -4,10 +4,7 @@ import fr.eni.demo.bll.LocationService;
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;
@@ -23,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 ResponseEntity.ok(response);
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

@@ -19,6 +19,7 @@ public class StockController {
private final StockService stockService;
<<<<<<< HEAD
// Ajouter un stock
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) {
@@ -54,4 +55,28 @@ public class StockController {
response.put("data", result);
return ResponseEntity.ok(response);
}
=======
// Ajouter un stock
@PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) {
stockService.add(stock);
return buildResponse("Stock added", true, new HashMap<>());
}
// Chercher un stock par id
@GetMapping("/{id}")
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) {
Optional<Stock> result = stockService.findById(id);
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);
}
>>>>>>> c09dfc497575d616cee9f47afcc62667dbb08b0c
}

View File

@@ -4,10 +4,11 @@ import fr.eni.demo.bll.JwtService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
@@ -15,6 +16,7 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
@EnableWebSecurity
@@ -27,14 +29,18 @@ public class SecurityConfig {
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
public SecurityFilterChain filterChain(HttpSecurity http, UserDetailsService userDetailsService) throws Exception {
JwtAuthFilter jwtFilter = new JwtAuthFilter(jwtService, userDetailsService);
http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth
.requestMatchers("/api/auth").permitAll()
.requestMatchers("/user/**").hasAnyRole("ADMIN")
.anyRequest().denyAll()
.requestMatchers("/api/auth/login").permitAll()
.anyRequest().authenticated()
)
.formLogin(Customizer.withDefaults());
.sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}