Skip to content

Commit 09a81fc

Browse files
committed
update
1 parent 77492d1 commit 09a81fc

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

crates/rs-tauri-vue/src-tauri/migrations/20221029182115_init.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS todos
55
description TEXT NOT NULL,
66
done BOOLEAN NOT NULL DEFAULT 0,
77
title VARCHAR(200) NOT NULL,
8-
completed BOOLEAN DEFAULT FALSE
8+
completed BOOLEAN NOT NULL DEFAULT 0
99
);
1010

1111
--

crates/rs-tauri-vue/src-tauri/src/storage/sql/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl AppSqlStorage {
7878

7979
match m.run(&self.db).await {
8080
Ok(_) => {
81-
info!("migrations run success");
81+
println!("migrations run success");
8282
},
8383
Err(e) => {
8484
panic!("migrations run error: {:?}", e);

crates/rs-tauri-vue/src-tauri/src/storage/sql/todo.rs

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,65 @@ impl AppSqlStorage {
99
// Insert the task, then obtain the ID of this row
1010
let id = sqlx::query!(
1111
r#"
12-
INSERT INTO todos ( description )
13-
VALUES ( ?1 )
12+
INSERT INTO todos ( description, title )
13+
VALUES ( ?1, ?2 )
1414
"#,
15-
todo.description
15+
todo.description,
16+
todo.title
1617
)
1718
.execute(&mut conn)
1819
.await?
1920
.last_insert_rowid();
2021

2122
Ok(id)
2223
}
24+
25+
pub async fn list_todo(&self) -> anyhow::Result<Vec<TodoEntity>> {
26+
let mut conn = self.db.acquire().await?;
27+
28+
let rows = sqlx::query!(
29+
r#"
30+
SELECT id, description, title, done, completed
31+
FROM todos
32+
ORDER BY id
33+
"#
34+
)
35+
.fetch_all(&mut conn)
36+
.await?;
37+
38+
let mut todos = Vec::new();
39+
for row in rows {
40+
println!("row: {:?}", row);
41+
todos.push(TodoEntity {
42+
id: row.id,
43+
description: row.description,
44+
title: row.title,
45+
done: row.done,
46+
completed: row.completed,
47+
});
48+
}
49+
50+
Ok(todos)
51+
}
52+
53+
pub async fn get_todo(&self, id: i64) -> anyhow::Result<TodoEntity> {
54+
let mut conn = self.db.acquire().await?;
55+
56+
let row =
57+
sqlx::query!(r#"SELECT id, title, description, completed FROM todos WHERE id = ?"#, id)
58+
.fetch_one(&mut conn)
59+
.await?;
60+
61+
println!("get_todo row: {:?}", row);
62+
63+
Ok(TodoEntity {
64+
id: row.id,
65+
description: row.description,
66+
title: row.title,
67+
done: false,
68+
completed: row.completed,
69+
})
70+
}
2371
}
2472

2573
#[cfg(test)]
@@ -33,4 +81,51 @@ mod test {
3381
db.init_migrations().await;
3482
db
3583
}
84+
85+
#[tokio::test]
86+
async fn test_add_todo() {
87+
let db = setup().await;
88+
let todo = TodoEntity {
89+
id: 0,
90+
title: "test title".to_string(),
91+
description: "test desc".to_string(),
92+
done: false,
93+
completed: false,
94+
};
95+
let id = db.add_todo(&todo).await.unwrap();
96+
assert!(id > 0);
97+
}
98+
99+
#[tokio::test]
100+
async fn test_get_todo() {
101+
let db = setup().await;
102+
let todo = TodoEntity {
103+
id: 0,
104+
title: "get todo test".to_string(),
105+
description: "test desc".to_string(),
106+
done: false,
107+
completed: false,
108+
};
109+
let id = db.add_todo(&todo).await.unwrap();
110+
let todo = db.get_todo(id).await.unwrap();
111+
assert_eq!(todo.description, "test desc");
112+
}
113+
114+
#[tokio::test]
115+
async fn test_list_todo() {
116+
let db = setup().await;
117+
let todo = TodoEntity {
118+
id: 0,
119+
title: "test title".to_string(),
120+
description: "test desc".to_string(),
121+
done: false,
122+
completed: false,
123+
};
124+
let id = db.add_todo(&todo).await.unwrap();
125+
assert!(id > 0);
126+
127+
let todos = db.list_todo().await.unwrap();
128+
129+
println!("todos: {:?}", todos)
130+
}
36131
}

0 commit comments

Comments
 (0)