clean up the dogs example

pull/5/head
Ondřej Hruška 5 years ago
parent f8d5445cdc
commit 4046e7f185
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 25
      examples/dog_list/main.rs

@ -2,7 +2,6 @@
#[macro_use] #[macro_use]
extern crate rocket; extern crate rocket;
use rocket::request::Form;
use rocket::response::content::Html; use rocket::response::content::Html;
use rocket::response::Redirect; use rocket::response::Redirect;
@ -30,38 +29,23 @@ fn index(session: Session) -> Html<String> {
<ul> <ul>
"#, "#,
); );
session.tap(|sess| { session.tap(|sess| {
for (n, dog) in sess.iter().enumerate() { for (n, dog) in sess.iter().enumerate() {
page.push_str(&format!( page.push_str(&format!(
r#" r#"<li>&#x1F436; {} <a href="/remove/{}">Remove</a></li>"#,
<li>&#x1F436; {} <a href="/remove/{}">Remove</a></li>
"#,
dog, n dog, n
)); ));
} }
}); });
page.push_str("</ul>");
page.push_str(
r#"
</ul>
"#,
);
Html(page) Html(page)
} }
#[derive(FromForm)]
struct AddForm {
name: String,
}
#[post("/add", data = "<dog>")] #[post("/add", data = "<dog>")]
fn add(session: Session, dog: Form<AddForm>) -> Redirect { fn add(session: Session, dog: String) -> Redirect {
session.tap(move |sess| { session.tap(move |sess| {
sess.push(dog.into_inner().name); sess.push(dog);
}); });
Redirect::found("/") Redirect::found("/")
} }
@ -72,6 +56,5 @@ fn remove(session: Session, dog: usize) -> Redirect {
sess.remove(dog); sess.remove(dog);
} }
}); });
Redirect::found("/") Redirect::found("/")
} }

Loading…
Cancel
Save