-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.rs
56 lines (48 loc) · 1.3 KB
/
list.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
use actix_web::{get, web, Responder, Result};
use lazy_static::__Deref;
use serde::Serialize;
use crate::app_state::AppState;
// TODO fill this out
#[derive(Debug, Serialize)]
struct ApiCategory {
name: String,
}
#[derive(Debug, Serialize)]
pub struct ApiTrack {
id: String,
title: String,
artist: Option<String>,
album: Option<String>,
disc: Option<String>,
category: String,
}
impl ApiTrack {
pub fn from_track(track: &crate::filemanager::model::Track) -> Self {
ApiTrack {
id: track.id.clone(),
title: track.name.clone(),
artist: track.artist.clone(),
album: track.album.clone(),
disc: track.disc.clone(),
category: track.category.clone(),
}
}
}
#[derive(Debug, Serialize)]
struct ListCategoriesResponse {
categories: Vec<ApiCategory>,
}
#[get("/categories")]
pub async fn list_categories(data: web::Data<AppState>) -> Result<impl Responder> {
let collection = data.collection.deref();
let api_categories: Vec<ApiCategory> = collection
.values()
.map(|category| ApiCategory {
name: category.name.clone(),
})
.collect();
let response = ListCategoriesResponse {
categories: api_categories,
};
Ok(web::Json(response))
}