From d2eda2fc7cfc28c634dfb44e210bdeaf0bc04221 Mon Sep 17 00:00:00 2001 From: Alex Wright Date: Sun, 11 Jul 2021 15:37:25 +0000 Subject: [PATCH] List view! The chain of lock().unwrap().values().map(closure).to_owned().collect() is pretty gross, I think, but it went together pretty smoothly. Was expecting more head bashing. --- src/model.rs | 4 ++++ src/rest.rs | 11 ++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) 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