various
This commit is contained in:
@ -2,11 +2,9 @@ use crate::models::CreateTrackedPlaylist;
|
||||
use crate::subsonic::Subsonic;
|
||||
use crate::CONFIG;
|
||||
use chrono::Local;
|
||||
use log::error;
|
||||
use sqlx::postgres::PgPool;
|
||||
use std::str::FromStr;
|
||||
use std::time::Duration;
|
||||
use thiserror::Error;
|
||||
use tokio::time::{interval, MissedTickBehavior};
|
||||
use uuid::Uuid;
|
||||
|
||||
|
19
src/main.rs
19
src/main.rs
@ -6,7 +6,9 @@ mod subsonic;
|
||||
mod track;
|
||||
|
||||
use crate::daemon::{create_playlists_daemon, update_playlists_daemon};
|
||||
use crate::models::{PartialTrackedPlaylist, TrackedPlaylist};
|
||||
use crate::models::{
|
||||
CreateTrackedPlaylist, PartialCreateTrackedPlaylist, PartialTrackedPlaylist, TrackedPlaylist,
|
||||
};
|
||||
use crate::subsonic::{Subsonic, SubsonicBuilder};
|
||||
use config::Config;
|
||||
use rocket::serde::json::{json, Json};
|
||||
@ -24,7 +26,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
CONFIG
|
||||
.set(
|
||||
Config::builder()
|
||||
.add_source(config::File::with_name("/etc/playlistd").required(false))
|
||||
.add_source(config::File::with_name("playlistd").required(false))
|
||||
.add_source(config::Environment::default().separator("_"))
|
||||
.build()
|
||||
.unwrap(),
|
||||
@ -71,6 +73,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
create_tracking_playlist,
|
||||
add_tracking_playlist,
|
||||
all_playlists,
|
||||
create_tracking_playlist_rule,
|
||||
],
|
||||
)
|
||||
.launch()
|
||||
@ -114,6 +117,18 @@ pub struct CreatePlaylist {
|
||||
pub tracking_type: Option<String>,
|
||||
}
|
||||
|
||||
/// Create a new playlist and attach a tracker
|
||||
#[post("/rule", data = "<create_playlist_rule>")]
|
||||
async fn create_tracking_playlist_rule(
|
||||
pool: &State<PgPool>,
|
||||
create_playlist_rule: Json<PartialCreateTrackedPlaylist>,
|
||||
) -> Json<Response<CreateTrackedPlaylist>> {
|
||||
match create_playlist_rule.record(pool.inner()).await {
|
||||
Ok(playlist) => Json(Response::Success(playlist)),
|
||||
Err(e) => Json(Response::Error(e.into())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Create a new playlist and attach a tracker
|
||||
#[post("/create", data = "<create_playlist>")]
|
||||
async fn create_tracking_playlist(
|
||||
|
@ -68,6 +68,40 @@ impl TrackedPlaylist {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PartialCreateTrackedPlaylist {
|
||||
pub name_template: String,
|
||||
pub playlist_size: i32,
|
||||
pub tracking_user: Option<String>,
|
||||
pub tracking_type: Option<String>,
|
||||
}
|
||||
|
||||
impl PartialCreateTrackedPlaylist {
|
||||
pub async fn record(
|
||||
&self,
|
||||
pool: impl Executor<'_, Database = Postgres> + Copy,
|
||||
) -> Result<CreateTrackedPlaylist, Error> {
|
||||
let uuid = Uuid::new_v4();
|
||||
|
||||
sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO "create_tracked_playlist"
|
||||
(id, name_template, playlist_size, tracking_user, tracking_type)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
"#,
|
||||
uuid,
|
||||
self.name_template,
|
||||
self.playlist_size,
|
||||
self.tracking_user,
|
||||
self.tracking_type,
|
||||
)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
|
||||
CreateTrackedPlaylist::rule(pool, uuid).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct CreateTrackedPlaylist {
|
||||
pub id: Uuid,
|
||||
|
Reference in New Issue
Block a user