Skip to content

Instantly share code, notes, and snippets.

@sean3z
Last active April 6, 2018 02:26
Show Gist options
  • Save sean3z/d39354b172669c235845c87d04a8bca9 to your computer and use it in GitHub Desktop.
Save sean3z/d39354b172669c235845c87d04a8bca9 to your computer and use it in GitHub Desktop.
Updating our routes to use real CRUD operations
#[post("/", data = "<hero>")]
fn create(hero: Json<Hero>, connection: db::Connection) -> Json<Hero> {
let insert = Hero { id: None, ..hero.into_inner() };
Json(Hero::create(insert, &connection))
}
#[get("/")]
fn read(connection: db::Connection) -> Json<Value> {
Json(json!(Hero::read(&connection)))
}
#[put("/<id>", data = "<hero>")]
fn update(id: i32, hero: Json<Hero>, connection: db::Connection) -> Json<Value> {
let update = Hero { id: Some(id), ..hero.into_inner() };
Json(json!({
"success": Hero::update(id, update, &connection)
}))
}
#[delete("/<id>")]
fn delete(id: i32, connection: db::Connection) -> Json<Value> {
Json(json!({
"success": Hero::delete(id, &connection)
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment