roles: sql, crate, struct, id, rocket id, list page

This commit is contained in:
Philippe Loctaux 2023-05-07 12:26:29 +02:00
parent 2e055e2037
commit efaf2bda5a
23 changed files with 462 additions and 2 deletions

View 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<_>>())
}
}

View 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
View 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
}
}