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, pub note: Option, } pub fn locations(conn: &mut PgConnection) -> Result, DatabaseError> { Ok(location::table.load::(conn)?) } pub fn load_location(conn: &mut PgConnection, uuid: Uuid) -> Result { Ok(location::table.find(uuid).get_result(conn)?) } pub fn insert_location(conn: &mut PgConnection, location: Location) -> Result { Ok(insert_into(location::table) .values(&location) .returning(id) .get_result(conn)?) } pub fn rename_location( conn: &mut PgConnection, uuid: Uuid, name: String, ) -> Result { Ok( diesel::update(location::table.filter(location::id.eq(uuid))) .set(location::name.eq(&name)) .execute(conn)?, ) }