This commit is contained in:
Olivier PARPAILLON
2025-07-17 11:28:39 +02:00
17 changed files with 183 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
package fr.eni.demo.bll; package fr.eni.demo.bll;
import fr.eni.demo.bo.Facture; import fr.eni.demo.bo.Facture;
import fr.eni.demo.bo.Location;
import java.util.List; import java.util.List;
@@ -8,4 +9,5 @@ public interface FactureService {
List<Facture> getByClient(Long clientId); List<Facture> getByClient(Long clientId);
List<Facture> getAll(); List<Facture> getAll();
List<Facture> getUnpayed(); List<Facture> getUnpayed();
void add(Facture facture);
} }

View File

@@ -1,6 +1,7 @@
package fr.eni.demo.bll; package fr.eni.demo.bll;
import fr.eni.demo.bo.Facture; import fr.eni.demo.bo.Facture;
import fr.eni.demo.bo.Location;
import fr.eni.demo.dal.FactureRepository; import fr.eni.demo.dal.FactureRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -35,4 +36,10 @@ public class FactureServiceImpl implements FactureService {
public List<Facture> getUnpayed() { public List<Facture> getUnpayed() {
return factureRepo.findByDatePayEmpty(); return factureRepo.findByDatePayEmpty();
} }
@Override
public void add(Facture facture) {
factureRepo.save(facture);
}
} }

View File

@@ -3,6 +3,8 @@ package fr.eni.demo.bll;
import fr.eni.demo.bo.Location; import fr.eni.demo.bo.Location;
public interface LocationService { public interface LocationService {
Location findById(Long id);
void add(Location location); void add(Location location);
void update(Location location); void updateDateEnd(String id, Location location);
Location findByCodeBarre(String codeBarre);
} }

View File

@@ -2,17 +2,28 @@ package fr.eni.demo.bll;
import fr.eni.demo.bo.Location; import fr.eni.demo.bo.Location;
import fr.eni.demo.dal.LocationRepository; import fr.eni.demo.dal.LocationRepository;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.NoSuchElementException;
@Service @Service
public class LocationServiceImpl implements LocationService { public class LocationServiceImpl implements LocationService {
private LocationRepository locationRepository;
private final LocationRepository locationRepository;
private StockService stockService; private StockService stockService;
public LocationServiceImpl(LocationRepository locationRepository) { public LocationServiceImpl(LocationRepository locationRepository) {
this.locationRepository = locationRepository; this.locationRepository = locationRepository;
} }
@Override
public Location findById(Long id) {
return locationRepository.findById(id)
.orElseThrow(() -> new EntityNotFoundException("Location non trouvée"));
}
@Override @Override
public void add(Location location) { public void add(Location location) {
locationRepository.save(location); locationRepository.save(location);
@@ -20,8 +31,17 @@ public class LocationServiceImpl implements LocationService {
} }
@Override @Override
public void update(Location location) { public void updateDateEnd(String id, Location location) {
Location existing = findById(Long.valueOf(id));
location.setId(existing.getId());
location.setEndDate(new Date());
locationRepository.save(location); locationRepository.save(location);
stockService.isRent(location.getStock(), false);
} }
@Override
public Location findByCodeBarre(String codeBarre) {
return locationRepository.findByCodeBarre(codeBarre)
.orElseThrow(() -> new NoSuchElementException("Code-barre inconnu : " + codeBarre));
}
} }

View File

@@ -0,0 +1,11 @@
package fr.eni.demo.bll;
import fr.eni.demo.bo.User;
import java.util.Optional;
import java.util.List;
public interface UserService {
void addUser(User user);
Optional<User> findByUsername(String username);
List<User> findAll();
}

View File

@@ -0,0 +1,39 @@
package fr.eni.demo.bll;
import fr.eni.demo.bo.User;
import fr.eni.demo.dal.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Autowired
public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@Override
public void addUser(User user) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
userRepository.save(user);
}
@Override
public Optional<User> findByUsername(String username) {
return userRepository.findByUsername(username);
}
@Override
public List<User> findAll() {
return userRepository.findAll();
}
}

View File

@@ -28,15 +28,12 @@ public class Client {
@Field(name = "EMAIL") @Field(name = "EMAIL")
private String email; private String email;
@DBRef
@Field(name = "ADRESSE") @Field(name = "ADRESSE")
private Adresse adresse; private Adresse adresse;
@DBRef
@Field(name = "LOCATIONS") @Field(name = "LOCATIONS")
private List<Location> locations; private List<Location> locations;
@DBRef
@Field(name = "FACTURES") @Field(name = "FACTURES")
private List<Facture> factures; private List<Facture> factures;
} }

View File

@@ -27,7 +27,6 @@ public class Facture {
@Field(name = "DATEPAY") @Field(name = "DATEPAY")
private Date datePay; private Date datePay;
@DBRef
@Field(name = "CLIENTS") @Field(name = "CLIENTS")
private Client client; private Client client;
} }

View File

@@ -19,17 +19,18 @@ public class Location {
@Id @Id
private String id; private String id;
@Field(name = "CODE_BARRE")
private String codeBarre;
@Field(name = "LOCATION_START_DATE") @Field(name = "LOCATION_START_DATE")
private Date startDate; private Date startDate;
@Field(name = "LOCATION_END_DATE") @Field(name = "LOCATION_END_DATE")
private Date endDate; private Date endDate;
@DBRef
@Field(name = "CLIENT") @Field(name = "CLIENT")
private Client client; private Client client;
@DBRef
@Field(name = "STOCK") @Field(name = "STOCK")
private Stock stock; private Stock stock;
} }

View File

@@ -34,11 +34,9 @@ public class Stock {
@Field(name = "GAME_IS_RENT") @Field(name = "GAME_IS_RENT")
private Boolean isRent; private Boolean isRent;
@DBRef
@Field(name = "GAME_TYPE") @Field(name = "GAME_TYPE")
private List<GameType> gameType; private List<GameType> gameType;
@DBRef
@Field(name = "LOCATIONS") @Field(name = "LOCATIONS")
private List<Location> locations; private List<Location> locations;
} }

View File

@@ -58,6 +58,7 @@ public class ClientController {
clientService.delete(id); clientService.delete(id);
return buildResponse("Address deleted", true, new HashMap<>()); return buildResponse("Address deleted", true, new HashMap<>());
} }
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) { 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", message); response.put("message", message);

View File

@@ -1,11 +1,14 @@
package fr.eni.demo.controller; package fr.eni.demo.controller;
import fr.eni.demo.bll.FactureService; import fr.eni.demo.bll.FactureService;
import fr.eni.demo.bll.LocationService;
import fr.eni.demo.bo.Facture; import fr.eni.demo.bo.Facture;
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.*; import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -16,31 +19,51 @@ import java.util.Map;
public class FactureController { public class FactureController {
private final FactureService factureService; private final FactureService factureService;
private final LocationService locationService;
// Toutes les factures // Toutes les factures
@GetMapping @GetMapping
public ResponseEntity<Map<String, Object>> getAllFactures() { public ResponseEntity<Map<String, Object>> getAllFactures() {
List<Facture> result = factureService.getAll(); List<Facture> result = factureService.getAll();
return buildResponse(result); return buildResponse("List all order", true, result);
} }
// Factures par client // Factures par client
@GetMapping("/client/{clientId}") @GetMapping("/client/{clientId}")
public ResponseEntity<Map<String, Object>> getFactureByClient(@PathVariable Long clientId) { public ResponseEntity<Map<String, Object>> getFactureByClient(@PathVariable Long clientId) {
return buildResponse(factureService.getByClient(clientId)); List<Facture> result = factureService.getByClient(clientId);
return buildResponse("List order customer", true, result);
} }
// Factures impayees // 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); return buildResponse("List unpayed older", true, result);
} }
private ResponseEntity<Map<String, Object>> buildResponse(Object data) { // Génère une facture
@PostMapping
public ResponseEntity<Map<String, Object>> setFacture(@RequestBody List<Location> locations) {
double price = 0;
for(Location location : locations){
price += location.getStock().getDailyPrice();
locationService.updateDateEnd(location.getId(), location);
}
Facture facture = new Facture();
facture.setClient(locations.get(0).getClient());
facture.setDatePay(new Date());
facture.setPrice(price);
factureService.add(facture);
return buildResponse("Older created", 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", message);
response.put("status", status);
response.put("data", data); response.put("data", data);
response.put("status", 200);
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }
} }

View File

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
@RestController @RestController
@RequestMapping("/api/location") @RequestMapping("/api/location")
@@ -23,6 +24,17 @@ public class LocationController {
return buildResponse("Location added", true, new HashMap<>()); return buildResponse("Location added", true, new HashMap<>());
} }
// Chercher location depuis code barre
@GetMapping("/codeBarre/{codeBarre}")
public ResponseEntity<Map<String, Object>> findByCodeBarre(@PathVariable String codeBarre) {
try {
Location location = locationService.findByCodeBarre(codeBarre);
return buildResponse("Location trouvée", true, location);
} catch (NoSuchElementException e) {
return buildResponse(e.getMessage(), false, new HashMap<>());
}
}
private ResponseEntity<Map<String, Object>> buildResponse(String message, boolean status, Object data) { 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", message); response.put("message", message);

View File

@@ -0,0 +1,35 @@
package fr.eni.demo.controller;
import fr.eni.demo.bll.UserService;
import fr.eni.demo.bo.User;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService;
@PostMapping
public ResponseEntity<User> createUser(@RequestBody User user) {
userService.addUser(user);
return ResponseEntity.ok(user);
}
@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
return ResponseEntity.ok(userService.findAll());
}
@GetMapping("/{username}")
public ResponseEntity<User> getUserByUsername(@PathVariable String username) {
return userService.findByUsername(username)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
}

View File

@@ -4,6 +4,11 @@ import fr.eni.demo.bo.Location;
import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository @Repository
public interface LocationRepository extends MongoRepository<Location, Integer> { public interface LocationRepository extends MongoRepository<Location, Long> {
Optional<Location> findByCodeBarre(String codeBarre);
} }

View File

@@ -0,0 +1,12 @@
package fr.eni.demo.dal;
import fr.eni.demo.bo.User;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface UserRepository extends MongoRepository<User, String> {
Optional<User> findByUsername(String username);
}

View File

@@ -66,7 +66,7 @@ public class SecurityConfig {
UserDetails user = User.builder() UserDetails user = User.builder()
.username("user") .username("user")
.password(encoder.encode("password")) .password(encoder.encode("password"))
.roles("USER") .roles("EMPLOYE")
.build(); .build();
return new InMemoryUserDetailsManager(user); return new InMemoryUserDetailsManager(user);