-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra.rs
104 lines (98 loc) · 2.5 KB
/
extra.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::dto::{
extra::{ExtraFilter, ExtraResponseWithRelations, ExtraType},
page::Pagination,
};
use entity::{extra, image, sea_orm_active_enums::ExtraTypeEnum};
use rocket::serde::uuid::Uuid;
use sea_orm::{ColumnTrait, ConnectionTrait, DbErr, EntityTrait, QueryFilter, QueryTrait, Set};
use slug::slugify;
pub async fn create<'s, 'a, C>(
extra_name: &'s str,
disc: Option<i32>,
track: Option<i32>,
types: &Vec<ExtraType>,
package_uuid: &Uuid,
artist_uuid: &Uuid,
file_uuid: &Uuid,
connection: &'a C,
) -> Result<extra::Model, DbErr>
where
C: ConnectionTrait,
{
let creation_dto = extra::ActiveModel {
name: Set(extra_name.to_string()),
slug: Set(slugify(extra_name)),
package_id: Set(*package_uuid),
artist_id: Set(*artist_uuid),
file_id: Set(*file_uuid),
disc_index: Set(disc),
track_index: Set(track),
r#type: Set(types.iter().map(|e| ExtraTypeEnum::from(*e)).collect()),
..Default::default()
};
extra::Entity::insert(creation_dto)
.exec_with_returning(connection)
.await
}
pub async fn find<'a, C>(
uuid: &Uuid,
connection: &'a C,
) -> Result<ExtraResponseWithRelations, DbErr>
where
C: ConnectionTrait,
{
let (extra, image) = extra::Entity::find_by_id(*uuid)
.find_also_related(image::Entity)
.one(connection)
.await?
.map_or(Err(DbErr::RecordNotFound("Extra".to_string())), |r| Ok(r))?;
Ok(ExtraResponseWithRelations {
extra: extra.into(),
thumbnail: image.map(|x| x.into()),
artist: None,
package: None,
file: None,
})
}
pub async fn find_many<'a, C>(
filters: &ExtraFilter,
pagination: &Pagination,
connection: &'a C,
) -> Result<Vec<ExtraResponseWithRelations>, DbErr>
where
C: ConnectionTrait,
{
let query = extra::Entity::find()
.apply_if(filters.r#type, |q, r#type| {
//TODO
q.filter(extra::Column::Type.eq(vec![ExtraTypeEnum::from(r#type)]))
})
.apply_if(filters.artist, |q, artist_uuid| {
q.filter(extra::Column::ArtistId.eq(artist_uuid))
})
.apply_if(filters.package, |q, package_uuid| {
q.filter(extra::Column::PackageId.eq(package_uuid))
});
let mut joint_query = query
.find_also_related(image::Entity)
.cursor_by(extra::Column::Id);
if let Some(after_id) = pagination.after_id {
joint_query.after(after_id);
}
joint_query
.first(pagination.page_size)
.all(connection)
.await
.map(|items| {
items
.into_iter()
.map(|(extra, image)| ExtraResponseWithRelations {
extra: extra.into(),
thumbnail: image.map(|i| i.into()),
package: None,
artist: None,
file: None,
})
.collect()
})
}