@@ -2,11 +2,21 @@ use std::sync::Arc;
|
||||
|
||||
use axum::async_trait;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
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>;
|
||||
|
||||
@@ -15,15 +25,14 @@ impl std::ops::Deref for LeaseService {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for LeaseService {
|
||||
fn default() -> Self {
|
||||
Self(Arc::new(DefaultLeaseService::default()))
|
||||
}
|
||||
struct DefaultLeaseService {
|
||||
db: Db,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct DefaultLeaseService {
|
||||
leases: Arc<Mutex<Vec<String>>>,
|
||||
impl DefaultLeaseService {
|
||||
pub fn new(db: Db) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
@@ -34,12 +43,17 @@ pub trait LeaseServiceTrait {
|
||||
#[async_trait]
|
||||
impl LeaseServiceTrait for DefaultLeaseService {
|
||||
async fn create_lease(&self) -> anyhow::Result<String> {
|
||||
let mut leases = self.leases.lock().await;
|
||||
let lease = uuid::Uuid::new_v4();
|
||||
let id = uuid::Uuid::new_v4();
|
||||
|
||||
let lease = uuid::Uuid::new_v4().to_string();
|
||||
self.db
|
||||
.insert(
|
||||
"lease",
|
||||
&lease.to_string(),
|
||||
&Lease { id, lease }.serialize_capnp(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
leases.push(lease.clone());
|
||||
|
||||
Ok(lease)
|
||||
Ok(lease.to_string())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user