synthwave/src/route/admin.rs
2024-10-04 14:38:35 +02:00

37 lines
1,018 B
Rust

use super::api_error;
use super::FallibleApiResponse;
use crate::get_pg;
use crate::route::vec_to_api;
use rocket::{get, State};
use serde_json::json;
use crate::check_admin;
use crate::library::track::Track;
use crate::library::user::User;
use crate::library::Libary;
#[get("/library/clean")]
pub async fn clean_library(lib: &State<Libary>, u: User) -> FallibleApiResponse {
check_admin!(u);
lib.clean_lost_files().await;
Ok(json!({"ok": 1}))
}
#[get("/library/singles")]
pub async fn get_singles_route(u: User) -> FallibleApiResponse {
check_admin!(u);
let singles: Vec<Track> =
sqlx::query_as("SELECT * FROM track WHERE album IS NULL AND artist IS NOT NULL")
.fetch_all(get_pg!())
.await
.unwrap();
Ok(json!(vec_to_api(&singles).await))
}
#[get("/library/orphans")]
pub async fn get_orphans_route(u: User) -> FallibleApiResponse {
check_admin!(u);
let orphans = Track::get_orphans().await;
Ok(json!(vec_to_api(&orphans).await))
}