updates
This commit is contained in:
parent
292ff60720
commit
fa8ed2b599
19 changed files with 349 additions and 216 deletions
|
@ -1,43 +1,43 @@
|
|||
use crate::helpers::db;
|
||||
use crate::models::tracks::{Tracks, TracksWithArtists};
|
||||
use crate::models::user::Users;
|
||||
use crate::schema::playlists;
|
||||
use crate::schema::{playlists, playlists_tracks};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::result::Error;
|
||||
use diesel::{
|
||||
AsChangeset, ExpressionMethods, Insertable, QueryDsl, Queryable, RunQueryDsl, Selectable,
|
||||
};
|
||||
use diesel::{prelude::*, result::Error};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
use super::artists_tracks::{ArtistTracks, TrackArtist};
|
||||
use super::{playlists_tracks::PlaylistTracks, tracks::Tracks, users::Users};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct PlaylistCreator {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(AsChangeset, Insertable, Queryable, Selectable, Deserialize, Serialize)]
|
||||
#[diesel(table_name = crate::schema::playlists)]
|
||||
#[diesel(belongs_to(Users))]
|
||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||
pub struct Playlist {
|
||||
#[derive(
|
||||
AsChangeset, Debug, Deserialize, Identifiable, Queryable, Selectable, Serialize, PartialEq,
|
||||
)]
|
||||
#[diesel(table_name = playlists)]
|
||||
pub struct Playlists {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub public: bool,
|
||||
|
||||
pub playlist_type: String,
|
||||
pub parent_id: Option<String>,
|
||||
|
||||
pub creator_id: String,
|
||||
|
||||
pub created_at: Option<NaiveDateTime>,
|
||||
pub updated_at: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Queryable, Serialize)]
|
||||
pub struct Playlists {
|
||||
pub id: String,
|
||||
#[derive(Debug, Deserialize, Insertable)]
|
||||
#[diesel(table_name = playlists)]
|
||||
pub struct NewPlaylist {
|
||||
pub name: String,
|
||||
pub public: bool,
|
||||
// pub playlist_type: String,
|
||||
pub creator_id: String,
|
||||
|
||||
pub created_at: Option<NaiveDateTime>,
|
||||
pub updated_at: Option<NaiveDateTime>,
|
||||
}
|
||||
|
||||
impl Playlists {
|
||||
|
@ -59,23 +59,26 @@ impl Playlists {
|
|||
let conn = &mut db::connection()?;
|
||||
|
||||
let mut playlists = playlists::table
|
||||
.distinct()
|
||||
.select(Playlists::as_select())
|
||||
.order((playlists::playlist_type.desc(), playlists::parent_id.desc()))
|
||||
.filter(playlists::creator_id.eq(user_id))
|
||||
.into_boxed();
|
||||
|
||||
if filter_public {
|
||||
playlists = playlists.filter(playlists::public.eq(true));
|
||||
playlists = playlists.filter(
|
||||
playlists::public
|
||||
.eq(true)
|
||||
.and(playlists::playlist_type.ne("folder")),
|
||||
);
|
||||
}
|
||||
|
||||
let playlists = playlists.get_results(conn)?;
|
||||
Ok(playlists)
|
||||
}
|
||||
|
||||
pub fn get_tracks(&self) -> Result<Vec<TracksWithArtists>, Error> {
|
||||
let tracks = Tracks::find_by_playlist(&self.id)?;
|
||||
let tracks: Vec<TracksWithArtists> = tracks
|
||||
.into_iter()
|
||||
.map(Tracks::with_artists)
|
||||
.collect::<Result<Vec<TracksWithArtists>, _>>()?;
|
||||
pub fn get_tracks(&self) -> Result<Vec<PlaylistTrack>, Error> {
|
||||
let tracks: Vec<PlaylistTrack> = PlaylistTracks::get_tracks(self)?;
|
||||
|
||||
Ok(tracks)
|
||||
}
|
||||
|
@ -84,34 +87,69 @@ impl Playlists {
|
|||
let creator = Users::find(&self.creator_id)?;
|
||||
Ok(creator)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Insertable, Deserialize)]
|
||||
#[diesel(table_name = playlists)]
|
||||
pub struct NewPlaylist {
|
||||
pub name: String,
|
||||
pub public: bool,
|
||||
pub creator_id: Option<String>,
|
||||
}
|
||||
|
||||
impl Playlist {
|
||||
pub fn create(name: &str, public: bool, creator_id: &str) -> NewPlaylist {
|
||||
NewPlaylist {
|
||||
name: name.to_string(),
|
||||
public,
|
||||
creator_id: Some(creator_id.to_string()),
|
||||
pub fn get_data(&self, tracks: &[PlaylistTrack]) -> (usize, usize) {
|
||||
if tracks.is_empty() {
|
||||
return (0, 0);
|
||||
}
|
||||
|
||||
let duration = tracks
|
||||
.iter()
|
||||
.map(|track| track.duration_ms)
|
||||
.reduce(|a, b| a + b)
|
||||
.unwrap() as usize;
|
||||
|
||||
(duration, tracks.len())
|
||||
}
|
||||
|
||||
fn from(playlist: Playlist) -> Playlist {
|
||||
Playlist {
|
||||
id: playlist.id,
|
||||
name: playlist.name,
|
||||
public: playlist.public,
|
||||
creator_id: playlist.creator_id,
|
||||
pub fn can_see(&self, creator: Option<Users>) -> bool {
|
||||
self.public || creator.map_or(false, |user| user.id == self.creator_id)
|
||||
}
|
||||
}
|
||||
|
||||
created_at: playlist.created_at,
|
||||
updated_at: playlist.updated_at,
|
||||
// // Folder
|
||||
// struct Folder {
|
||||
// id: String,
|
||||
// name: String,
|
||||
// playlists: Vec<Playlists>,
|
||||
// }
|
||||
|
||||
// impl Folder {
|
||||
// pub fn get_playlists(&self) -> Result<Vec<Playlists>, Error> {
|
||||
// let playlists = playlists::table
|
||||
// }
|
||||
// }
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct PlaylistTrack {
|
||||
pub id: String,
|
||||
pub title: String,
|
||||
pub duration_ms: i32,
|
||||
|
||||
pub artists: Vec<TrackArtist>,
|
||||
pub added_at: NaiveDateTime,
|
||||
pub spotify_id: Option<String>,
|
||||
pub tidal_id: Option<String>,
|
||||
}
|
||||
|
||||
impl From<Tracks> for PlaylistTrack {
|
||||
fn from(track: Tracks) -> Self {
|
||||
let artists = ArtistTracks::get_artists(&track).unwrap();
|
||||
|
||||
let added_at = playlists_tracks::table
|
||||
.filter(playlists_tracks::track_id.eq(&track.id))
|
||||
.select(playlists_tracks::added_at)
|
||||
.first::<NaiveDateTime>(&mut db::connection().unwrap())
|
||||
.unwrap();
|
||||
|
||||
PlaylistTrack {
|
||||
id: track.id,
|
||||
title: track.title,
|
||||
duration_ms: track.duration_ms,
|
||||
artists,
|
||||
added_at,
|
||||
spotify_id: track.spotify_id,
|
||||
tidal_id: track.tidal_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue