# Conflicts:
#	src/main/java/fr/eni/demo/bll/LocationService.java
#	src/main/java/fr/eni/demo/bll/LocationServiceImpl.java
#	src/main/java/fr/eni/demo/dal/LocationRepository.java
This commit is contained in:
jleroy2023
2025-07-16 16:41:05 +02:00
8 changed files with 117 additions and 1 deletions

View File

@@ -7,4 +7,5 @@ public interface LocationService {
void add(Location location);
void update(Location location);
void updateDateEnd(String id, Location location);
Location findByCodeBarre(String 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

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

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

@@ -5,5 +5,8 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface LocationRepository extends MongoRepository<Location, Long> {
public interface LocationRepository extends MongoRepository<Location, Integer> {
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);
}