60 lines
1.2 KiB
Rust
60 lines
1.2 KiB
Rust
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<dyn LeaseServiceTrait + Send + Sync + 'static>);
|
|
|
|
impl LeaseService {
|
|
pub fn new(db: Db) -> Self {
|
|
Self(Arc::new(DefaultLeaseService::new(db)))
|
|
}
|
|
}
|
|
|
|
impl std::ops::Deref for LeaseService {
|
|
type Target = Arc<dyn LeaseServiceTrait + Send + Sync + 'static>;
|
|
|
|
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<String>;
|
|
}
|
|
|
|
#[async_trait]
|
|
impl LeaseServiceTrait for DefaultLeaseService {
|
|
async fn create_lease(&self) -> anyhow::Result<String> {
|
|
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())
|
|
}
|
|
}
|