roles: sql, crate, struct, id, rocket id, list page
This commit is contained in:
parent
2e055e2037
commit
efaf2bda5a
23 changed files with 462 additions and 2 deletions
26
crates/roles/src/database.rs
Normal file
26
crates/roles/src/database.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use crate::error::Error;
|
||||
use crate::Role;
|
||||
use database::sqlx::SqliteExecutor;
|
||||
use database::Roles as DatabaseRoles;
|
||||
use id::RoleID;
|
||||
|
||||
impl From<DatabaseRoles> for Role {
|
||||
fn from(db: DatabaseRoles) -> Self {
|
||||
Self {
|
||||
name: RoleID(db.name),
|
||||
label: db.label,
|
||||
created_at: db.created_at,
|
||||
is_archived: db.is_archived,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Role {
|
||||
pub async fn get_all(conn: impl SqliteExecutor<'_>) -> Result<Vec<Self>, Error> {
|
||||
Ok(DatabaseRoles::get_all(conn)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(Self::from)
|
||||
.collect::<Vec<_>>())
|
||||
}
|
||||
}
|
||||
8
crates/roles/src/error.rs
Normal file
8
crates/roles/src/error.rs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
// error
|
||||
#[derive(thiserror::Error)]
|
||||
// the rest
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
#[error("Database: {0}")]
|
||||
Database(#[from] database::Error),
|
||||
}
|
||||
32
crates/roles/src/lib.rs
Normal file
32
crates/roles/src/lib.rs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
mod database;
|
||||
mod error;
|
||||
|
||||
use chrono::{DateTime, Utc};
|
||||
use id::RoleID;
|
||||
use serde::Serialize;
|
||||
|
||||
// Exports
|
||||
pub use crate::error::Error;
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct Role {
|
||||
name: RoleID,
|
||||
label: String,
|
||||
created_at: DateTime<Utc>,
|
||||
is_archived: bool,
|
||||
}
|
||||
|
||||
impl Role {
|
||||
pub fn name(&self) -> &RoleID {
|
||||
&self.name
|
||||
}
|
||||
pub fn label(&self) -> &str {
|
||||
&self.label
|
||||
}
|
||||
pub fn created_at(&self) -> DateTime<Utc> {
|
||||
self.created_at
|
||||
}
|
||||
pub fn is_archived(&self) -> bool {
|
||||
self.is_archived
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue