Files
LibreTunes/src/api/history.rs
Ethan Girouard 9cd1e8291a
Some checks failed
Push Workflows / rustfmt (push) Successful in 10s
Push Workflows / docs (push) Successful in 1m43s
Push Workflows / clippy (push) Successful in 2m12s
Push Workflows / test (push) Successful in 4m17s
Push Workflows / leptos-test (push) Successful in 4m24s
Push Workflows / build (push) Successful in 6m2s
Push Workflows / docker-build (push) Failing after 12m7s
Push Workflows / nix-build (push) Successful in 15m46s
Use feature-specific Client in server functions
2025-05-03 04:24:28 +00:00

51 lines
1.7 KiB
Rust

use crate::models::backend::HistoryEntry;
use crate::models::backend::Song;
use crate::util::serverfn_client::Client;
use chrono::NaiveDateTime;
use leptos::prelude::*;
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "ssr")] {
use leptos::server_fn::error::NoCustomError;
use crate::util::database::get_db_conn;
use crate::api::auth::get_user;
}
}
/// Get the history of the current user.
#[server(endpoint = "history/get", client = Client)]
pub async fn get_history(limit: Option<i64>) -> Result<Vec<HistoryEntry>, ServerFnError> {
let user = get_user().await?;
let db_con = &mut get_db_conn();
let history = user.get_history(limit, db_con).map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error getting history: {e}"))
})?;
Ok(history)
}
/// Get the listen dates and songs of the current user.
#[server(endpoint = "history/get_songs", client = Client)]
pub async fn get_history_songs(
limit: Option<i64>,
) -> Result<Vec<(NaiveDateTime, Song)>, ServerFnError> {
let user = get_user().await?;
let db_con = &mut get_db_conn();
let songs = user.get_history_songs(limit, db_con).map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error getting history songs: {e}"))
})?;
Ok(songs)
}
/// Add a song to the history of the current user.
#[server(endpoint = "history/add", client = Client)]
pub async fn add_history(song_id: i32) -> Result<(), ServerFnError> {
let user = get_user().await?;
let db_con = &mut get_db_conn();
user.add_history(song_id, db_con).map_err(|e| {
ServerFnError::<NoCustomError>::ServerError(format!("Error adding history: {e}"))
})?;
Ok(())
}