Use BackendState::get_db_conn() instead of database module
This commit is contained in:
@ -8,8 +8,8 @@ use cfg_if::cfg_if;
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use diesel::prelude::*;
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::models::backend;
|
||||
use crate::util::backend_state::BackendState;
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,11 +18,11 @@ pub async fn get_album(id: i32) -> BackendResult<Option<frontend::Album>> {
|
||||
use crate::models::backend::Album;
|
||||
use crate::schema::*;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let album = albums::table
|
||||
.find(id)
|
||||
.first::<Album>(db_con)
|
||||
.first::<Album>(&mut db_conn)
|
||||
.optional()
|
||||
.context("Error loading album from database")?;
|
||||
|
||||
@ -32,7 +32,7 @@ pub async fn get_album(id: i32) -> BackendResult<Option<frontend::Album>> {
|
||||
.filter(album_artists::album_id.eq(id))
|
||||
.inner_join(artists::table.on(album_artists::artist_id.eq(artists::id)))
|
||||
.select(artists::all_columns)
|
||||
.load(db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading album artists from database")?;
|
||||
|
||||
let img = album
|
||||
@ -60,7 +60,7 @@ pub async fn get_songs(id: i32) -> BackendResult<Vec<frontend::Song>> {
|
||||
.await
|
||||
.context("Error getting logged-in user")?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let song_list = if let Some(user) = user {
|
||||
let song_list: Vec<(
|
||||
@ -95,7 +95,7 @@ pub async fn get_songs(id: i32) -> BackendResult<Vec<frontend::Song>> {
|
||||
song_dislikes::all_columns.nullable(),
|
||||
))
|
||||
.order(songs::track.asc())
|
||||
.load(db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading album songs from database")?;
|
||||
song_list
|
||||
} else {
|
||||
@ -117,7 +117,7 @@ pub async fn get_songs(id: i32) -> BackendResult<Vec<frontend::Song>> {
|
||||
artists::all_columns.nullable(),
|
||||
))
|
||||
.order(songs::track.asc())
|
||||
.load(db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading album songs from database")?;
|
||||
|
||||
let song_list: Vec<(
|
||||
|
@ -5,7 +5,7 @@ use leptos::prelude::*;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::util::backend_state::BackendState;
|
||||
use diesel::prelude::*;
|
||||
use chrono::NaiveDate;
|
||||
}
|
||||
@ -51,10 +51,11 @@ pub async fn add_album(
|
||||
image_path: image_path_arg,
|
||||
};
|
||||
|
||||
let db = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
diesel::insert_into(albums::table)
|
||||
.values(&new_album)
|
||||
.execute(db)
|
||||
.execute(&mut db_conn)
|
||||
.context("Error inserting new album into database")?;
|
||||
|
||||
Ok(())
|
||||
|
@ -9,11 +9,11 @@ use crate::util::serverfn_client::Client;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::util::database::get_db_conn;
|
||||
use diesel::prelude::*;
|
||||
use std::collections::HashMap;
|
||||
use crate::models::backend::Album;
|
||||
use crate::models::backend::NewArtist;
|
||||
use crate::util::backend_state::BackendState;
|
||||
}
|
||||
}
|
||||
|
||||
@ -32,10 +32,11 @@ pub async fn add_artist(artist_name: String) -> BackendResult<()> {
|
||||
|
||||
let new_artist = NewArtist { name: artist_name };
|
||||
|
||||
let db = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
diesel::insert_into(artists)
|
||||
.values(&new_artist)
|
||||
.execute(db)
|
||||
.execute(&mut db_conn)
|
||||
.context("Error inserting new artist into database")?;
|
||||
|
||||
Ok(())
|
||||
@ -45,10 +46,11 @@ pub async fn add_artist(artist_name: String) -> BackendResult<()> {
|
||||
pub async fn get_artist_by_id(artist_id: i32) -> BackendResult<Option<Artist>> {
|
||||
use crate::schema::artists::dsl::*;
|
||||
|
||||
let db = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let artist = artists
|
||||
.filter(id.eq(artist_id))
|
||||
.first::<Artist>(db)
|
||||
.first::<Artist>(&mut db_conn)
|
||||
.optional()
|
||||
.context("Error loading artist from database")?;
|
||||
|
||||
@ -66,7 +68,8 @@ pub async fn top_songs_by_artist(
|
||||
|
||||
let user_id = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let db = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let song_play_counts: Vec<(i32, i64)> = if let Some(limit) = limit {
|
||||
song_history::table
|
||||
.group_by(song_history::song_id)
|
||||
@ -76,7 +79,7 @@ pub async fn top_songs_by_artist(
|
||||
.order_by(diesel::dsl::count(song_history::id).desc())
|
||||
.left_join(songs::table.on(songs::id.eq(song_history::song_id)))
|
||||
.limit(limit)
|
||||
.load(db)?
|
||||
.load(&mut db_conn)?
|
||||
} else {
|
||||
song_history::table
|
||||
.group_by(song_history::song_id)
|
||||
@ -85,7 +88,7 @@ pub async fn top_songs_by_artist(
|
||||
.filter(song_artists::artist_id.eq(artist_id))
|
||||
.order_by(diesel::dsl::count(song_history::id).desc())
|
||||
.left_join(songs::table.on(songs::id.eq(song_history::song_id)))
|
||||
.load(db)?
|
||||
.load(&mut db_conn)?
|
||||
};
|
||||
|
||||
let song_play_counts: HashMap<i32, i64> = song_play_counts.into_iter().collect();
|
||||
@ -122,7 +125,7 @@ pub async fn top_songs_by_artist(
|
||||
song_likes::all_columns.nullable(),
|
||||
song_dislikes::all_columns.nullable(),
|
||||
))
|
||||
.load(db)?;
|
||||
.load(&mut db_conn)?;
|
||||
|
||||
let mut top_songs_map: HashMap<i32, (frontend::Song, i64)> =
|
||||
HashMap::with_capacity(top_songs.len());
|
||||
@ -184,7 +187,7 @@ pub async fn albums_by_artist(
|
||||
) -> BackendResult<Vec<frontend::Album>> {
|
||||
use crate::schema::*;
|
||||
|
||||
let db = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let album_ids = albums::table
|
||||
.left_join(album_artists::table)
|
||||
@ -208,7 +211,7 @@ pub async fn albums_by_artist(
|
||||
.on(albums::id.eq(album_artists::album_id)),
|
||||
)
|
||||
.select((albums::all_columns, artists::all_columns))
|
||||
.load(db)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading album artists from database")?;
|
||||
|
||||
for (album, artist) in album_artists {
|
||||
|
@ -5,13 +5,13 @@ use leptos::prelude::*;
|
||||
#[server(endpoint = "health", client = Client)]
|
||||
pub async fn health() -> BackendResult<String> {
|
||||
use crate::util::backend_state::BackendState;
|
||||
use crate::util::database::get_db_conn;
|
||||
use diesel::connection::SimpleConnection;
|
||||
use tower_sessions_redis_store::fred::interfaces::ClientLike;
|
||||
|
||||
let backend_state = BackendState::get().await?;
|
||||
|
||||
get_db_conn()
|
||||
backend_state
|
||||
.get_db_conn()?
|
||||
.batch_execute("SELECT 1")
|
||||
.context("Failed to execute database health check query")?;
|
||||
|
||||
|
@ -9,7 +9,7 @@ use cfg_if::cfg_if;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::util::backend_state::BackendState;
|
||||
use crate::api::auth::get_user;
|
||||
}
|
||||
}
|
||||
@ -19,9 +19,10 @@ cfg_if! {
|
||||
pub async fn get_history(limit: Option<i64>) -> BackendResult<Vec<HistoryEntry>> {
|
||||
let user = get_user().await.context("Error getting logged-in user")?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let history = user
|
||||
.get_history(limit, db_con)
|
||||
.get_history(limit, &mut db_conn)
|
||||
.context("Error getting history")?;
|
||||
|
||||
Ok(history)
|
||||
@ -32,10 +33,10 @@ pub async fn get_history(limit: Option<i64>) -> BackendResult<Vec<HistoryEntry>>
|
||||
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 db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let songs = user
|
||||
.get_history_songs(limit, db_con)
|
||||
.get_history_songs(limit, &mut db_conn)
|
||||
.context("Error getting history songs")?;
|
||||
|
||||
Ok(songs)
|
||||
@ -46,8 +47,9 @@ pub async fn get_history_songs(limit: Option<i64>) -> BackendResult<Vec<(NaiveDa
|
||||
pub async fn add_history(song_id: i32) -> BackendResult<()> {
|
||||
let user = get_user().await.context("Error getting logged-in user")?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
user.add_history(song_id, db_con)
|
||||
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(())
|
||||
|
@ -9,8 +9,8 @@ cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::api::auth::get_user;
|
||||
use diesel::prelude::*;
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::util::extract_field::extract_field;
|
||||
use crate::util::backend_state::BackendState;
|
||||
use std::collections::HashMap;
|
||||
use log::*;
|
||||
use crate::schema::*;
|
||||
@ -19,7 +19,7 @@ cfg_if! {
|
||||
|
||||
#[cfg(feature = "ssr")]
|
||||
async fn user_owns_playlist(user_id: i32, playlist_id: i32) -> BackendResult<bool> {
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let exists = playlists::table
|
||||
.find(playlist_id)
|
||||
@ -37,7 +37,7 @@ async fn user_owns_playlist(user_id: i32, playlist_id: i32) -> BackendResult<boo
|
||||
pub async fn get_playlists() -> BackendResult<Vec<backend::Playlist>> {
|
||||
let user_id = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let playlists = playlists::table
|
||||
.filter(playlists::owner_id.eq(user_id))
|
||||
@ -52,7 +52,7 @@ pub async fn get_playlists() -> BackendResult<Vec<backend::Playlist>> {
|
||||
pub async fn get_playlist(playlist_id: i32) -> BackendResult<backend::Playlist> {
|
||||
let user_id = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let playlist: backend::Playlist = playlists::table
|
||||
.find(playlist_id)
|
||||
@ -78,7 +78,7 @@ pub async fn get_playlist_songs(playlist_id: i32) -> BackendResult<Vec<frontend:
|
||||
.context("Playlist does not exist or does not belong to the user"));
|
||||
}
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let songs: Vec<(
|
||||
backend::Song,
|
||||
@ -177,7 +177,7 @@ pub async fn add_song_to_playlist(playlist_id: i32, song_id: i32) -> BackendResu
|
||||
.context("Playlist does not exist or does not belong to the user"));
|
||||
}
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
diesel::insert_into(crate::schema::playlist_songs::table)
|
||||
.values((
|
||||
@ -243,7 +243,7 @@ pub async fn create_playlist(data: MultipartData) -> BackendResult<()> {
|
||||
owner_id: user.id,
|
||||
};
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// Create a transaction to create the playlist
|
||||
// If saving the image fails, the playlist will not be created
|
||||
@ -357,7 +357,7 @@ pub async fn delete_playlist(playlist_id: i32) -> BackendResult<()> {
|
||||
.context("Playlist does not exist or does not belong to the user"));
|
||||
}
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
diesel::delete(playlists::table.find(playlist_id))
|
||||
.execute(&mut db_conn)
|
||||
@ -381,7 +381,7 @@ pub async fn rename_playlist(id: i32, new_name: String) -> BackendResult<()> {
|
||||
return Err(AccessError::NotFoundOrUnauthorized.into());
|
||||
}
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
diesel::update(playlists::table.find(id))
|
||||
.set(playlists::name.eq(new_name))
|
||||
|
@ -11,12 +11,12 @@ cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::api::auth::get_user;
|
||||
|
||||
use crate::util::database::get_db_conn;
|
||||
use diesel::prelude::*;
|
||||
use diesel::dsl::count;
|
||||
use crate::models::backend::{Album, Artist, Song, HistoryEntry};
|
||||
use crate::models::backend;
|
||||
use crate::schema::*;
|
||||
use crate::util::backend_state::BackendState;
|
||||
|
||||
use std::collections::HashMap;
|
||||
}
|
||||
@ -82,7 +82,7 @@ pub async fn recent_songs(
|
||||
) -> BackendResult<Vec<(NaiveDateTime, frontend::Song)>> {
|
||||
let viewing_user_id = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let mut db_con = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// Create an alias for the table so it can be referenced twice in the query
|
||||
let history2 = diesel::alias!(song_history as history2);
|
||||
@ -134,7 +134,7 @@ pub async fn recent_songs(
|
||||
song_likes::all_columns.nullable(),
|
||||
song_dislikes::all_columns.nullable(),
|
||||
))
|
||||
.load(&mut db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading recent songs from database")?;
|
||||
|
||||
// Process the history data into a map of song ids to song data
|
||||
@ -201,7 +201,7 @@ pub async fn top_songs(
|
||||
) -> BackendResult<Vec<(i64, frontend::Song)>> {
|
||||
let viewing_user_id = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let mut db_con = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// Get the play count and ids of the songs listened to in the date range
|
||||
let history_counts: Vec<(i32, i64)> = if let Some(limit) = limit {
|
||||
@ -212,7 +212,7 @@ pub async fn top_songs(
|
||||
.select((song_history::song_id, count(song_history::song_id)))
|
||||
.order(count(song_history::song_id).desc())
|
||||
.limit(limit)
|
||||
.load(&mut db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading top song ids and counts from database")?
|
||||
} else {
|
||||
song_history::table
|
||||
@ -220,7 +220,7 @@ pub async fn top_songs(
|
||||
.filter(song_history::user_id.eq(for_user_id))
|
||||
.group_by(song_history::song_id)
|
||||
.select((song_history::song_id, count(song_history::song_id)))
|
||||
.load(&mut db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading top song ids and counts from database")?
|
||||
};
|
||||
|
||||
@ -259,7 +259,7 @@ pub async fn top_songs(
|
||||
song_likes::all_columns.nullable(),
|
||||
song_dislikes::all_columns.nullable(),
|
||||
))
|
||||
.load(&mut db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading top songs from database")?;
|
||||
|
||||
// Process the history data into a map of song ids to song data
|
||||
@ -328,7 +328,7 @@ pub async fn top_artists(
|
||||
end_date: NaiveDateTime,
|
||||
limit: Option<i64>,
|
||||
) -> BackendResult<Vec<(i64, frontend::Artist)>> {
|
||||
let mut db_con = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let artist_counts: Vec<(i64, Artist)> = if let Some(limit) = limit {
|
||||
song_history::table
|
||||
@ -340,7 +340,7 @@ pub async fn top_artists(
|
||||
.select((count(artists::id), artists::all_columns))
|
||||
.order(count(artists::id).desc())
|
||||
.limit(limit)
|
||||
.load(&mut db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading top artists from database")?
|
||||
} else {
|
||||
song_history::table
|
||||
@ -351,7 +351,7 @@ pub async fn top_artists(
|
||||
.group_by(artists::id)
|
||||
.select((count(artists::id), artists::all_columns))
|
||||
.order(count(artists::id).desc())
|
||||
.load(&mut db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading top artists from database")?
|
||||
};
|
||||
|
||||
@ -376,7 +376,7 @@ pub async fn top_artists(
|
||||
pub async fn get_liked_songs() -> BackendResult<Vec<frontend::Song>> {
|
||||
let user_id = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let songs: Vec<(
|
||||
backend::Song,
|
||||
|
@ -13,8 +13,7 @@ if #[cfg(feature = "ssr")] {
|
||||
use diesel::expression::AsExpression;
|
||||
use std::collections::HashMap;
|
||||
use crate::models::backend;
|
||||
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::util::backend_state::BackendState;
|
||||
|
||||
// Define pg_trgm operators
|
||||
// Functions do not use indices for queries, so we need to use operators
|
||||
@ -53,7 +52,7 @@ pub async fn search_albums(
|
||||
) -> BackendResult<SearchResults<frontend::Album>> {
|
||||
use crate::schema::*;
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let album_ids = albums::table
|
||||
.filter(trgm_similar(albums::title, query.clone()))
|
||||
@ -119,7 +118,7 @@ pub async fn search_artists(
|
||||
) -> BackendResult<SearchResults<frontend::Artist>> {
|
||||
use crate::schema::*;
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let artist_list = artists::table
|
||||
.filter(trgm_similar(artists::name, query.clone()))
|
||||
@ -166,7 +165,7 @@ pub async fn search_songs(
|
||||
.await
|
||||
.context("Error getting logged-in user")?;
|
||||
|
||||
let mut db_conn = get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let song_list = if let Some(user) = user {
|
||||
let song_list: Vec<(
|
||||
|
@ -8,7 +8,7 @@ use crate::util::serverfn_client::Client;
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::util::backend_state::BackendState;
|
||||
use crate::api::auth::get_user;
|
||||
use crate::models::backend::{Song, Album, Artist};
|
||||
use diesel::prelude::*;
|
||||
@ -20,9 +20,9 @@ cfg_if! {
|
||||
pub async fn set_like_song(song_id: i32, like: bool) -> BackendResult<()> {
|
||||
let user = get_user().await.context("Error getting logged-in user")?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
user.set_like_song(song_id, like, db_con)
|
||||
user.set_like_song(song_id, like, &mut db_conn)
|
||||
.await
|
||||
.context("Error setting like status for song")
|
||||
}
|
||||
@ -32,9 +32,9 @@ pub async fn set_like_song(song_id: i32, like: bool) -> BackendResult<()> {
|
||||
pub async fn set_dislike_song(song_id: i32, dislike: bool) -> BackendResult<()> {
|
||||
let user = get_user().await.context("Error getting logged-in user")?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
user.set_dislike_song(song_id, dislike, db_con)
|
||||
user.set_dislike_song(song_id, dislike, &mut db_conn)
|
||||
.await
|
||||
.context("Error setting dislike status for song")
|
||||
}
|
||||
@ -44,18 +44,18 @@ pub async fn set_dislike_song(song_id: i32, dislike: bool) -> BackendResult<()>
|
||||
pub async fn get_like_dislike_song(song_id: i32) -> BackendResult<(bool, bool)> {
|
||||
let user = get_user().await.context("Error getting logged-in user")?;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// TODO this could probably be done more efficiently with a tokio::try_join, but
|
||||
// doing so is much more complicated than it would initially seem
|
||||
|
||||
let like = user
|
||||
.get_like_song(song_id, db_con)
|
||||
.get_like_song(song_id, &mut db_conn)
|
||||
.await
|
||||
.context("Error getting song like status")?;
|
||||
|
||||
let dislike = user
|
||||
.get_dislike_song(song_id, db_con)
|
||||
.get_dislike_song(song_id, &mut db_conn)
|
||||
.await
|
||||
.context("Error getting song dislike status")?;
|
||||
|
||||
@ -68,7 +68,7 @@ pub async fn get_song_by_id(song_id: i32) -> BackendResult<Option<frontend::Song
|
||||
|
||||
let user_id: i32 = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let song_parts: Vec<(
|
||||
Song,
|
||||
@ -101,7 +101,7 @@ pub async fn get_song_by_id(song_id: i32) -> BackendResult<Option<frontend::Song
|
||||
song_likes::all_columns.nullable(),
|
||||
song_dislikes::all_columns.nullable(),
|
||||
))
|
||||
.load(db_con)
|
||||
.load(&mut db_conn)
|
||||
.context("Error loading song from database")?;
|
||||
|
||||
let song = song_parts.first().cloned();
|
||||
@ -142,12 +142,12 @@ pub async fn get_song_by_id(song_id: i32) -> BackendResult<Option<frontend::Song
|
||||
pub async fn get_song_plays(song_id: i32) -> BackendResult<i64> {
|
||||
use crate::schema::*;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let plays = song_history::table
|
||||
.filter(song_history::song_id.eq(song_id))
|
||||
.count()
|
||||
.get_result::<i64>(db_con)
|
||||
.get_result::<i64>(&mut db_conn)
|
||||
.context("Error getting song plays")?;
|
||||
|
||||
Ok(plays)
|
||||
@ -159,7 +159,7 @@ pub async fn get_my_song_plays(song_id: i32) -> BackendResult<i64> {
|
||||
|
||||
let user_id: i32 = get_user().await.context("Error getting logged-in user")?.id;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let plays = song_history::table
|
||||
.filter(
|
||||
@ -168,7 +168,7 @@ pub async fn get_my_song_plays(song_id: i32) -> BackendResult<i64> {
|
||||
.and(song_history::user_id.eq(user_id)),
|
||||
)
|
||||
.count()
|
||||
.get_result::<i64>(db_con)
|
||||
.get_result::<i64>(&mut db_conn)
|
||||
.context("Error getting song plays for user")?;
|
||||
|
||||
Ok(plays)
|
||||
|
@ -7,7 +7,7 @@ use cfg_if::cfg_if;
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use multer::Field;
|
||||
use crate::util::database::get_db_conn;
|
||||
use crate::util::backend_state::BackendState;
|
||||
use crate::util::extract_field::extract_field;
|
||||
use diesel::prelude::*;
|
||||
use log::*;
|
||||
@ -27,16 +27,17 @@ async fn validate_artist_ids(artist_ids: Field<'static>) -> BackendResult<Vec<i3
|
||||
Ok(artist_ids) => {
|
||||
let artist_ids = artist_ids.trim_end_matches(',').split(',');
|
||||
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
artist_ids
|
||||
.filter(|artist_id| !artist_id.is_empty())
|
||||
.map(|artist_id| {
|
||||
// Parse the artist id as an integer
|
||||
if let Ok(artist_id) = artist_id.parse::<i32>() {
|
||||
// Check if the artist exists
|
||||
let db_con = &mut get_db_conn();
|
||||
let artist = crate::schema::artists::dsl::artists
|
||||
.find(artist_id)
|
||||
.first::<Artist>(db_con);
|
||||
.first::<Artist>(&mut db_conn);
|
||||
|
||||
match artist {
|
||||
Ok(_) => Ok(artist_id),
|
||||
@ -70,11 +71,12 @@ async fn validate_album_id(album_id: Field<'static>) -> BackendResult<Option<i32
|
||||
|
||||
// Parse the album id as an integer
|
||||
if let Ok(album_id) = album_id.parse::<i32>() {
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// Check if the album exists
|
||||
let db_con = &mut get_db_conn();
|
||||
let album = crate::schema::albums::dsl::albums
|
||||
.find(album_id)
|
||||
.first::<Album>(db_con);
|
||||
.first::<Album>(&mut db_conn);
|
||||
|
||||
match album {
|
||||
Ok(_) => Ok(Some(album_id)),
|
||||
@ -290,11 +292,12 @@ pub async fn upload(data: MultipartData) -> BackendResult<()> {
|
||||
image_path: None,
|
||||
};
|
||||
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// Save the song to the database
|
||||
let db_con = &mut get_db_conn();
|
||||
let song = song
|
||||
.insert_into(crate::schema::songs::table)
|
||||
.get_result::<Song>(db_con)
|
||||
.get_result::<Song>(&mut db_conn)
|
||||
.context("Error adding song to database")?;
|
||||
|
||||
// Save the song's artists to the database
|
||||
@ -313,7 +316,7 @@ pub async fn upload(data: MultipartData) -> BackendResult<()> {
|
||||
|
||||
diesel::insert_into(crate::schema::song_artists::table)
|
||||
.values(&artist_ids)
|
||||
.execute(db_con)
|
||||
.execute(&mut db_conn)
|
||||
.context("Error saving song artists to database")?;
|
||||
|
||||
Ok(())
|
||||
|
@ -1,7 +1,6 @@
|
||||
cfg_if::cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use diesel::prelude::*;
|
||||
use crate::util::database::get_db_conn;
|
||||
|
||||
use pbkdf2::{
|
||||
password_hash::{
|
||||
@ -12,6 +11,7 @@ cfg_if::cfg_if! {
|
||||
};
|
||||
|
||||
use crate::models::backend::NewUser;
|
||||
use crate::util::backend_state::BackendState;
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,12 +33,13 @@ pub struct UserCredentials {
|
||||
pub async fn find_user(username_or_email: String) -> BackendResult<Option<User>> {
|
||||
use crate::schema::users::dsl::*;
|
||||
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
// Look for either a username or email that matches the input, and return an option with None if no user is found
|
||||
let db_con = &mut get_db_conn();
|
||||
let user = users
|
||||
.filter(username.eq(username_or_email.clone()))
|
||||
.or_filter(email.eq(username_or_email))
|
||||
.first::<User>(db_con)
|
||||
.first::<User>(&mut db_conn)
|
||||
.optional()
|
||||
.context("Error loading user from database")?;
|
||||
|
||||
@ -51,10 +52,11 @@ pub async fn find_user(username_or_email: String) -> BackendResult<Option<User>>
|
||||
pub async fn find_user_by_id(user_id: i32) -> BackendResult<Option<User>> {
|
||||
use crate::schema::users::dsl::*;
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
let user = users
|
||||
.filter(id.eq(user_id))
|
||||
.first::<User>(db_con)
|
||||
.first::<User>(&mut db_conn)
|
||||
.optional()
|
||||
.context("Error loading user from database")?;
|
||||
|
||||
@ -86,11 +88,11 @@ pub async fn create_user(new_user: &NewUser) -> BackendResult<()> {
|
||||
..new_user.clone()
|
||||
};
|
||||
|
||||
let db_con = &mut get_db_conn();
|
||||
let mut db_conn = BackendState::get().await?.get_db_conn()?;
|
||||
|
||||
diesel::insert_into(users)
|
||||
.values(&new_user)
|
||||
.execute(db_con)
|
||||
.execute(&mut db_conn)
|
||||
.context("Error inserting new user into database")?;
|
||||
|
||||
Ok(())
|
||||
|
@ -7,7 +7,7 @@ use cfg_if::cfg_if;
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "ssr")] {
|
||||
use diesel::prelude::*;
|
||||
use crate::util::database::*;
|
||||
use crate::util::backend_state::PgPooledConn;
|
||||
use crate::models::backend::{Song, HistoryEntry};
|
||||
use crate::util::error::*;
|
||||
}
|
||||
|
Reference in New Issue
Block a user