ouverture_core/database/
setup.rs1use sea_orm::sea_query::{ColumnDef, TableCreateStatement};
2use sea_orm::{error::*, sea_query, ConnectionTrait, DbConn, ExecResult};
3
4use crate::music::song::{Song, SongSource};
5use sea_orm::prelude::*;
6use sea_orm::{entity::*, query::*};
7
8use color_eyre::Result;
9
10#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
11#[sea_orm(table_name = "songs")]
12pub struct Model {
13 #[sea_orm(primary_key)]
14 pub id: i32,
15 pub title: Option<String>,
16 pub artist: Option<String>,
17 pub album: Option<String>,
18 pub source: Option<String>,
19}
20#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
21pub enum Relation {}
22
23impl ActiveModelBehavior for ActiveModel {}
24
25async fn create_table(db: &DbConn, stmt: &TableCreateStatement) -> Result<ExecResult, DbErr> {
26 let builder = db.get_database_backend();
27 db.execute(builder.build(stmt)).await
28}
29
30pub async fn create_post_table(db: &DbConn) -> Result<ExecResult, DbErr> {
31 let stmt = sea_query::Table::create()
32 .table(Entity)
33 .if_not_exists()
34 .col(
35 ColumnDef::new(Column::Id)
36 .integer()
37 .not_null()
38 .auto_increment()
39 .primary_key(),
40 )
41 .col(ColumnDef::new(Column::Title).string())
42 .col(ColumnDef::new(Column::Artist).string())
43 .col(ColumnDef::new(Column::Album).string())
44 .col(ColumnDef::new(Column::Source).string())
45 .to_owned();
46
47 create_table(db, &stmt).await
48}
49
50impl From<Song> for ActiveModel {
51 fn from(s: Song) -> ActiveModel {
52 ActiveModel {
53 title: Set(s.title),
54 artist: Set(s.artist),
55 album: Set(s.album),
56 source: Set(match s.source {
57 None => None,
58 Some(source) => Some(source.into()),
59 }),
60 ..Default::default()
61 }
62 }
63}
64
65impl From<Model> for Song {
66 fn from(a: Model) -> Song {
67 Song {
68 title: a.title.into(),
69 artist: a.artist.into(),
70 album: a.album.into(),
71 source: match a.source {
72 None => None,
73 Some(source) => Some(source.into()),
74 },
75 ..Default::default()
76 }
77 }
78}