28 lines
741 B
Rust
28 lines
741 B
Rust
use sqlx::{query, query_as, PgPool};
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Area {
|
|
pub id: i32,
|
|
pub name: String
|
|
}
|
|
|
|
impl Area {
|
|
pub async fn create(pool: &PgPool, name: &str) -> anyhow::Result<i32> {
|
|
let result = query!("INSERT INTO area (name) VALUES ($1) RETURNING id;", name).fetch_one(pool).await?;
|
|
|
|
Ok(result.id)
|
|
}
|
|
|
|
pub async fn read_by_id(pool: &PgPool, id: i32) -> anyhow::Result<Area> {
|
|
let record = query_as!(Area, "SELECT * FROM area WHERE id = $1", id).fetch_one(pool).await?;
|
|
|
|
Ok(record)
|
|
}
|
|
|
|
pub async fn read_all(pool: &PgPool) -> anyhow::Result<Vec<Area>> {
|
|
let records = query_as!(Area, "SELECT * FROM area").fetch_all(pool).await?;
|
|
|
|
Ok(records)
|
|
}
|
|
}
|