use std::sync::Arc; use axum::async_trait; use churn_capnp::CapnpPackExt; use churn_domain::Lease; use crate::db::Db; #[derive(Clone)] pub struct LeaseService(Arc); impl LeaseService { pub fn new(db: Db) -> Self { Self(Arc::new(DefaultLeaseService::new(db))) } } impl std::ops::Deref for LeaseService { type Target = Arc; fn deref(&self) -> &Self::Target { &self.0 } } struct DefaultLeaseService { db: Db, } impl DefaultLeaseService { pub fn new(db: Db) -> Self { Self { db } } } #[async_trait] pub trait LeaseServiceTrait { async fn create_lease(&self) -> anyhow::Result; } #[async_trait] impl LeaseServiceTrait for DefaultLeaseService { async fn create_lease(&self) -> anyhow::Result { let lease = uuid::Uuid::new_v4(); let id = uuid::Uuid::new_v4(); self.db .insert( "lease", &lease.to_string(), &Lease { id, lease }.serialize_capnp(), ) .await?; Ok(lease.to_string()) } }