-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperstar.rs
48 lines (38 loc) · 1.24 KB
/
superstar.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
extern crate rocket;
use rocket_contrib::{JSON, Value};
use rocket::{State};
use mysql;
use talent;
#[put("/<slug>", format = "application/json", data = "<superstar>")]
#[allow(unused_variables)]
pub fn upsert(pool: State<mysql::Pool>, slug: String, superstar: JSON<talent::Talent>) -> JSON<Value> {
let upsert = talent::upsert_superstar(pool, superstar.into_inner());
if upsert.is_ok() {
return JSON(json!({
"status": "ok",
"message": upsert.unwrap()
}))
} else {
return JSON(json!({
"status": "error",
"message": upsert.unwrap_err()
}))
}
}
#[get("/<slug>", format = "application/json")]
#[allow(unmounted_route)]
pub fn retrieve(pool: State<mysql::Pool>, slug: String) -> Option<JSON<Value>> {
let talent: Option<talent::Talent> = talent::retrieve_by_slug(pool, slug);
if talent.is_none() {
return None;
}
Some(JSON(json!(talent)))
}
#[get("/search/<term>", format = "application/json")]
pub fn search(pool: State<mysql::Pool>, term: String) -> Option<JSON<Value>> {
let results: Vec<talent::Talent> = talent::search_by_term(pool, term);
if results.len() < 1 {
return None;
}
Some(JSON(json!(results)))
}