search code barre

This commit is contained in:
mepiphana2023
2025-07-16 16:37:28 +02:00
parent b33d50835e
commit 3a37ddc252
6 changed files with 28 additions and 6 deletions

View File

@@ -4,5 +4,5 @@ import fr.eni.demo.bo.Location;
public interface LocationService {
void add(Location location);
void update(Location location);
Location findByCodeBarre(String codeBarre);
}

View File

@@ -4,6 +4,8 @@ import fr.eni.demo.bo.Location;
import fr.eni.demo.dal.LocationRepository;
import org.springframework.stereotype.Service;
import java.util.NoSuchElementException;
@Service
public class LocationServiceImpl implements LocationService {
private LocationRepository locationRepository;
@@ -16,12 +18,11 @@ public class LocationServiceImpl implements LocationService {
@Override
public void add(Location location) {
locationRepository.save(location);
stockService.isRent(location.getStock(), true);
}
@Override
public void update(Location location) {
locationRepository.save(location);
stockService.isRent(location.getStock(), false);
public Location findByCodeBarre(String codeBarre) {
return locationRepository.findByCodeBarre(codeBarre)
.orElseThrow(() -> new NoSuchElementException("Code-barre inconnu : " + codeBarre));
}
}

View File

@@ -19,6 +19,9 @@ public class Location {
@Id
private String id;
@Field(name = "CODE_BARRE")
private String codeBarre;
@Field(name = "LOCATION_START_DATE")
private Date startDate;

View File

@@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
@RestController
@RequestMapping("/api/location")
@@ -23,6 +24,17 @@ public class LocationController {
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) {
Map<String, Object> response = new HashMap<>();
response.put("message", message);

View File

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

View File

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