Refactor example

This commit is contained in:
Chris Tsang 2021-06-16 17:28:52 +08:00
parent 97faaf0f34
commit d8902ba979

View File

@ -76,6 +76,12 @@ async fn find_together(db: &Database) -> Result<(), QueryErr> {
Ok(()) Ok(())
} }
impl Cake {
fn find_by_name(name: &str) -> Select<Self> {
Self::find().filter(cake::Column::Name.contains(name))
}
}
async fn find_one(db: &Database) -> Result<(), QueryErr> { async fn find_one(db: &Database) -> Result<(), QueryErr> {
print!("find one by primary key: "); print!("find one by primary key: ");
@ -86,12 +92,9 @@ async fn find_one(db: &Database) -> Result<(), QueryErr> {
println!("{:?}", cheese); println!("{:?}", cheese);
println!(); println!();
print!("find one by like: "); print!("find one by name: ");
let chocolate = Cake::find() let chocolate = Cake::find_by_name("chocolate").one(db).await?;
.filter(cake::Column::Name.contains("chocolate"))
.one(db)
.await?;
println!(); println!();
println!("{:?}", chocolate); println!("{:?}", chocolate);