music_player_entity/
album.rs1use music_player_types::types::{
2 Album as AlbumType, RemoteCoverUrl, RemoteTrackUrl, Song, Track as TrackType,
3};
4use sea_orm::{entity::prelude::*, ActiveValue};
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Default, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
8#[sea_orm(table_name = "album")]
9pub struct Model {
10 #[sea_orm(primary_key, auto_increment = false)]
11 pub id: String,
12 pub title: String,
13 pub artist: String,
14 pub artist_id: Option<String>,
15 pub year: Option<u32>,
16 pub cover: Option<String>,
17 #[sea_orm(ignore)]
18 pub tracks: Vec<super::track::Model>,
19}
20
21#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
22pub enum Relation {
23 #[sea_orm(has_many = "super::track::Entity")]
24 Track,
25 #[sea_orm(
26 belongs_to = "super::artist::Entity",
27 from = "Column::ArtistId",
28 to = "super::artist::Column::Id"
29 )]
30 Artist,
31}
32
33impl Related<super::track::Entity> for Entity {
34 fn to() -> RelationDef {
35 Relation::Track.def()
36 }
37}
38
39impl Related<super::artist::Entity> for Entity {
40 fn to() -> RelationDef {
41 Relation::Artist.def()
42 }
43}
44
45impl ActiveModelBehavior for ActiveModel {}
46
47impl From<&Song> for ActiveModel {
48 fn from(song: &Song) -> Self {
49 let id = format!("{:x}", md5::compute(format!("{}", song.album)));
50 Self {
51 id: ActiveValue::set(id),
52 title: ActiveValue::Set(song.album.clone()),
53 artist: ActiveValue::Set(song.album_artist.clone()),
54 artist_id: ActiveValue::Set(Some(format!(
55 "{:x}",
56 md5::compute(song.album_artist.to_owned())
57 ))),
58 year: ActiveValue::Set(song.year),
59 cover: ActiveValue::Set(song.cover.clone()),
60 }
61 }
62}
63
64impl From<AlbumType> for Model {
65 fn from(album: AlbumType) -> Self {
66 let tracks: Vec<TrackType> = album
67 .clone()
68 .tracks
69 .into_iter()
70 .map(|track| TrackType {
71 album: Some(album.clone()),
72 ..track
73 })
74 .collect();
75 Self {
76 id: album.id.clone(),
77 title: album.title,
78 cover: album.cover,
79 artist: album.artist,
80 artist_id: album.artist_id,
81 year: album.year,
82 tracks: tracks.into_iter().map(Into::into).collect(),
83 }
84 }
85}
86
87impl Into<AlbumType> for Model {
88 fn into(self) -> AlbumType {
89 AlbumType {
90 id: self.id,
91 title: self.title,
92 cover: self.cover,
93 artist: self.artist,
94 artist_id: self.artist_id,
95 year: self.year,
96 tracks: self.tracks.into_iter().map(Into::into).collect(),
97 }
98 }
99}
100
101impl RemoteCoverUrl for Model {
102 fn with_remote_cover_url(&self, base_url: &str) -> Self {
103 Self {
104 cover: self
105 .cover
106 .clone()
107 .map(|cover| format!("{}/covers/{}", base_url, cover)),
108 ..self.clone()
109 }
110 }
111}
112
113impl RemoteTrackUrl for Model {
114 fn with_remote_track_url(&self, base_url: &str) -> Self {
115 Self {
116 tracks: self
117 .tracks
118 .clone()
119 .into_iter()
120 .map(|track| track.with_remote_track_url(base_url))
121 .collect(),
122 ..self.clone()
123 }
124 }
125}