-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcourses.rs
64 lines (56 loc) · 1.6 KB
/
courses.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
use super::*;
#[derive(Deserialize)]
pub(crate) struct GetCoursesParams {
limit: Option<i64>,
offset: Option<u64>,
with_course_count: Option<bool>,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub(crate) struct GetCoursesPayload {
pub(crate) courses: Vec<Course>,
pub(crate) course_count: Option<u64>,
}
pub(crate) async fn get_courses(
Query(params): Query<GetCoursesParams>,
AppState(db): AppState<Arc<Db>>,
filter: Json<CourseFilter>,
) -> Result<impl IntoResponse> {
Ok(Json(GetCoursesPayload {
courses: db
.courses(params.limit, params.offset, Some(filter.0))
.await?,
course_count: if params.with_course_count.unwrap_or(false) {
Some(db.course_count().await?)
} else {
None
},
}))
}
#[derive(Debug, Deserialize)]
pub(crate) struct GetCourseParams {
with_reviews: Option<bool>,
}
pub(crate) async fn get_course_by_id(
Path(id): Path<String>,
Query(params): Query<GetCourseParams>,
AppState(state): AppState<State>,
) -> Result<impl IntoResponse> {
Ok(match state.db.find_course_by_id(&id).await? {
Some(course) => {
let mut reviews = params
.with_reviews
.unwrap_or(false)
.then_some(state.db.find_reviews_by_course_id(&id).await?);
if let Some(ref mut reviews) = reviews {
reviews.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));
return Ok((
StatusCode::OK,
Json(Some(json!({ "course": course, "reviews": reviews }))),
));
}
(StatusCode::OK, Json(Some(json!(course))))
}
None => (StatusCode::NOT_FOUND, Json(None)),
})
}