ood_persistence/examples/web/src/app/list/controller.rs

42 lines
1.0 KiB
Rust

use super::service::{create_postgres_list_service, ListService};
use super::{List, ListId};
use crate::db::persistence::PersistencePool;
use crate::db::persistence::PostgresPersistence;
use crate::error::ApiResult;
pub fn create_postgres_list_controller(
persistence: PostgresPersistence,
) -> ListController<PostgresPersistence> {
ListController {
list_service: create_postgres_list_service(persistence),
}
}
#[derive(Debug, Deserialize)]
pub struct AddListInput {
name: String,
}
pub struct ListController<P>
where
P: PersistencePool,
{
list_service: ListService<P>,
}
impl<P> ListController<P>
where
P: PersistencePool,
{
pub async fn get_list_opt(&self, list_id: Option<ListId>) -> ApiResult<Option<List>> {
match list_id {
Some(list_id) => self.list_service.get_list_opt(list_id).await,
_ => Ok(None),
}
}
pub async fn add_list(&self, input: AddListInput) -> ApiResult<List> {
self.list_service.add_list(&input.name).await
}
}