From e22e4ff5fb75e60744ae2d4385ac48cfbd56f0d2 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Mon, 15 Jul 2024 17:58:01 +0530 Subject: [PATCH] feat: impl cqrs_es::Query for CategoryView --- .../output/db/postgres/category_view.rs | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/inventory/adapters/output/db/postgres/category_view.rs b/src/inventory/adapters/output/db/postgres/category_view.rs index 154fb73..95cc996 100644 --- a/src/inventory/adapters/output/db/postgres/category_view.rs +++ b/src/inventory/adapters/output/db/postgres/category_view.rs @@ -3,7 +3,6 @@ // SPDX-License-Identifier: AGPL-3.0-or-later use async_trait::async_trait; -use cqrs_es::persist::GenericQuery; use cqrs_es::persist::{PersistenceError, ViewContext, ViewRepository}; use cqrs_es::{EventEnvelope, Query, View}; use serde::{Deserialize, Serialize}; @@ -179,7 +178,22 @@ impl Query for SimpleLoggingQuery { } } -// Our second query, this one will be handled with Postgres `GenericQuery` -// which will serialize and persist our view after it is updated. It also -// provides a `load` method to deserialize the view on request. -pub type CategoryQuery = GenericQuery; +#[async_trait] +impl Query for InventoryDBPostgresAdapter { + async fn dispatch(&self, category_id: &str, events: &[EventEnvelope]) { + 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(); + } +}