feat: impl cqrs_es::Query for CategoryView
This commit is contained in:
parent
e3fb0f775d
commit
e22e4ff5fb
1 changed files with 19 additions and 5 deletions
|
@ -3,7 +3,6 @@
|
||||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use cqrs_es::persist::GenericQuery;
|
|
||||||
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository};
|
||||||
use cqrs_es::{EventEnvelope, Query, View};
|
use cqrs_es::{EventEnvelope, Query, View};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -179,7 +178,22 @@ impl Query<Category> for SimpleLoggingQuery {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Our second query, this one will be handled with Postgres `GenericQuery`
|
#[async_trait]
|
||||||
// which will serialize and persist our view after it is updated. It also
|
impl Query<Category> for InventoryDBPostgresAdapter {
|
||||||
// provides a `load` method to deserialize the view on request.
|
async fn dispatch(&self, category_id: &str, events: &[EventEnvelope<Category>]) {
|
||||||
pub type CategoryQuery = GenericQuery<InventoryDBPostgresAdapter, CategoryView, Category>;
|
let res = self
|
||||||
|
.load_with_context(&category_id)
|
||||||
|
.await
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
Some((
|
||||||
|
CategoryView::default(),
|
||||||
|
ViewContext::new(category_id.into(), 0),
|
||||||
|
))
|
||||||
|
});
|
||||||
|
let (mut view, view_context): (CategoryView, ViewContext) = res.unwrap();
|
||||||
|
for event in events {
|
||||||
|
view.update(event);
|
||||||
|
}
|
||||||
|
self.update_view(view, view_context).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue