Last active
April 6, 2018 02:26
-
-
Save sean3z/d39354b172669c235845c87d04a8bca9 to your computer and use it in GitHub Desktop.
Updating our routes to use real CRUD operations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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