feat: move core services to services directory

This commit is contained in:
Andrew Phillips
2025-08-25 18:22:17 -03:00
committed by Andrew Phillips (aider)
parent 8cc0cfc606
commit 321e00171e
9 changed files with 201 additions and 533 deletions

View File

@@ -0,0 +1,116 @@
use crate::services::error::CoreError;
use crate::services::item_service::ItemService;
use crate::services::types::{ItemWithContent, ItemWithMeta};
use rusqlite::Connection;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex;
/// An asynchronous wrapper around the `ItemService` for use in async contexts like the web server.
/// It uses `tokio::task::spawn_blocking` to run synchronous database and filesystem operations
/// on a dedicated thread pool, preventing them from blocking the async runtime.
#[allow(dead_code)]
pub struct AsyncItemService {
data_path: PathBuf,
db: Arc<Mutex<Connection>>,
}
#[allow(dead_code)]
impl AsyncItemService {
pub fn new(data_path: PathBuf, db: Arc<Mutex<Connection>>) -> Self {
Self { data_path, db }
}
pub async fn get_item(&self, id: i64) -> Result<ItemWithMeta, CoreError> {
let data_path = self.data_path.clone();
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
let conn = db.blocking_lock();
let item_service = ItemService::new(data_path);
item_service.get_item(&conn, id)
})
.await
.unwrap() // Propagate panics from spawn_blocking
}
pub async fn get_item_content(&self, id: i64) -> Result<ItemWithContent, CoreError> {
let data_path = self.data_path.clone();
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
let conn = db.blocking_lock();
let item_service = ItemService::new(data_path);
item_service.get_item_content(&conn, id)
})
.await
.unwrap()
}
pub async fn find_item(
&self,
ids: Vec<i64>,
tags: Vec<String>,
meta: HashMap<String, String>,
) -> Result<ItemWithMeta, CoreError> {
let data_path = self.data_path.clone();
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
let conn = db.blocking_lock();
let item_service = ItemService::new(data_path);
item_service.find_item(&conn, &ids, &tags, &meta)
})
.await
.unwrap()
}
pub async fn list_items(
&self,
tags: Vec<String>,
meta: HashMap<String, String>,
) -> Result<Vec<ItemWithMeta>, CoreError> {
let data_path = self.data_path.clone();
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
let conn = db.blocking_lock();
let item_service = ItemService::new(data_path);
item_service.list_items(&conn, &tags, &meta)
})
.await
.unwrap()
}
pub async fn delete_item(&self, id: i64) -> Result<(), CoreError> {
let data_path = self.data_path.clone();
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
let mut conn = db.blocking_lock();
let item_service = ItemService::new(data_path);
item_service.delete_item(&mut conn, id)
})
.await
.unwrap()
}
pub async fn save_item_from_mcp(
&self,
content: Vec<u8>,
tags: Vec<String>,
metadata: HashMap<String, String>,
) -> Result<ItemWithMeta, CoreError> {
let data_path = self.data_path.clone();
let db = self.db.clone();
tokio::task::spawn_blocking(move || {
let mut conn = db.blocking_lock();
let item_service = ItemService::new(data_path);
item_service.save_item_from_mcp(&content, &tags, &metadata, &mut conn)
})
.await
.unwrap()
}
}