-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateUserCommandHandler.java
More file actions
53 lines (41 loc) · 1.75 KB
/
UpdateUserCommandHandler.java
File metadata and controls
53 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package io.autoinvestor.application.UpdateUserUseCase;
import io.autoinvestor.application.UserDTO;
import io.autoinvestor.application.UserNotFound;
import io.autoinvestor.application.UsersReadModel;
import io.autoinvestor.domain.EventStore;
import io.autoinvestor.domain.events.Event;
import io.autoinvestor.domain.events.EventPublisher;
import io.autoinvestor.domain.model.User;
import io.autoinvestor.domain.model.UserId;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class UpdateUserCommandHandler {
private final EventStore eventStore;
private final EventPublisher eventPublisher;
private final UsersReadModel readModel;
public void handle(UpdateUserCommand command) {
String userIdToUpdate =
String.valueOf(
readModel
.getById(command.userId())
.map(UserDTO::userId)
.orElseThrow(() -> UserNotFound.with(command.userId())));
User user = this.eventStore.get(UserId.from(userIdToUpdate));
user.update(userIdToUpdate, command.riskLevel());
List<Event<?>> events = user.getUncommittedEvents();
this.eventStore.save(user);
UserDTO dto =
new UserDTO(
user.getState().userId().value(),
user.getState().userEmail().value(),
user.getState().firstName().value(),
user.getState().lastName().value(),
command.riskLevel());
this.readModel.update(dto);
this.eventPublisher.publish(events);
user.markEventsAsCommitted();
}
}