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 fr.eni.demo.bo.Adresse;
import java.util.List;
public interface AdresseService { public interface AdresseService {
Adresse add(Adresse adresse); Adresse add(Adresse adresse);
Adresse findById(Long id); Adresse findById(Long id);
List<Adresse> findAll();
Adresse update(Long id, Adresse adresse);
void delete(Long id);
Adresse findAdresseByClientId(Long clientId); 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.bo.Client;
import fr.eni.demo.dal.AdresseRepository; import fr.eni.demo.dal.AdresseRepository;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import org.springframework.beans.factory.annotation.Autowired; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
@RequiredArgsConstructor
public class AdresseServiceImpl implements AdresseService { public class AdresseServiceImpl implements AdresseService {
private AdresseRepository adresseRepo; private final AdresseRepository adresseRepo;
@Autowired private final ClientService clientService;
private ClientService clientService;
public AdresseServiceImpl(AdresseRepository adresseRepo) {
this.adresseRepo = adresseRepo;
}
@Override @Override
public Adresse add(Adresse adresse) { 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) { if (adresse == null) {
throw new IllegalArgumentException("Adresse is null"); throw new IllegalArgumentException("Adresse is null");
} }
@@ -32,24 +66,5 @@ public class AdresseServiceImpl implements AdresseService{
if (adresse.getVille() == null || adresse.getVille().isBlank()) { if (adresse.getVille() == null || adresse.getVille().isBlank()) {
throw new IllegalArgumentException("Ville is null or empty"); 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.Jwts;
import io.jsonwebtoken.SignatureAlgorithm; import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys; import io.jsonwebtoken.security.Keys;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.security.Key; import java.security.Key;

View File

@@ -20,6 +20,6 @@ public class User {
private String username; private String username;
@Field(name = "PASSWORD") @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 org.springframework.web.bind.annotation.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
@RestController @RestController
@@ -16,37 +17,53 @@ public class AddressController {
private final AdresseService adresseService; private final AdresseService adresseService;
// Creer nouvelle adresse
@PostMapping @PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Adresse adresse) { public ResponseEntity<Map<String, Object>> create(@RequestBody Adresse adresse) {
adresseService.add(adresse); Adresse saved = adresseService.add(adresse);
Map<String, Object> response = new HashMap<>(); return buildResponse("Adresse added", true, saved);
response.put("message", "Adresse added");
response.put("status", true);
response.put("data", new HashMap<>());
return ResponseEntity.ok(response);
} }
// Recuperer adresse par id
@GetMapping("/{id}") @GetMapping("/{id}")
public ResponseEntity<Map<String, Object>> findById(@PathVariable Long id) { public ResponseEntity<Map<String, Object>> findById(@PathVariable String id) {
Adresse result = adresseService.findById(id); Adresse result = adresseService.findById(Long.valueOf(id));
Map<String, Object> response = new HashMap<>(); return buildResponse("Adresse found", true, result);
response.put("message", "Adresse find"); }
response.put("status", true);
response.put("data", 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); 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

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

View File

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

View File

@@ -17,28 +17,29 @@ public class FactureController {
private final FactureService factureService; private final FactureService factureService;
@GetMapping() // Toutes les factures
@GetMapping
public ResponseEntity<Map<String, Object>> getAllFactures() { public ResponseEntity<Map<String, Object>> getAllFactures() {
List<Facture> result = factureService.getAll(); List<Facture> result = factureService.getAll();
Map<String, Object> response = new HashMap<>(); return buildResponse(result);
response.put("data", result);
response.put("status", 200);
return ResponseEntity.ok(response);
} }
@GetMapping("/client/{id}") // Factures par client
@GetMapping("/client/{clientId}")
public ResponseEntity<Map<String, Object>> getFactureByClient(@PathVariable Long clientId) { public ResponseEntity<Map<String, Object>> getFactureByClient(@PathVariable Long clientId) {
Map<String, Object> response = new HashMap<>(); return buildResponse(factureService.getByClient(clientId));
response.put("data", factureService.getByClient(clientId));
response.put("status", 200);
return ResponseEntity.ok(response);
} }
// Factures impayees
@GetMapping("/unpayed") @GetMapping("/unpayed")
public ResponseEntity<Map<String, Object>> getUnpayedFactures() { public ResponseEntity<Map<String, Object>> getUnpayedFactures() {
List<Facture> result = factureService.getUnpayed(); List<Facture> result = factureService.getUnpayed();
return buildResponse(result);
}
private ResponseEntity<Map<String, Object>> buildResponse(Object data) {
Map<String, Object> response = new HashMap<>(); Map<String, Object> response = new HashMap<>();
response.put("data", result); response.put("data", data);
response.put("status", 200); response.put("status", 200);
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }

View File

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

View File

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

View File

@@ -19,6 +19,7 @@ public class StockController {
private final StockService stockService; private final StockService stockService;
<<<<<<< HEAD
// Ajouter un stock // Ajouter un stock
@PostMapping @PostMapping
public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) { public ResponseEntity<Map<String, Object>> create(@RequestBody Stock stock) {
@@ -54,4 +55,28 @@ public class StockController {
response.put("data", result); response.put("data", result);
return ResponseEntity.ok(response); 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager; 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.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 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.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.User;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService; 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.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager; import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.SecurityFilterChain; import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration @Configuration
@EnableWebSecurity @EnableWebSecurity
@@ -27,14 +29,18 @@ public class SecurityConfig {
} }
@Bean @Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { public SecurityFilterChain filterChain(HttpSecurity http, UserDetailsService userDetailsService) throws Exception {
JwtAuthFilter jwtFilter = new JwtAuthFilter(jwtService, userDetailsService);
http http
.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests(auth -> auth .authorizeHttpRequests(auth -> auth
.requestMatchers("/api/auth").permitAll() .requestMatchers("/api/auth/login").permitAll()
.requestMatchers("/user/**").hasAnyRole("ADMIN") .anyRequest().authenticated()
.anyRequest().denyAll()
) )
.formLogin(Customizer.withDefaults()); .sessionManagement(sess -> sess.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);
return http.build(); return http.build();
} }