48 lines
1.3 KiB
Rust
48 lines
1.3 KiB
Rust
use crate::{DbConnection, schema::location_owner, user::DatabaseError};
|
|
use diesel::{
|
|
BoolExpressionMethods, ExpressionMethods, QueryDsl, RunQueryDsl,
|
|
dsl::{delete, insert_into},
|
|
prelude::{Insertable, Queryable},
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Insertable, Queryable)]
|
|
#[diesel(table_name = location_owner)]
|
|
pub struct LocationOwner {
|
|
pub location_id: Uuid,
|
|
pub user_id: Uuid,
|
|
}
|
|
|
|
pub fn grant_permission(
|
|
conn: &mut DbConnection,
|
|
location_owner: LocationOwner,
|
|
) -> Result<usize, DatabaseError> {
|
|
Ok(insert_into(location_owner::table)
|
|
.values(&location_owner)
|
|
.execute(conn)?)
|
|
}
|
|
|
|
pub fn revoke_permission(
|
|
conn: &mut DbConnection,
|
|
location_owner: LocationOwner,
|
|
) -> Result<usize, DatabaseError> {
|
|
Ok(delete(location_owner::table)
|
|
.filter(
|
|
location_owner::location_id
|
|
.eq(location_owner.location_id)
|
|
.and(location_owner::user_id.eq(location_owner.user_id)),
|
|
)
|
|
.execute(conn)?)
|
|
}
|
|
|
|
pub fn location_permissions(
|
|
conn: &mut DbConnection,
|
|
location_id: Uuid,
|
|
) -> Result<Vec<Uuid>, DatabaseError> {
|
|
Ok(location_owner::table
|
|
.select(location_owner::user_id)
|
|
.filter(location_owner::location_id.eq(location_id))
|
|
.get_results(conn)?)
|
|
}
|