45 lines
1.2 KiB
Rust
45 lines
1.2 KiB
Rust
use crate::schema::location::{self, id};
|
|
use diesel::{
|
|
ExpressionMethods, Insertable, PgConnection, QueryDsl, Queryable, RunQueryDsl, dsl::insert_into,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use super::error::DatabaseError;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
|
|
#[diesel(table_name = location)]
|
|
pub struct Location {
|
|
pub id: Uuid,
|
|
pub name: String,
|
|
pub address: Option<String>,
|
|
pub note: Option<String>,
|
|
}
|
|
|
|
pub fn locations(conn: &mut PgConnection) -> Result<Vec<Location>, DatabaseError> {
|
|
Ok(location::table.load::<Location>(conn)?)
|
|
}
|
|
|
|
pub fn load_location(conn: &mut PgConnection, uuid: Uuid) -> Result<Location, DatabaseError> {
|
|
Ok(location::table.find(uuid).get_result(conn)?)
|
|
}
|
|
|
|
pub fn insert_location(conn: &mut PgConnection, location: Location) -> Result<Uuid, DatabaseError> {
|
|
Ok(insert_into(location::table)
|
|
.values(&location)
|
|
.returning(id)
|
|
.get_result(conn)?)
|
|
}
|
|
|
|
pub fn rename_location(
|
|
conn: &mut PgConnection,
|
|
uuid: Uuid,
|
|
name: String,
|
|
) -> Result<usize, DatabaseError> {
|
|
Ok(
|
|
diesel::update(location::table.filter(location::id.eq(uuid)))
|
|
.set(location::name.eq(&name))
|
|
.execute(conn)?,
|
|
)
|
|
}
|