Skip to content

Commit

Permalink
Refactor command and query execution to use "runtime" instead of "ser…
Browse files Browse the repository at this point in the history
…vices"
  • Loading branch information
Almaju committed Dec 19, 2023
1 parent d5758fe commit 883edb0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions application/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use framework::*;
use serde::Deserialize;

// Shared command logic
async fn pull_and_push<R>(services: &R, message: &TodoListMessage) -> Result<()>
async fn pull_and_push<R>(runtime: &R, message: &TodoListMessage) -> Result<()>
where
R: TodoListRepository + TodoListStore + Send + Sync,
{
let mut todolist = services.pull().await?;
let mut todolist = runtime.pull().await?;
let new_events = message.send(&todolist)?;
new_events.apply(&mut todolist);
let projection = TodoListProjection::project(&todolist);
services.push(&new_events).await?;
services.save(&projection).await?;
runtime.push(&new_events).await?;
runtime.save(&projection).await?;
Ok(())
}

Expand All @@ -30,8 +30,8 @@ impl<R> Command<R> for AddTaskCommand
where
R: TodoListRepository + TodoListStore + Send + Sync,
{
async fn execute(&self, services: &R) -> Result<()> {
pull_and_push(services, &TodoListMessage::AddTask(self.name.clone())).await?;
async fn execute(&self, runtime: &R) -> Result<()> {
pull_and_push(runtime, &TodoListMessage::AddTask(self.name.clone())).await?;
Ok(())
}
}
Expand All @@ -46,8 +46,8 @@ impl<R> Command<R> for RemoveTaskCommand
where
R: TodoListRepository + TodoListStore + Send + Sync,
{
async fn execute(&self, services: &R) -> Result<()> {
pull_and_push(services, &TodoListMessage::RemoveTask(self.index)).await?;
async fn execute(&self, runtime: &R) -> Result<()> {
pull_and_push(runtime, &TodoListMessage::RemoveTask(self.index)).await?;
Ok(())
}
}
Expand All @@ -62,8 +62,8 @@ impl<R> Command<R> for CompleteTaskCommand
where
R: TodoListRepository + TodoListStore + Send + Sync,
{
async fn execute(&self, services: &R) -> Result<()> {
pull_and_push(services, &TodoListMessage::CompleteTask(self.index)).await?;
async fn execute(&self, runtime: &R) -> Result<()> {
pull_and_push(runtime, &TodoListMessage::CompleteTask(self.index)).await?;
Ok(())
}
}
4 changes: 2 additions & 2 deletions application/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ where
{
type Output = TodoListProjection;

async fn execute(&self, services: &R) -> Result<Self::Output> {
services.fetch().await
async fn execute(&self, runtime: &R) -> Result<Self::Output> {
runtime.fetch().await
}
}
4 changes: 2 additions & 2 deletions crates/framework/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ pub trait Projection: Serialize + for<'de> Deserialize<'de> {

#[async_trait]
pub trait Command<R>: for<'de> Deserialize<'de> {
async fn execute(&self, services: &R) -> Result<()>;
async fn execute(&self, runtime: &R) -> Result<()>;
}

#[async_trait]
pub trait Query<R>: for<'de> Deserialize<'de> {
type Output: Projection;
async fn execute(&self, services: &R) -> Result<Self::Output>;
async fn execute(&self, runtime: &R) -> Result<Self::Output>;
}

0 comments on commit 883edb0

Please sign in to comment.