From 61cabf1f05b6c90b3c4f1a151d610124f8ee5d54 Mon Sep 17 00:00:00 2001 From: Jackz Date: Tue, 22 Apr 2025 21:27:34 -0500 Subject: [PATCH] Add library list per user --- src/main.rs | 1 + src/managers/libraries.rs | 10 +++++++++- src/models/library.rs | 6 ++++++ src/routes/ui/user.rs | 7 +++++-- templates/index.html.hbs | 12 ++++++------ 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7fc2d41..fbbc871 100644 --- a/src/main.rs +++ b/src/main.rs @@ -131,6 +131,7 @@ async fn rocket() -> _ { .manage(libraries_manager) .manage(settings) .manage(sso) + .manage(users) .attach(store.fairing()) .attach(Template::custom(|engines| { diff --git a/src/managers/libraries.rs b/src/managers/libraries.rs index aa7d50a..d7cb8e6 100644 --- a/src/managers/libraries.rs +++ b/src/managers/libraries.rs @@ -1,9 +1,10 @@ use std::collections::HashMap; -use sqlx::{Pool, Postgres}; +use sqlx::{query_as, Pool, Postgres}; use tokio::sync::RwLock; use crate::objs::library::Library; use crate::managers::repos::{RepoContainer, RepoManager}; use crate::models; +use crate::models::library::LibraryModel; use crate::util::{JsonErrorResponse, ResponseError}; pub struct LibraryManager { @@ -19,6 +20,13 @@ impl LibraryManager { } } + pub async fn list(&self, user_id: &str) -> Result, anyhow::Error> { + // TODO: check for access from library_permissions + let libraries = query_as!(LibraryModel, "SELECT * FROM storage.libraries WHERE owner_id = $1", user_id) + .fetch_all(&self.pool) + .await.map_err(anyhow::Error::from)?; + Ok(libraries) + } pub async fn get(&self, library_id: &str) -> Result { let Some(library) = models::library::get_library(&self.pool, library_id).await .map_err(|e| ResponseError::GenericError)? else { diff --git a/src/models/library.rs b/src/models/library.rs index c251e32..f724408 100644 --- a/src/models/library.rs +++ b/src/models/library.rs @@ -26,6 +26,12 @@ pub struct LibraryWithRepoModel { pub storage_type: String, } +pub enum PermissionLevel { + ReadOnly = 0, + ReadWrite = 1, + Admin = 2 +} + pub async fn get_library(pool: &DB, library_id: &str) -> Result, anyhow::Error> { let library_id = Uuid::from_str(library_id)?; let library = query_as!(LibraryModel, "select * from storage.libraries where id = $1", library_id) diff --git a/src/routes/ui/user.rs b/src/routes/ui/user.rs index 70ff74f..b98cfcb 100644 --- a/src/routes/ui/user.rs +++ b/src/routes/ui/user.rs @@ -17,6 +17,7 @@ use tokio::sync::Mutex; use crate::consts::FILE_CONSTANTS; use crate::guards::{AuthUser}; use crate::managers::libraries::LibraryManager; +use crate::managers::user::UsersState; use crate::objs::library::ListOptions; use crate::routes::ui::auth; use crate::util::{JsonErrorResponse, ResponseError}; @@ -26,8 +27,10 @@ pub async fn user_settings(user: AuthUser, route: &Route) -> Template { Template::render("settings", context! { session: user.session, route: route.uri.path() }) } #[get("/")] -pub async fn index(user: AuthUser, route: &Route) -> Template { - Template::render("index", context! { session: user.session, route: route.uri.path(), test: "value" }) +pub async fn index(user: AuthUser, libraries: &State>>, route: &Route) -> Template { + let libraries = libraries.lock().await; + let list = libraries.list(&user.session.user.id).await.unwrap(); + Template::render("index", context! { session: user.session, libraries: list, route: route.uri.path(), test: "value" }) } #[get("/library/")] diff --git a/templates/index.html.hbs b/templates/index.html.hbs index 23c9dc8..cced765 100644 --- a/templates/index.html.hbs +++ b/templates/index.html.hbs @@ -16,20 +16,20 @@ Name - Size - Last Updated + Created Owner + {{#each libraries}} - My Library + {{name}} - - - Me + {{ created_at }} + {{ owner_id }} + {{/each}}