#[cfg(feature = "test-helpers")] use fake::{faker::name::en::*, Dummy}; use serde::Deserialize; use serde::Serialize; use sqlx::Postgres; use uuid::Uuid; use validator::Validate; #[derive(Serialize, Debug, Deserialize)] pub struct {{entity_struct_name}} { // these are examples only pub id: Uuid, pub name: String, } #[derive(Deserialize, Validate, Clone)] #[cfg_attr(feature = "test-helpers", derive(Serialize, Dummy))] pub struct {{entity_struct_name}}Changeset { // these are examples only #[cfg_attr(feature = "test-helpers", dummy(faker = "Name()"))] #[validate(length(min = 1))] pub name: String, } pub async fn load_all( executor: impl sqlx::Executor<'_, Database = Postgres>, ) -> Result, anyhow::Error> { todo!("Adopt the SQL query as necessary!"); let {{entity_plural_name}} = sqlx::query_as!({{entity_struct_name}}, "SELECT id, name FROM {{entity_plural_name}}") .fetch_all(executor) .await?; Ok({{entity_plural_name}}) } pub async fn load( id: Uuid, executor: impl sqlx::Executor<'_, Database = Postgres>, ) -> Result<{{entity_struct_name}}, crate::Error> { todo!("Adopt the SQL query as necessary!"); match sqlx::query_as!( {{entity_struct_name}}, "SELECT id, description FROM {{entity_plural_name}} WHERE id = $1", id ) .fetch_optional(executor) .await .map_err(|e| crate::Error::DbError(e.into()))? { Some({{entity_singular_name}}) => Ok({{entity_singular_name}}), None => Err(crate::Error::NoRecordFound), } } pub async fn create( {{entity_singular_name}}: {{entity_struct_name}}Changeset, executor: impl sqlx::Executor<'_, Database = Postgres>, ) -> Result<{{entity_struct_name}}, crate::Error> { {{entity_singular_name}}.validate().map_err(crate::Error::ValidationError)?; todo!("Adopt the SQL query and bound parameters as necessary!"); let record = sqlx::query!( "INSERT INTO {{entity_plural_name}} (name) VALUES ($1) RETURNING id", {{entity_singular_name}}.name ) .fetch_one(executor) .await .map_err(|e| crate::Error::DbError(e.into()))?; Ok({{entity_struct_name}} { id: record.id, name: {{entity_singular_name}}.name, }) } pub async fn update( id: Uuid, {{entity_singular_name}}: {{entity_struct_name}}Changeset, executor: impl sqlx::Executor<'_, Database = Postgres>, ) -> Result<{{entity_struct_name}}, crate::Error> { {{entity_singular_name}}.validate().map_err(crate::Error::ValidationError)?; todo!("Adopt the SQL query and bound parameters as necessary!"); match sqlx::query!( "UPDATE {{entity_plural_name}} SET name = $1 WHERE id = $2 RETURNING id, name", {{entity_singular_name}}.name, id ) .fetch_optional(executor) .await .map_err(|e| crate::Error::DbError(e.into()))? { Some(record) => Ok({{entity_struct_name}} { id: record.id, name: record.name, }), None => Err(crate::Error::NoRecordFound), } } pub async fn delete( id: Uuid, executor: impl sqlx::Executor<'_, Database = Postgres>, ) -> Result<(), crate::Error> { todo!("Adopt the SQL query as necessary!"); match sqlx::query!("DELETE FROM {{entity_plural_name}} WHERE id = $1 RETURNING id", id) .fetch_optional(executor) .await .map_err(|e| crate::Error::DbError(e.into()))? { Some(_) => Ok(()), None => Err(crate::Error::NoRecordFound), } }