refactor
This commit is contained in:
parent
08e24f63f4
commit
fdb45f953e
15 changed files with 59 additions and 661 deletions
658
Cargo.lock
generated
658
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mongodb = "2.8.2"
|
|
||||||
rocket = { version = "0.5.1", features = ["json"] }
|
rocket = { version = "0.5.1", features = ["json"] }
|
||||||
rocket_cors = "0.6.0"
|
rocket_cors = "0.6.0"
|
||||||
serde = { version = "1.0.203", features = ["derive"] }
|
serde = { version = "1.0.203", features = ["derive"] }
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
use crate::route::ToAPI;
|
use crate::route::ToAPI;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use sqlx::FromRow;
|
use sqlx::FromRow;
|
||||||
|
|
||||||
use crate::{get_pg, library::artist::Artist};
|
use crate::get_pg;
|
||||||
|
|
||||||
use super::track::Track;
|
use super::track::Track;
|
||||||
|
|
||||||
|
@ -60,7 +59,9 @@ impl Album {
|
||||||
pub async fn remove(&self) {
|
pub async fn remove(&self) {
|
||||||
sqlx::query("DELETE FROM album WHERE id = $1")
|
sqlx::query("DELETE FROM album WHERE id = $1")
|
||||||
.bind(self.id)
|
.bind(self.id)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(id: &uuid::Uuid) -> Option<Self> {
|
pub async fn get(id: &uuid::Uuid) -> Option<Self> {
|
||||||
|
|
|
@ -56,7 +56,9 @@ impl Artist {
|
||||||
pub async fn remove(&self) {
|
pub async fn remove(&self) {
|
||||||
sqlx::query("DELETE FROM artist WHERE id = $1")
|
sqlx::query("DELETE FROM artist WHERE id = $1")
|
||||||
.bind(self.id)
|
.bind(self.id)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the image of an artist or `None` if it can't be found.
|
/// Gets the image of an artist or `None` if it can't be found.
|
||||||
|
|
|
@ -5,12 +5,6 @@ use serde::{Deserialize, Serialize};
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
pub struct AudioMetadata(pub serde_json::Value);
|
pub struct AudioMetadata(pub serde_json::Value);
|
||||||
|
|
||||||
impl From<AudioMetadata> for mongodb::bson::Bson {
|
|
||||||
fn from(val: AudioMetadata) -> Self {
|
|
||||||
mongodb::bson::to_bson(&val.0).unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl AudioMetadata {
|
impl AudioMetadata {
|
||||||
fn get_key(&self, key: &str) -> Option<&str> {
|
fn get_key(&self, key: &str) -> Option<&str> {
|
||||||
self.0.as_object()?.get(key)?.as_str()
|
self.0.as_object()?.get(key)?.as_str()
|
||||||
|
|
|
@ -5,7 +5,6 @@ use std::{
|
||||||
|
|
||||||
use album::Album;
|
use album::Album;
|
||||||
use artist::Artist;
|
use artist::Artist;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use track::Track;
|
use track::Track;
|
||||||
use walkdir::WalkDir;
|
use walkdir::WalkDir;
|
||||||
|
@ -170,7 +169,7 @@ impl Libary {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reload_metadata(&self, track_id: &uuid::Uuid) -> Result<(), ()> {
|
pub async fn reload_metadata(&self, track_id: &uuid::Uuid) -> Result<(), ()> {
|
||||||
let mut track = Track::get(track_id).await.ok_or_else(|| ())?;
|
let track = Track::get(track_id).await.ok_or_else(|| ())?;
|
||||||
let path = &track.path;
|
let path = &track.path;
|
||||||
log::info!("Rescanning metadata for {path}");
|
log::info!("Rescanning metadata for {path}");
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::FromRow;
|
use sqlx::FromRow;
|
||||||
|
|
||||||
use crate::{
|
use crate::{get_pg, library::user::User};
|
||||||
get_pg,
|
|
||||||
library::{track::Track, user::User},
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
|
||||||
pub struct Playlist {
|
pub struct Playlist {
|
||||||
|
|
|
@ -3,11 +3,7 @@ use serde_json::json;
|
||||||
use sqlx::prelude::FromRow;
|
use sqlx::prelude::FromRow;
|
||||||
use std::{collections::HashSet, str::FromStr};
|
use std::{collections::HashSet, str::FromStr};
|
||||||
|
|
||||||
use crate::{
|
use crate::{get_pg, library::album::Album, route::ToAPI};
|
||||||
get_pg,
|
|
||||||
library::{album::Album, artist::Artist},
|
|
||||||
route::ToAPI,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::{event::Event, metadata::AudioMetadata, user::User};
|
use super::{event::Event, metadata::AudioMetadata, user::User};
|
||||||
|
|
||||||
|
@ -28,7 +24,9 @@ impl Track {
|
||||||
.bind(data.get("path").unwrap().as_str().unwrap().to_string())
|
.bind(data.get("path").unwrap().as_str().unwrap().to_string())
|
||||||
.bind(data.get("title").unwrap().as_str().unwrap().to_string())
|
.bind(data.get("title").unwrap().as_str().unwrap().to_string())
|
||||||
.bind(data.get("meta"))
|
.bind(data.get("meta"))
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn of_path(path: &str) -> Option<Self> {
|
pub async fn of_path(path: &str) -> Option<Self> {
|
||||||
|
@ -74,7 +72,9 @@ impl Track {
|
||||||
.bind(title)
|
.bind(title)
|
||||||
.bind(meta)
|
.bind(meta)
|
||||||
.bind(self.id)
|
.bind(self.id)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
Some(())
|
Some(())
|
||||||
}
|
}
|
||||||
|
@ -89,7 +89,9 @@ impl Track {
|
||||||
pub async fn remove(&self) {
|
pub async fn remove(&self) {
|
||||||
sqlx::query("DELETE FROM track WHERE id = $1")
|
sqlx::query("DELETE FROM track WHERE id = $1")
|
||||||
.bind(self.id)
|
.bind(self.id)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn find_of_album(album: &uuid::Uuid) -> Vec<Self> {
|
pub async fn find_of_album(album: &uuid::Uuid) -> Vec<Self> {
|
||||||
|
|
|
@ -53,7 +53,9 @@ impl User {
|
||||||
.bind(&u.username)
|
.bind(&u.username)
|
||||||
.bind(&u.password)
|
.bind(&u.password)
|
||||||
.bind(&u.user_role)
|
.bind(&u.user_role)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
Some(u)
|
Some(u)
|
||||||
}
|
}
|
||||||
|
@ -74,7 +76,9 @@ impl User {
|
||||||
sqlx::query("UPDATE user SET password = $1 WHERE username = $2;")
|
sqlx::query("UPDATE user SET password = $1 WHERE username = $2;")
|
||||||
.bind(bcrypt::hash(new, bcrypt::DEFAULT_COST).unwrap())
|
.bind(bcrypt::hash(new, bcrypt::DEFAULT_COST).unwrap())
|
||||||
.bind(&self.username)
|
.bind(&self.username)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ mod library;
|
||||||
mod route;
|
mod route;
|
||||||
|
|
||||||
use library::user::{User, UserRole};
|
use library::user::{User, UserRole};
|
||||||
use mongodb::bson::doc;
|
|
||||||
use rocket::routes;
|
use rocket::routes;
|
||||||
use rocket::tokio::sync::OnceCell;
|
use rocket::tokio::sync::OnceCell;
|
||||||
use rocket::{http::Method, launch};
|
use rocket::{http::Method, launch};
|
||||||
|
|
|
@ -4,7 +4,6 @@ use super::vec_to_api;
|
||||||
use super::FallibleApiResponse;
|
use super::FallibleApiResponse;
|
||||||
use super::ToAPI;
|
use super::ToAPI;
|
||||||
use fs::NamedFile;
|
use fs::NamedFile;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use rocket::{fs, get, State};
|
use rocket::{fs, get, State};
|
||||||
|
|
||||||
use crate::cache::RouteCache;
|
use crate::cache::RouteCache;
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::get_pg;
|
use crate::get_pg;
|
||||||
use crate::library::track::Track;
|
use crate::library::track::Track;
|
||||||
use crate::library::user::User;
|
use crate::library::user::User;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use rocket::get;
|
use rocket::get;
|
||||||
use rocket::post;
|
use rocket::post;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
@ -172,7 +171,9 @@ pub async fn playlist_edit_route(
|
||||||
.bind(new_vis)
|
.bind(new_vis)
|
||||||
.bind(tracks_ref)
|
.bind(tracks_ref)
|
||||||
.bind(&to_uuid(&playlist_id)?)
|
.bind(&to_uuid(&playlist_id)?)
|
||||||
.fetch(get_pg!());
|
.fetch_one(get_pg!())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
Ok(json!({"edited": playlist_id}))
|
Ok(json!({"edited": playlist_id}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use super::api_error;
|
use super::api_error;
|
||||||
use super::FallibleApiResponse;
|
use super::FallibleApiResponse;
|
||||||
use mongodb::bson::doc;
|
|
||||||
use rocket::get;
|
use rocket::get;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use super::api_error;
|
use super::api_error;
|
||||||
use super::no_uuid_error;
|
|
||||||
use super::to_uuid;
|
use super::to_uuid;
|
||||||
use super::FallibleApiResponse;
|
use super::FallibleApiResponse;
|
||||||
use super::ToAPI;
|
use super::ToAPI;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::get_pg;
|
use crate::get_pg;
|
||||||
use crate::library::user::Session;
|
|
||||||
use crate::library::user::User;
|
use crate::library::user::User;
|
||||||
use crate::route::vec_to_api;
|
use crate::route::vec_to_api;
|
||||||
use rocket::get;
|
use rocket::get;
|
||||||
|
|
Loading…
Reference in a new issue