-
Notifications
You must be signed in to change notification settings - Fork 7
/
todo.rs
273 lines (245 loc) · 8.24 KB
/
todo.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
pub mod status;
use crate::model::todo::{InsertTodo, StoredTodo, UpdateStoredTodo, UpsertStoredTodo};
use crate::repository::DatabaseRepositoryImpl;
use async_trait::async_trait;
use sqlx::{query, query_as};
use todo_kernel::model::todo::status::TodoStatus;
use todo_kernel::model::todo::{NewTodo, Todo, UpdateTodo, UpsertTodo};
use todo_kernel::model::Id;
use todo_kernel::repository::todo::TodoRepository;
#[async_trait]
impl TodoRepository for DatabaseRepositoryImpl<Todo> {
async fn get(&self, id: &Id<Todo>) -> anyhow::Result<Option<Todo>> {
let pool = self.db.0.clone();
let sql = r#"
select
t.id as id,
t.title as title,
t.description as description,
ts.id as status_id,
ts.code as status_code,
ts.name as status_name,
t.created_at as created_at,
t.updated_at as updated_at
from
todos as t
inner join
todo_statuses as ts
on ts.id = t.status_id
where
t.id = $1
"#;
let stored_todo = query_as::<_, StoredTodo>(sql)
.bind(id.value.to_string())
.fetch_one(&*pool)
.await
.ok();
match stored_todo {
Some(st) => Ok(Some(st.try_into()?)),
None => Ok(None),
}
}
async fn find(&self, status: Option<TodoStatus>) -> anyhow::Result<Option<Vec<Todo>>> {
let pool = self.db.0.clone();
let where_status = if let Some(s) = &status {
s.id.value.to_string()
} else {
"".to_string()
};
let mut sql = r#"
select
t.id as id,
t.title as title,
t.description as description,
ts.id as status_id,
ts.code as status_code,
ts.name as status_name,
t.created_at as created_at,
t.updated_at as updated_at
from
todos as t
inner join
todo_statuses as ts
on ts.id = t.status_id
where t.status_id in ($1)
order by t.created_at asc
"#
.to_string();
if status.is_none() {
sql = sql.replace("$1", "select id from todo_statuses");
}
let stored_todo_list = query_as::<_, StoredTodo>(&sql)
.bind(where_status)
.fetch_all(&*pool)
.await
.ok();
match stored_todo_list {
Some(todo_list) => {
let todos = todo_list.into_iter().flat_map(|st| st.try_into()).collect();
Ok(Some(todos))
}
None => Ok(None),
}
}
async fn insert(&self, source: NewTodo) -> anyhow::Result<Todo> {
let pool = self.db.0.clone();
let todo: InsertTodo = source.into();
let id = todo.id.clone();
let _ = query("insert into todos (id, title, description) values ($1, $2, $3)")
.bind(todo.id)
.bind(todo.title)
.bind(todo.description)
.execute(&*pool)
.await?;
let sql = r#"
select
t.id as id,
t.title as title,
t.description as description,
ts.id as status_id,
ts.code as status_code,
ts.name as status_name,
t.created_at as created_at,
t.updated_at as updated_at
from
todos as t
inner join
todo_statuses as ts
on ts.id = t.status_id
where
t.id = $1
"#;
let stored_todo = query_as::<_, StoredTodo>(sql)
.bind(id)
.fetch_one(&*pool)
.await?;
Ok(stored_todo.try_into()?)
}
async fn update(&self, source: UpdateTodo) -> anyhow::Result<Todo> {
let pool = self.db.0.clone();
let todo: UpdateStoredTodo = source.into();
let id = todo.id.clone();
let update_sql = r#"
update
todos as target
set
title = case when $2 is not null then $2 else current_todo.title end,
description = case when $3 is not null then $3 else current_todo.description end,
status_id = case when $4 is not null then $4 else current_todo.status_id end,
updated_at = current_timestamp
from
(select * from todos where id = $1) as current_todo
where
target.id = $1
"#;
let _ = query(update_sql)
.bind(todo.id)
.bind(todo.title)
.bind(todo.description)
.bind(todo.status_id)
.execute(&*pool)
.await?;
let sql = r#"
select
t.id as id,
t.title as title,
t.description as description,
ts.id as status_id,
ts.code as status_code,
ts.name as status_name,
t.created_at as created_at,
t.updated_at as updated_at
from
todos as t
inner join
todo_statuses as ts
on ts.id = t.status_id
where
t.id = $1
"#;
let stored_todo = query_as::<_, StoredTodo>(sql)
.bind(id)
.fetch_one(&*pool)
.await?;
Ok(stored_todo.try_into()?)
}
async fn upsert(&self, source: UpsertTodo) -> anyhow::Result<Todo> {
let pool = self.db.0.clone();
let todo: UpsertStoredTodo = source.into();
let id = todo.id.clone();
let upsert_sql = r#"
insert into todos (id, title, description, status_id) values ($1, $2, $3, $4)
on conflict on constraint pk_todos_id
do update set title = $2, description = $3, status_id = $4, updated_at = current_timestamp
"#;
let _ = query(upsert_sql)
.bind(todo.id)
.bind(todo.title)
.bind(todo.description)
.bind(todo.status_id)
.execute(&*pool)
.await?;
let sql = r#"
select
t.id as id,
t.title as title,
t.description as description,
ts.id as status_id,
ts.code as status_code,
ts.name as status_name,
t.created_at as created_at,
t.updated_at as updated_at
from
todos as t
inner join
todo_statuses as ts
on ts.id = t.status_id
where
t.id = $1
"#;
let stored_todo = query_as::<_, StoredTodo>(sql)
.bind(id)
.fetch_one(&*pool)
.await?;
Ok(stored_todo.try_into()?)
}
async fn delete(&self, id: &Id<Todo>) -> anyhow::Result<Option<Todo>> {
let pool = self.db.0.clone();
let sql = r#"
select
t.id as id,
t.title as title,
t.description as description,
ts.id as status_id,
ts.code as status_code,
ts.name as status_name,
t.created_at as created_at,
t.updated_at as updated_at
from
todos as t
inner join
todo_statuses as ts
on ts.id = t.status_id
where
t.id = $1
"#;
let stored_todo = query_as::<_, StoredTodo>(sql)
.bind(id.value.to_string())
.fetch_one(&*pool)
.await
.ok();
match stored_todo {
Some(st) => {
let delete_sql = r#"
delete from todos where id = $1
"#;
let _ = query(delete_sql)
.bind(id.value.to_string())
.execute(&*pool)
.await?;
Ok(Some(st.try_into()?))
}
None => Ok(None),
}
}
}