// Models {% for model in models %} #[derive(Debug, Clone, Serialize, Deserialize)] pub struct {{ model.name|capitalize }} { {% for field in model.fields %} pub {{ field.name }}: {{ field.field_type|rust_type }}, {% endfor %} } impl {{ model.name|capitalize }} { pub fn new( {% for field in model.fields %} {{ field.name }}: {{ field.field_type|rust_type }}, {% endfor %} ) -> Self { Self { {% for field in model.fields %} {{ field.name }}, {% endfor %} } } } #[async_trait] impl DbModel for {{ model.name|capitalize }} { async fn create(self, db: &SqlitePool) -> Result { sqlx::query!( "INSERT INTO {{ model.name }} ({% for field in model.fields %}{{ field.name }}{% if not loop.last %}, {% endif %}{% endfor %}) VALUES ({% for field in model.fields %}?{% if not loop.last %}, {% endif %}{% endfor %})", {% for field in model.fields %} self.{{ field.name }}, {% endfor %} ) .execute(db) .await .map_err(WyreError::DatabaseError)?; Ok(self) } async fn read(id: Uuid, db: &SqlitePool) -> Result { sqlx::query_as!( Self, "SELECT * FROM {{ model.name }} WHERE id = ?", id ) .fetch_one(db) .await .map_err(|e| match e { sqlx::Error::RowNotFound => WyreError::NotFoundError(format!("{{ model.name }} with id {} not found", id)), _ => WyreError::DatabaseError(e), }) } async fn update(self, db: &SqlitePool) -> Result { sqlx::query!( "UPDATE {{ model.name }} SET {% for field in model.fields %} {{ field.name }} = ?{% if not loop.last %},{% endif %} {% endfor %} WHERE id = ?", {% for field in model.fields %} self.{{ field.name }}, {% endfor %} self.id ) .execute(db) .await .map_err(WyreError::DatabaseError)?; Ok(self) } async fn delete(id: Uuid, db: &SqlitePool) -> Result<(), WyreError> { sqlx::query!("DELETE FROM {{ model.name }} WHERE id = ?", id) .execute(db) .await .map_err(WyreError::DatabaseError)?; Ok(()) } } {% endfor %}