use actix::{Actor, Context}; use sqlx::PgPool; pub use account_orders::*; pub use accounts::*; pub use order_items::*; pub use products::*; pub use shopping_cart_items::*; pub use shopping_carts::*; pub use stocks::*; pub mod account_orders; pub mod accounts; pub mod order_items; pub mod products; pub mod shopping_cart_items; pub mod shopping_carts; pub mod stocks; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("Failed to connect to database. {0:?}")] Connect(sqlx::Error), #[error("{0}")] Account(accounts::Error), #[error("{0}")] AccountOrder(account_orders::Error), #[error("{0}")] Product(products::Error), #[error("{0}")] Stock(stocks::Error), #[error("{0}")] OrderItem(order_items::Error), #[error("{0}")] ShoppingCart(shopping_carts::Error), #[error("{0}")] ShoppingCartItem(shopping_cart_items::Error), } pub type Result = std::result::Result; pub struct Database { pool: PgPool, } impl Clone for Database { fn clone(&self) -> Self { Self { pool: self.pool.clone() } } } impl Database { pub(crate) async fn build(url: &str) -> Result { let pool = sqlx::PgPool::connect(url).await.map_err(Error::Connect)?; Ok(Database { pool }) } pub fn pool(&self) -> &PgPool { &self.pool } } impl Actor for Database { type Context = Context; }