Add library list per user

This commit is contained in:
Jackzie 2025-04-22 21:27:34 -05:00
parent c85d539dc1
commit 61cabf1f05
5 changed files with 27 additions and 9 deletions

View file

@ -131,6 +131,7 @@ async fn rocket() -> _ {
.manage(libraries_manager) .manage(libraries_manager)
.manage(settings) .manage(settings)
.manage(sso) .manage(sso)
.manage(users)
.attach(store.fairing()) .attach(store.fairing())
.attach(Template::custom(|engines| { .attach(Template::custom(|engines| {

View file

@ -1,9 +1,10 @@
use std::collections::HashMap; use std::collections::HashMap;
use sqlx::{Pool, Postgres}; use sqlx::{query_as, Pool, Postgres};
use tokio::sync::RwLock; use tokio::sync::RwLock;
use crate::objs::library::Library; use crate::objs::library::Library;
use crate::managers::repos::{RepoContainer, RepoManager}; use crate::managers::repos::{RepoContainer, RepoManager};
use crate::models; use crate::models;
use crate::models::library::LibraryModel;
use crate::util::{JsonErrorResponse, ResponseError}; use crate::util::{JsonErrorResponse, ResponseError};
pub struct LibraryManager { pub struct LibraryManager {
@ -19,6 +20,13 @@ impl LibraryManager {
} }
} }
pub async fn list(&self, user_id: &str) -> Result<Vec<LibraryModel>, 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<Library, ResponseError> { pub async fn get(&self, library_id: &str) -> Result<Library, ResponseError> {
let Some(library) = models::library::get_library(&self.pool, library_id).await let Some(library) = models::library::get_library(&self.pool, library_id).await
.map_err(|e| ResponseError::GenericError)? else { .map_err(|e| ResponseError::GenericError)? else {

View file

@ -26,6 +26,12 @@ pub struct LibraryWithRepoModel {
pub storage_type: String, pub storage_type: String,
} }
pub enum PermissionLevel {
ReadOnly = 0,
ReadWrite = 1,
Admin = 2
}
pub async fn get_library(pool: &DB, library_id: &str) -> Result<Option<LibraryModel>, anyhow::Error> { pub async fn get_library(pool: &DB, library_id: &str) -> Result<Option<LibraryModel>, anyhow::Error> {
let library_id = Uuid::from_str(library_id)?; let library_id = Uuid::from_str(library_id)?;
let library = query_as!(LibraryModel, "select * from storage.libraries where id = $1", library_id) let library = query_as!(LibraryModel, "select * from storage.libraries where id = $1", library_id)

View file

@ -17,6 +17,7 @@ use tokio::sync::Mutex;
use crate::consts::FILE_CONSTANTS; use crate::consts::FILE_CONSTANTS;
use crate::guards::{AuthUser}; use crate::guards::{AuthUser};
use crate::managers::libraries::LibraryManager; use crate::managers::libraries::LibraryManager;
use crate::managers::user::UsersState;
use crate::objs::library::ListOptions; use crate::objs::library::ListOptions;
use crate::routes::ui::auth; use crate::routes::ui::auth;
use crate::util::{JsonErrorResponse, ResponseError}; 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() }) Template::render("settings", context! { session: user.session, route: route.uri.path() })
} }
#[get("/")] #[get("/")]
pub async fn index(user: AuthUser, route: &Route) -> Template { pub async fn index(user: AuthUser, libraries: &State<Arc<Mutex<LibraryManager>>>, route: &Route) -> Template {
Template::render("index", context! { session: user.session, route: route.uri.path(), test: "value" }) 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/<library_id>")] #[get("/library/<library_id>")]

View file

@ -16,20 +16,20 @@
<thead> <thead>
<tr> <tr>
<td>Name </td> <td>Name </td>
<td>Size </td> <td>Created </td>
<td>Last Updated </td>
<td>Owner </td> <td>Owner </td>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{#each libraries}}
<tr> <tr>
<td class="px-4 py-4"> <td class="px-4 py-4">
<a href="/library/dbabbf7d-9b63-487b-9908-57c2df11b2d2/My Library/">My Library</a> <a href="/library/{{id}}/{{name}}/">{{name}}</a>
</td> </td>
<td></td> <td>{{ created_at }}</td>
<td></td> <td>{{ owner_id }}</td>
<td>Me</td>
</tr> </tr>
{{/each}}
</tbody> </tbody>
</table> </table>
</div> </div>