use anyhow::{anyhow, Result}; use async_trait::async_trait; use sqlx::postgres::PgPool; use url::Url; use super::ListParams; #[async_trait] pub trait Model where Self: std::marker::Sized, { fn resource_name() -> &'static str; fn pk(&self) -> Option; fn url(&self, api_url: &Url) -> Result { if let Some(pk) = self.pk() { Ok(api_url.join(&format!("/{}s/{}", Self::resource_name(), pk))?) } else { Err(anyhow!( "Cannot get URL for {} with no primary key", Self::resource_name() )) } } async fn get(db: &PgPool, id: i32) -> Result; async fn save(self, db: &PgPool) -> Result; async fn delete(db: &PgPool, id: i32) -> Result; async fn list(db: &PgPool, list_params: ListParams) -> Result>; }