mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-04-04 09:31:05 +00:00
Implement TodoRepo trait
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@@ -2430,6 +2430,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"async-std",
|
||||
"async-trait",
|
||||
"dotenv",
|
||||
"futures",
|
||||
"paw",
|
||||
|
||||
@@ -12,3 +12,4 @@ paw = "1.0"
|
||||
sqlx = { path = "../../../", features = ["postgres", "offline", "runtime-async-std-native-tls"] }
|
||||
structopt = { version = "0.3", features = ["paw"] }
|
||||
dotenv = "0.15.0"
|
||||
async-trait = "0.1.41"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use async_trait::async_trait;
|
||||
use sqlx::postgres::PgPool;
|
||||
use sqlx::Done;
|
||||
use std::{env, sync::Arc};
|
||||
@@ -24,7 +25,7 @@ async fn main(args: Args) -> anyhow::Result<()> {
|
||||
handle_command(args, todo_repo).await
|
||||
}
|
||||
|
||||
async fn handle_command(args: Args, todo_repo: PostgresTodoRepo) -> anyhow::Result<()> {
|
||||
async fn handle_command(args: Args, todo_repo: impl TodoRepo) -> anyhow::Result<()> {
|
||||
match args.cmd {
|
||||
Some(Command::Add { description }) => {
|
||||
println!("Adding new todo with description '{}'", &description);
|
||||
@@ -48,6 +49,13 @@ async fn handle_command(args: Args, todo_repo: PostgresTodoRepo) -> anyhow::Resu
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait TodoRepo {
|
||||
async fn add_todo(&self, description: String) -> anyhow::Result<i64>;
|
||||
async fn complete_todo(&self, id: i64) -> anyhow::Result<bool>;
|
||||
async fn list_todos(&self) -> anyhow::Result<()>;
|
||||
}
|
||||
|
||||
struct PostgresTodoRepo {
|
||||
pg_pool: Arc<PgPool>,
|
||||
}
|
||||
@@ -58,7 +66,10 @@ impl PostgresTodoRepo {
|
||||
pg_pool: Arc::new(pg_pool),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl TodoRepo for PostgresTodoRepo {
|
||||
async fn add_todo(&self, description: String) -> anyhow::Result<i64> {
|
||||
let rec = sqlx::query!(
|
||||
r#"
|
||||
|
||||
Reference in New Issue
Block a user