search code barre
This commit is contained in:
@@ -4,5 +4,5 @@ import fr.eni.demo.bo.Location;
|
|||||||
|
|
||||||
public interface LocationService {
|
public interface LocationService {
|
||||||
void add(Location location);
|
void add(Location location);
|
||||||
void update(Location location);
|
Location findByCodeBarre(String codeBarre);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import fr.eni.demo.bo.Location;
|
|||||||
import fr.eni.demo.dal.LocationRepository;
|
import fr.eni.demo.dal.LocationRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class LocationServiceImpl implements LocationService {
|
public class LocationServiceImpl implements LocationService {
|
||||||
private LocationRepository locationRepository;
|
private LocationRepository locationRepository;
|
||||||
@@ -16,12 +18,11 @@ public class LocationServiceImpl implements LocationService {
|
|||||||
@Override
|
@Override
|
||||||
public void add(Location location) {
|
public void add(Location location) {
|
||||||
locationRepository.save(location);
|
locationRepository.save(location);
|
||||||
stockService.isRent(location.getStock(), true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Location location) {
|
public Location findByCodeBarre(String codeBarre) {
|
||||||
locationRepository.save(location);
|
return locationRepository.findByCodeBarre(codeBarre)
|
||||||
stockService.isRent(location.getStock(), false);
|
.orElseThrow(() -> new NoSuchElementException("Code-barre inconnu : " + codeBarre));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,9 @@ 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;
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
@@ -4,6 +4,12 @@ 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, Integer> {
|
||||||
|
|
||||||
|
Optional<Location> findByCodeBarre(String codeBarre);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
|||||||
Reference in New Issue
Block a user