use { crate::entities::{ market::{Entity as Market, Model as MarketModel}, ticker::{Entity as Ticker, Model as TickerModel}, }, eyre::Result, sea_orm::{DatabaseConnection, DbErr, EntityTrait, IntoActiveModel}, }; /// ๐ŸŽ ยป service for the `Market` entity pub struct Service { conn: DatabaseConnection, } impl Service { pub async fn new(conn: DatabaseConnection) -> Self { Self { conn } } /// ๐ŸŽ ยป gets all markets from the database pub async fn get_all(&self) -> Result, DbErr> { let markets = Market::find().all(&self.conn).await?; Ok(markets) } /// ๐ŸŽ ยป gets a market by its id pub async fn create(&self, market: MarketModel) -> Result { Market::insert(market.clone().into_active_model()).exec(&self.conn).await?; Ok(market) } /// ๐ŸŽ ยป gets all markets with their tickers pub async fn get_all_with_tickers( &self, ) -> Result)>, DbErr> { let markets_with_tickers: Vec<(MarketModel, Vec)> = Market::find().find_with_related(Ticker).all(&self.conn).await?; Ok(markets_with_tickers) } }