use super::RepositoryError; use crate::domain::listing::{ListRequest, Page, SortDirection, SortKey}; use crate::domain::bags::{Bag, BagFilter, BagSortKey, BagWithRoast, NewBag, UpdateBag}; use crate::domain::ids::{BagId, RoastId, RoasterId, SessionId, TokenId, UserId}; use crate::domain::roasters::RoasterSortKey; use crate::domain::roasters::{NewRoaster, Roaster, UpdateRoaster}; use crate::domain::roasts::RoastSortKey; use crate::domain::roasts::{NewRoast, Roast, RoastWithRoaster, UpdateRoast}; use crate::domain::sessions::{NewSession, Session}; use crate::domain::timeline::{NewTimelineEvent, TimelineEvent, TimelineSortKey}; use crate::domain::tokens::{NewToken, Token}; use crate::domain::users::{NewUser, User}; use async_trait::async_trait; #[async_trait] pub trait RoasterRepository: Send + Sync { async fn insert(&self, roaster: NewRoaster) -> Result; async fn get(&self, id: RoasterId) -> Result; async fn get_by_slug(&self, slug: &str) -> Result; async fn list( &self, request: &ListRequest, ) -> Result, RepositoryError>; async fn update( &self, id: RoasterId, changes: UpdateRoaster, ) -> Result; async fn delete(&self, id: RoasterId) -> Result<(), RepositoryError>; async fn list_all(&self) -> Result, RepositoryError> { let sort_key = ::default(); let request = ListRequest::::show_all(sort_key, sort_key.default_direction()); let page = self.list(&request).await?; Ok(page.items) } async fn list_all_sorted( &self, sort_key: RoasterSortKey, direction: SortDirection, ) -> Result, RepositoryError> { let request = ListRequest::show_all(sort_key, direction); let page = self.list(&request).await?; Ok(page.items) } } #[async_trait] pub trait RoastRepository: Send + Sync { async fn insert(&self, roast: NewRoast) -> Result; async fn get(&self, id: RoastId) -> Result; async fn get_by_slug( &self, roaster_id: RoasterId, slug: &str, ) -> Result; async fn list( &self, request: &ListRequest, ) -> Result, RepositoryError>; async fn list_by_roaster( &self, roaster_id: RoasterId, ) -> Result, RepositoryError>; async fn update(&self, id: RoastId, changes: UpdateRoast) -> Result; async fn delete(&self, id: RoastId) -> Result<(), RepositoryError>; async fn list_all(&self) -> Result, RepositoryError> { let sort_key = ::default(); let request = ListRequest::::show_all(sort_key, sort_key.default_direction()); let page = self.list(&request).await?; Ok(page.items) } } #[async_trait] pub trait TimelineEventRepository: Send + Sync { async fn insert(&self, event: NewTimelineEvent) -> Result; async fn list( &self, request: &ListRequest, ) -> Result, RepositoryError>; async fn list_all(&self) -> Result, RepositoryError> { let sort_key = ::default(); let request = ListRequest::::show_all(sort_key, sort_key.default_direction()); let page = self.list(&request).await?; Ok(page.items) } } #[async_trait] pub trait UserRepository: Send + Sync { async fn insert(&self, user: NewUser) -> Result; async fn get(&self, id: UserId) -> Result; async fn get_by_username(&self, username: &str) -> Result; async fn exists(&self) -> Result; } #[async_trait] pub trait TokenRepository: Send + Sync { async fn insert(&self, token: NewToken) -> Result; async fn get(&self, id: TokenId) -> Result; async fn get_by_token_hash(&self, token_hash: &str) -> Result; async fn list_by_user(&self, user_id: UserId) -> Result, RepositoryError>; async fn revoke(&self, id: TokenId) -> Result; async fn update_last_used(&self, id: TokenId) -> Result<(), RepositoryError>; } #[async_trait] pub trait SessionRepository: Send + Sync { async fn insert(&self, session: NewSession) -> Result; async fn get(&self, id: SessionId) -> Result; async fn get_by_token_hash(&self, token_hash: &str) -> Result; async fn delete(&self, id: SessionId) -> Result<(), RepositoryError>; async fn delete_expired(&self) -> Result<(), RepositoryError>; } #[async_trait] pub trait BagRepository: Send + Sync { async fn insert(&self, bag: NewBag) -> Result; async fn get(&self, id: BagId) -> Result; async fn list( &self, filter: BagFilter, request: &ListRequest, ) -> Result, RepositoryError>; async fn update(&self, id: BagId, changes: UpdateBag) -> Result; async fn delete(&self, id: BagId) -> Result<(), RepositoryError>; }