Skip to content

Instantly share code, notes, and snippets.

@sean3z
Last active April 3, 2018 17:05
Show Gist options
  • Save sean3z/89c40ac39e52e1847c124cd2fc578ae0 to your computer and use it in GitHub Desktop.
Save sean3z/89c40ac39e52e1847c124cd2fc578ae0 to your computer and use it in GitHub Desktop.
Hero implementation
impl Hero {
pub fn create(hero: Hero, connection: &MysqlConnection) -> Hero {
diesel::insert_into(heroes::table)
.values(&hero)
.execute(connection)
.expect("Error creating new hero");
heroes::table.order(heroes::id.desc()).first(connection).unwrap()
}
pub fn read(connection: &MysqlConnection) -> Vec<Hero> {
heroes::table.order(heroes::id.asc()).load::<Hero>(connection).unwrap()
}
pub fn update(id: i32, hero: Hero, connection: &MysqlConnection) -> bool {
diesel::update(heroes::table.find(id)).set(&hero).execute(connection).is_ok()
}
pub fn delete(id: i32, connection: &MysqlConnection) -> bool {
diesel::delete(heroes::table.find(id)).execute(connection).is_ok()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment