feat(ReadUser): añadida función readByUsername en repositorio, servicio y controlador

parent 2ef728d4
...@@ -8,6 +8,8 @@ public interface ReadUserUseCase { ...@@ -8,6 +8,8 @@ public interface ReadUserUseCase {
User readById(String id); User readById(String id);
User readByUsername(String username);
List<User> readAll(); List<User> readAll();
} }
...@@ -28,6 +28,12 @@ public class ReadUserUseCaseImpl implements ReadUserUseCase { ...@@ -28,6 +28,12 @@ public class ReadUserUseCaseImpl implements ReadUserUseCase {
} }
@Override @Override
public User readByUsername(String username) {
return repository.readByUsername(username).map(mapper::mapDocument)
.orElseThrow(() -> new EntityNotFoundException("Usuario con username " + username + " no encontrado"));
}
@Override
public List<User> readAll() { public List<User> readAll() {
return repository.readAll().stream() return repository.readAll().stream()
.map(mapper::mapDocument) .map(mapper::mapDocument)
......
...@@ -9,6 +9,8 @@ public interface ReadUserRepository { ...@@ -9,6 +9,8 @@ public interface ReadUserRepository {
Optional<UserDocument> readById(String id); Optional<UserDocument> readById(String id);
Optional<UserDocument> readByUsername(String username);
List<UserDocument> readAll(); List<UserDocument> readAll();
} }
...@@ -22,11 +22,16 @@ public class ReadUserController { ...@@ -22,11 +22,16 @@ public class ReadUserController {
private final UserMapper mapper = Mappers.getMapper(UserMapper.class); private final UserMapper mapper = Mappers.getMapper(UserMapper.class);
@GetMapping("/{id}") @GetMapping("/id/{id}")
public ResponseEntity<UserOutputDto> readById(@PathVariable String id) { public ResponseEntity<UserOutputDto> readById(@PathVariable String id) {
return ResponseEntity.ok().body(mapper.map(service.readById(id))); return ResponseEntity.ok().body(mapper.map(service.readById(id)));
} }
@GetMapping("/{username}")
public ResponseEntity<UserOutputDto> readByUsername(@PathVariable String username) {
return ResponseEntity.ok().body(mapper.map(service.readByUsername(username)));
}
@GetMapping @GetMapping
public ResponseEntity<List<UserOutputDto>> readAll() { public ResponseEntity<List<UserOutputDto>> readAll() {
if (service.readAll().isEmpty()) if (service.readAll().isEmpty())
......
...@@ -21,6 +21,11 @@ public class ReadUserRepositoryImpl implements ReadUserRepository { ...@@ -21,6 +21,11 @@ public class ReadUserRepositoryImpl implements ReadUserRepository {
} }
@Override @Override
public Optional<UserDocument> readByUsername(String username) {
return repositoryJpa.findByUsername(username);
}
@Override
public List<UserDocument> readAll() { public List<UserDocument> readAll() {
return repositoryJpa.findAll(); return repositoryJpa.findAll();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment