Files
LibreTunes/src/api/history.rs

57 lines
1.7 KiB
Rust

use crate::models::backend::HistoryEntry;
use crate::models::backend::Song;
use crate::util::error::*;
use crate::util::serverfn_client::Client;
use chrono::NaiveDateTime;
use leptos::prelude::*;
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(feature = "ssr")] {
use crate::util::backend_state::BackendState;
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>) -> BackendResult<Vec<HistoryEntry>> {
let user = get_user().await.context("Error getting logged-in user")?;
let mut db_conn = BackendState::get().await?.get_db_conn()?;
let history = user
.get_history(limit, &mut db_conn)
.context("Error getting history")?;
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>) -> BackendResult<Vec<(NaiveDateTime, Song)>> {
let user = get_user().await.context("Error getting logged-in user")?;
let mut db_conn = BackendState::get().await?.get_db_conn()?;
let songs = user
.get_history_songs(limit, &mut db_conn)
.context("Error getting history songs")?;
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) -> BackendResult<()> {
let user = get_user().await.context("Error getting logged-in user")?;
let mut db_conn = BackendState::get().await?.get_db_conn()?;
user.add_history(song_id, &mut db_conn)
.context("Error adding song to history")?;
Ok(())
}