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.
This commit is contained in:
Alex Wright 2021-07-11 15:37:25 +00:00
parent b8b8533f18
commit d2eda2fc7c
2 changed files with 12 additions and 3 deletions

View File

@ -45,6 +45,10 @@ impl Database {
} }
} }
pub fn list_people(&self) -> Vec<Person> {
self.people.lock().expect("lock people").values().map(|person| person.to_owned()).collect()
}
pub fn add_person(&self, person: Person) { pub fn add_person(&self, person: Person) {
self.people.lock().expect("Couldn't lock people").insert(person.id.to_owned(), person); self.people.lock().expect("Couldn't lock people").insert(person.id.to_owned(), person);
} }

View File

@ -6,8 +6,13 @@ use crate::model::{
Person, Person,
}; };
#[rocket::get("/rest/<id>", format = "json")] #[rocket::get("/people", format = "json")]
fn get_rest(id: String, context: &State<Database>) -> Option<Json<Person>> { fn people_list(context: &State<Database>) -> Json<Vec<Person>> {
Json(context.list_people())
}
#[rocket::get("/people/<id>", format = "json")]
fn people_detail(id: String, context: &State<Database>) -> Option<Json<Person>> {
match context.get_person(&id) { match context.get_person(&id) {
None => None, None => None,
Some(person) => Some(Json(person)) Some(person) => Some(Json(person))
@ -17,6 +22,6 @@ fn get_rest(id: String, context: &State<Database>) -> Option<Json<Person>> {
pub fn stage() -> AdHoc { pub fn stage() -> AdHoc {
AdHoc::on_ignite("Add REST endpoint", |rocket| async { AdHoc::on_ignite("Add REST endpoint", |rocket| async {
rocket rocket
.mount("/", rocket::routes![get_rest]) .mount("/", rocket::routes![people_detail, people_list])
}) })
} }