-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from cloudcreate-dk/release_0_40_19
Release 0.40.19
- Loading branch information
Showing
36 changed files
with
1,107 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...cloudcreate/essentials/spring/examples/postgresql/cqrs/task/TaskEventStoreRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task; | ||
|
||
import dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.StatefulAggregateRepository; | ||
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.ConfigurableEventStore; | ||
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType; | ||
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.persistence.table_per_aggregate_type.SeparateTablePerAggregateEventStreamConfiguration; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands.CreateTask; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.Task; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.TaskId; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.events.TaskEvent; | ||
import lombok.NonNull; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
import static dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.StatefulAggregateInstanceFactory.reflectionBasedAggregateRootFactory; | ||
|
||
@Component | ||
public class TaskEventStoreRepository { | ||
|
||
public static final AggregateType AGGREGATE_TYPE = AggregateType.of("Task"); | ||
private final ConfigurableEventStore<SeparateTablePerAggregateEventStreamConfiguration> eventStore; | ||
private final StatefulAggregateRepository<TaskId, TaskEvent, Task> repository; | ||
|
||
public TaskEventStoreRepository(@NonNull ConfigurableEventStore<SeparateTablePerAggregateEventStreamConfiguration> eventStore) { | ||
this.eventStore = eventStore; | ||
repository = StatefulAggregateRepository.from(eventStore, | ||
AGGREGATE_TYPE, | ||
reflectionBasedAggregateRootFactory(), | ||
Task.class); | ||
} | ||
|
||
public Optional<Task> findTask(@NonNull TaskId taskId) { | ||
return repository.tryLoad(taskId); | ||
} | ||
|
||
public Task getTask(@NonNull TaskId taskId) { | ||
return repository.load(taskId); | ||
} | ||
|
||
public Task createTask(@NonNull TaskId taskId, CreateTask cmd) { | ||
var task = new Task(taskId, cmd); | ||
return repository.save(task); | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...in/java/dk/cloudcreate/essentials/spring/examples/postgresql/cqrs/task/TaskProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task; | ||
|
||
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.eventstream.AggregateType; | ||
import dk.cloudcreate.essentials.components.eventsourced.eventstore.postgresql.processor.*; | ||
import dk.cloudcreate.essentials.components.foundation.messaging.MessageHandler; | ||
import dk.cloudcreate.essentials.reactive.command.CmdHandler; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands.*; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.Task; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.events.TaskCreated; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Objects.nonNull; | ||
|
||
@Slf4j | ||
@Service | ||
public class TaskProcessor extends InTransactionEventProcessor { | ||
|
||
private final TaskEventStoreRepository taskEventStoreRepository; | ||
|
||
protected TaskProcessor(TaskEventStoreRepository taskEventStoreRepository, | ||
EventProcessorDependencies eventProcessorDependencies) { | ||
super(eventProcessorDependencies, true); | ||
this.taskEventStoreRepository = taskEventStoreRepository; | ||
} | ||
|
||
public TaskEventStoreRepository getTaskEventStoreRepository() { | ||
return taskEventStoreRepository; | ||
} | ||
|
||
@Override | ||
public String getProcessorName() { | ||
return "TaskProcessor"; | ||
} | ||
|
||
@Override | ||
protected List<AggregateType> reactsToEventsRelatedToAggregateTypes() { | ||
return List.of(TaskEventStoreRepository.AGGREGATE_TYPE); | ||
} | ||
|
||
@CmdHandler | ||
public void handle(CreateTask cmd) { | ||
log.info("Creating task with command '{}'", cmd); | ||
taskEventStoreRepository.createTask(cmd.taskId(), cmd); | ||
|
||
} | ||
|
||
@CmdHandler | ||
public void handle(AddComment cmd) { | ||
log.info("Adding comment '{}'", cmd); | ||
Task task = taskEventStoreRepository.findTask(cmd.taskId()).orElseThrow(); | ||
task.addComment(cmd); | ||
} | ||
|
||
@MessageHandler | ||
void handle(TaskCreated event) { | ||
if (nonNull(event.getComment())) { | ||
log.info("Task '{}' contains comment adding comment command", event); | ||
commandBus.send(new AddComment(event.getTaskId(), event.getComment(), event.getCreatedAt())); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...a/dk/cloudcreate/essentials/spring/examples/postgresql/cqrs/task/commands/AddComment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands; | ||
|
||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.TaskId; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record AddComment(TaskId taskId, String content, LocalDateTime createdAt) implements TaskCommand { | ||
} |
22 changes: 22 additions & 0 deletions
22
...a/dk/cloudcreate/essentials/spring/examples/postgresql/cqrs/task/commands/CreateTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands; | ||
|
||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.TaskId; | ||
|
||
public record CreateTask(TaskId taskId, String comment) implements TaskCommand { | ||
} |
20 changes: 20 additions & 0 deletions
20
.../dk/cloudcreate/essentials/spring/examples/postgresql/cqrs/task/commands/TaskCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands; | ||
|
||
public interface TaskCommand { | ||
} |
22 changes: 22 additions & 0 deletions
22
...n/java/dk/cloudcreate/essentials/spring/examples/postgresql/cqrs/task/domain/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record Comment(TaskId taskId, String content, LocalDateTime createdAt) { | ||
} |
69 changes: 69 additions & 0 deletions
69
...main/java/dk/cloudcreate/essentials/spring/examples/postgresql/cqrs/task/domain/Task.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2021-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain; | ||
|
||
import dk.cloudcreate.essentials.components.eventsourced.aggregates.EventHandler; | ||
import dk.cloudcreate.essentials.components.eventsourced.aggregates.stateful.modern.AggregateRoot; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands.AddComment; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.commands.CreateTask; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.events.CommentAdded; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.events.TaskCreated; | ||
import dk.cloudcreate.essentials.spring.examples.postgresql.cqrs.task.domain.events.TaskEvent; | ||
import lombok.ToString; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
@ToString | ||
public class Task extends AggregateRoot<TaskId, TaskEvent, Task> { | ||
|
||
private Set<Comment> comments; | ||
|
||
public Task(TaskId aggregateId) { | ||
super(aggregateId); | ||
} | ||
|
||
public Task(TaskId aggregateId, CreateTask cmd) { | ||
super(aggregateId); | ||
apply(new TaskCreated(aggregateId, | ||
cmd.comment(), | ||
LocalDateTime.now() | ||
)); | ||
} | ||
|
||
public void addComment(AddComment cmd) { | ||
Comment comment = new Comment(cmd.taskId(), cmd.content(), cmd.createdAt()); | ||
if (!comments.contains(comment)) { | ||
apply(new CommentAdded(cmd.taskId(), cmd.content(), cmd.createdAt())); | ||
} | ||
} | ||
|
||
public Set<Comment> getComments() { | ||
return comments; | ||
} | ||
|
||
@EventHandler | ||
private void on(TaskCreated event) { | ||
comments = new HashSet<>(); | ||
} | ||
|
||
@EventHandler | ||
private void on(CommentAdded event) { | ||
comments.add(new Comment(event.getTaskId(), event.getContent(), event.getCreatedAt())); | ||
} | ||
} |
Oops, something went wrong.