This commit is contained in:
JMARyA 2024-10-04 14:46:06 +02:00
parent 08e24f63f4
commit fdb45f953e
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
15 changed files with 59 additions and 661 deletions

658
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2021"
[dependencies]
mongodb = "2.8.2"
rocket = { version = "0.5.1", features = ["json"] }
rocket_cors = "0.6.0"
serde = { version = "1.0.203", features = ["derive"] }

View file

@ -1,10 +1,9 @@
use crate::route::ToAPI;
use mongodb::bson::doc;
use serde::{Deserialize, Serialize};
use serde_json::json;
use sqlx::FromRow;
use crate::{get_pg, library::artist::Artist};
use crate::get_pg;
use super::track::Track;
@ -60,7 +59,9 @@ impl Album {
pub async fn remove(&self) {
sqlx::query("DELETE FROM album WHERE id = $1")
.bind(self.id)
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
}
pub async fn get(id: &uuid::Uuid) -> Option<Self> {

View file

@ -56,7 +56,9 @@ impl Artist {
pub async fn remove(&self) {
sqlx::query("DELETE FROM artist WHERE id = $1")
.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.

View file

@ -5,12 +5,6 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
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 {
fn get_key(&self, key: &str) -> Option<&str> {
self.0.as_object()?.get(key)?.as_str()

View file

@ -5,7 +5,6 @@ use std::{
use album::Album;
use artist::Artist;
use mongodb::bson::doc;
use serde_json::json;
use track::Track;
use walkdir::WalkDir;
@ -170,7 +169,7 @@ impl Libary {
}
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;
log::info!("Rescanning metadata for {path}");

View file

@ -1,10 +1,7 @@
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use crate::{
get_pg,
library::{track::Track, user::User},
};
use crate::{get_pg, library::user::User};
#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
pub struct Playlist {

View file

@ -3,11 +3,7 @@ use serde_json::json;
use sqlx::prelude::FromRow;
use std::{collections::HashSet, str::FromStr};
use crate::{
get_pg,
library::{album::Album, artist::Artist},
route::ToAPI,
};
use crate::{get_pg, library::album::Album, route::ToAPI};
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("title").unwrap().as_str().unwrap().to_string())
.bind(data.get("meta"))
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
}
pub async fn of_path(path: &str) -> Option<Self> {
@ -74,7 +72,9 @@ impl Track {
.bind(title)
.bind(meta)
.bind(self.id)
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
Some(())
}
@ -89,7 +89,9 @@ impl Track {
pub async fn remove(&self) {
sqlx::query("DELETE FROM track WHERE id = $1")
.bind(self.id)
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
}
pub async fn find_of_album(album: &uuid::Uuid) -> Vec<Self> {

View file

@ -53,7 +53,9 @@ impl User {
.bind(&u.username)
.bind(&u.password)
.bind(&u.user_role)
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
Some(u)
}
@ -74,7 +76,9 @@ impl User {
sqlx::query("UPDATE user SET password = $1 WHERE username = $2;")
.bind(bcrypt::hash(new, bcrypt::DEFAULT_COST).unwrap())
.bind(&self.username)
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
return Ok(());
}

View file

@ -5,7 +5,6 @@ mod library;
mod route;
use library::user::{User, UserRole};
use mongodb::bson::doc;
use rocket::routes;
use rocket::tokio::sync::OnceCell;
use rocket::{http::Method, launch};

View file

@ -4,7 +4,6 @@ use super::vec_to_api;
use super::FallibleApiResponse;
use super::ToAPI;
use fs::NamedFile;
use mongodb::bson::doc;
use rocket::{fs, get, State};
use crate::cache::RouteCache;

View file

@ -1,7 +1,6 @@
use crate::get_pg;
use crate::library::track::Track;
use crate::library::user::User;
use mongodb::bson::doc;
use rocket::get;
use rocket::post;
use rocket::serde::json::Json;
@ -172,7 +171,9 @@ pub async fn playlist_edit_route(
.bind(new_vis)
.bind(tracks_ref)
.bind(&to_uuid(&playlist_id)?)
.fetch(get_pg!());
.fetch_one(get_pg!())
.await
.unwrap();
Ok(json!({"edited": playlist_id}))
}

View file

@ -1,6 +1,5 @@
use super::api_error;
use super::FallibleApiResponse;
use mongodb::bson::doc;
use rocket::get;
use serde_json::json;

View file

@ -1,7 +1,6 @@
use std::str::FromStr;
use super::api_error;
use super::no_uuid_error;
use super::to_uuid;
use super::FallibleApiResponse;
use super::ToAPI;

View file

@ -1,5 +1,4 @@
use crate::get_pg;
use crate::library::user::Session;
use crate::library::user::User;
use crate::route::vec_to_api;
use rocket::get;