@@ -9,17 +9,65 @@ impl AppSqlStorage {
9
9
// Insert the task, then obtain the ID of this row
10
10
let id = sqlx:: query!(
11
11
r#"
12
- INSERT INTO todos ( description )
13
- VALUES ( ?1 )
12
+ INSERT INTO todos ( description, title )
13
+ VALUES ( ?1, ?2 )
14
14
"# ,
15
- todo. description
15
+ todo. description,
16
+ todo. title
16
17
)
17
18
. execute ( & mut conn)
18
19
. await ?
19
20
. last_insert_rowid ( ) ;
20
21
21
22
Ok ( id)
22
23
}
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
+ }
23
71
}
24
72
25
73
#[ cfg( test) ]
@@ -33,4 +81,51 @@ mod test {
33
81
db. init_migrations ( ) . await ;
34
82
db
35
83
}
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
+ }
36
131
}
0 commit comments