diff --git a/src/model.rs b/src/model.rs index 398cf03..fa4785c 100644 --- a/src/model.rs +++ b/src/model.rs @@ -45,6 +45,10 @@ impl Database { } } + pub fn list_people(&self) -> Vec { + self.people.lock().expect("lock people").values().map(|person| person.to_owned()).collect() + } + pub fn add_person(&self, person: Person) { self.people.lock().expect("Couldn't lock people").insert(person.id.to_owned(), person); } diff --git a/src/rest.rs b/src/rest.rs index 5c70bda..5bd7518 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -6,8 +6,13 @@ use crate::model::{ Person, }; -#[rocket::get("/rest/", format = "json")] -fn get_rest(id: String, context: &State) -> Option> { +#[rocket::get("/people", format = "json")] +fn people_list(context: &State) -> Json> { + Json(context.list_people()) +} + +#[rocket::get("/people/", format = "json")] +fn people_detail(id: String, context: &State) -> Option> { match context.get_person(&id) { None => None, Some(person) => Some(Json(person)) @@ -17,6 +22,6 @@ fn get_rest(id: String, context: &State) -> Option> { pub fn stage() -> AdHoc { AdHoc::on_ignite("Add REST endpoint", |rocket| async { rocket - .mount("/", rocket::routes![get_rest]) + .mount("/", rocket::routes![people_detail, people_list]) }) } \ No newline at end of file