rowt/src/model/role.rs

61 lines
1.3 KiB
Rust
Raw Normal View History

use serde::Serialize;
use sqlx::{FromRow, SqlitePool};
#[derive(FromRow, Serialize, Clone)]
pub struct Role {
2024-01-22 19:05:18 +01:00
pub(crate) id: i64,
pub(crate) name: String,
}
impl Role {
pub async fn all(db: &SqlitePool) -> Vec<Role> {
sqlx::query_as!(Role, "SELECT id, name FROM role")
.fetch_all(db)
.await
.unwrap()
}
2024-01-01 15:50:06 +01:00
pub async fn find_by_id(db: &SqlitePool, name: i32) -> Option<Self> {
sqlx::query_as!(
Self,
"
SELECT id, name
FROM role
WHERE id like ?
",
name
)
.fetch_one(db)
.await
.ok()
}
2024-01-22 19:05:18 +01:00
pub async fn find_by_name(db: &SqlitePool, name: &str) -> Option<Self> {
sqlx::query_as!(
Self,
"
SELECT id, name
FROM role
WHERE name like ?
",
name
)
.fetch_one(db)
.await
.ok()
}
2024-01-01 15:50:06 +01:00
pub async fn mails_from_role(&self, db: &SqlitePool) -> Vec<String> {
let query = format!(
"SELECT u.mail
FROM user u
JOIN user_role ur ON u.id = ur.user_id
JOIN role r ON ur.role_id = r.id
2024-02-15 22:29:08 +01:00
WHERE r.id = {} AND deleted=0;",
2024-01-01 15:50:06 +01:00
self.id
);
sqlx::query_scalar(&query).fetch_all(db).await.unwrap()
}
}