From cec58d78e968250e4c589899eab460d1132f6d01 Mon Sep 17 00:00:00 2001 From: rtkay123 Date: Mon, 30 Mar 2026 17:46:25 +0200 Subject: refactor: generic svcs --- lib/api-config/src/schema/mod.rs | 66 ++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'lib/api-config') diff --git a/lib/api-config/src/schema/mod.rs b/lib/api-config/src/schema/mod.rs index 7c65d49..f626e87 100644 --- a/lib/api-config/src/schema/mod.rs +++ b/lib/api-config/src/schema/mod.rs @@ -1,12 +1,12 @@ mod create; pub use create::CreateSchema; +use sqlx::PgPool; use tracing::debug; use async_trait::async_trait; use serde::{Deserialize, Serialize}; use std::fmt::Debug; use time::OffsetDateTime; -use warden_core::state::AppState; use crate::ConfigurationError; @@ -60,31 +60,31 @@ pub struct TransactionSchema { pub updated_at: OffsetDateTime, } +pub struct SchemaService { + pub database: PgPool, +} + #[async_trait] -pub trait SchemaDriver { +pub trait SchemaDriver: Send + Sync { async fn create_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, schema: &serde_json::Value, ) -> Result; - async fn delete_schema( - &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, - ) -> Result<(), ConfigurationError>; + async fn delete_schema(&self, kind: &str, version: &str) -> Result<(), ConfigurationError>; async fn get_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, ) -> Result, ConfigurationError>; async fn update_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, schema: &serde_json::Value, ) -> Result, ConfigurationError>; @@ -92,17 +92,17 @@ pub trait SchemaDriver { &self, limit: i64, first: Option, - after: Option + Send + Sync + Debug>, + after: Option<&str>, ) -> Result, ConfigurationError>; } #[async_trait] -impl SchemaDriver for AppState { +impl SchemaDriver for SchemaService { #[tracing::instrument(skip(self, schema))] async fn create_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, schema: &serde_json::Value, ) -> Result { debug!("creating transaction schema"); @@ -111,8 +111,8 @@ impl SchemaDriver for AppState { "insert into transaction_schema (schema_type, schema_version, schema) values ($1, $2, $3) returning * ", - kind.as_ref(), - version.as_ref(), + kind, + version, sqlx::types::Json(&schema) as _ ) .fetch_one(&self.database) @@ -123,14 +123,14 @@ impl SchemaDriver for AppState { #[tracing::instrument(skip(self))] async fn delete_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, ) -> Result<(), crate::ConfigurationError> { debug!("deleting transaction schema"); sqlx::query!( "delete from transaction_schema where schema_type = $1 and schema_version = $2", - kind.as_ref(), - version.as_ref(), + kind, + version, ) .execute(&self.database) .await?; @@ -140,8 +140,8 @@ impl SchemaDriver for AppState { #[tracing::instrument(skip(self))] async fn get_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, ) -> Result, crate::ConfigurationError> { debug!("getting transaction schema"); let result = sqlx::query_as!( @@ -149,8 +149,8 @@ impl SchemaDriver for AppState { "select * from transaction_schema where schema_type = $1 and schema_version = $2", - kind.as_ref(), - version.as_ref(), + kind, + version, ) .fetch_optional(&self.database) .await?; @@ -161,8 +161,8 @@ impl SchemaDriver for AppState { #[tracing::instrument(skip(self, schema))] async fn update_schema( &self, - kind: impl AsRef + Send + Sync + Debug, - version: impl AsRef + Send + Sync + Debug, + kind: &str, + version: &str, schema: &serde_json::Value, ) -> Result, crate::ConfigurationError> { debug!("updating transaction schema"); @@ -178,8 +178,8 @@ impl SchemaDriver for AppState { and schema_version = $2 returning * ", - kind.as_ref(), - version.as_ref(), + kind, + version, sqlx::types::Json(&schema) as _ ) .fetch_optional(&self.database) @@ -192,7 +192,7 @@ impl SchemaDriver for AppState { &self, limit: i64, first: Option, - after: Option + Send + Sync + Debug>, + after: Option<&str>, ) -> Result, ConfigurationError> { debug!("getting transaction schemas"); let limit = first.unwrap_or(limit); @@ -200,7 +200,7 @@ impl SchemaDriver for AppState { let mut last_version = String::default(); if let Some(s) = after { - let parts: Vec<&str> = s.as_ref().split(',').collect(); + let parts: Vec<&str> = s.split(',').collect(); if parts.len() == 2 { last_type = parts[0].to_string(); last_version = parts[1].to_string(); -- cgit v1.2.3