Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.autoinvestor.application.RequestUserById;

import io.autoinvestor.application.UsersReadModel;
import io.autoinvestor.ui.user.UserResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class GetUserByIdCommandHandler {
private final UsersReadModel readModel;

public UserResponse handle (GetUserByIdQuery query) {
return readModel.getById(query.userId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package io.autoinvestor.application.RequestUserById;

public record GetUserByIdQuery(String userId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface UsersReadModel {
void add(UserReadModelDocument document);

UserResponse get(String email);
UserResponse getById(String userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@Document(collection = "users")
public record UserReadModelDocument(
@Id String userId,
String userId,
String email,
String firstName,
String lastName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public UserResponse get(String email) {
.map(UserResponseDocumentMapper::map)
.orElse(null);
}

@Override
public UserResponse getById(String userId) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,14 @@ public UserResponse get(String email) {
}
return UserResponseDocumentMapper.map(document);
}

@Override
public UserResponse getById(String userId) {
Query query = new Query(Criteria.where("userId").is(userId));
UserReadModelDocument document = mongoTemplate.findOne(query, UserReadModelDocument.class, "users");
if (document == null) {
return null;
}
return UserResponseDocumentMapper.map(document);
}
}
6 changes: 4 additions & 2 deletions src/main/java/io/autoinvestor/ui/user/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import io.autoinvestor.application.RegisterUserUseCase.RegisterUserCommand;
import io.autoinvestor.application.RegisterUserUseCase.RegisterUserCommandHandler;
import io.autoinvestor.application.RequestUserById.GetUserByIdCommandHandler;
import io.autoinvestor.application.RequestUserById.GetUserByIdQuery;
import io.autoinvestor.application.RequestUserUseCase.GetUserQuery;
import io.autoinvestor.application.RequestUserUseCase.GetUserQueryHandler;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,6 +19,7 @@ public class UserController {
private static final Integer DEFAULT_RISK_LEVEL = 1;

private final GetUserQueryHandler getUserCommandHandler;
private final GetUserByIdCommandHandler getUserByIdCommandHandler;
private final RegisterUserCommandHandler registerUserCommandHandler;

@GetMapping
Expand All @@ -25,8 +28,7 @@ public ResponseEntity<UserResponse> getUser(
@RequestParam(value = "email", required = false) String email
) {
if (userId != null) {
// TODO: return user by userId
return ResponseEntity.ok(new UserResponse(userId, null, null, null, null));
return ResponseEntity.ok(getUserByIdCommandHandler.handle(new GetUserByIdQuery(userId)));
} if (email != null) {
return ResponseEntity.ok(getUserCommandHandler.handle(new GetUserQuery(email)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.application.name=users
spring.cloud.gcp.pubsub.project-id=autoinvestor-tfm
spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.database=${MONGODB_DB}
spring.data.mongodb.uri=mongodb://localhost:27017
spring.data.mongodb.database=mydatabase