| 1 | |
|
| 2 | |
|
| 3 | | use super::{now_ms, Pool}; |
| 4 | | use sqlx::FromRow; |
| 5 | |
|
| 6 | | #[derive(Clone, Debug, FromRow)] |
| 7 | | pub struct IssueRow { |
| 8 | | pub id: i64, |
| 9 | | pub repo_id: i64, |
| 10 | | pub number: i64, |
| 11 | | pub title: String, |
| 12 | | pub body: String, |
| 13 | | pub author_id: Option<i64>, |
| 14 | | pub author: String, |
| 15 | | pub state: String, |
| 16 | | pub reason: String, |
| 17 | | pub locked: bool, |
| 18 | | pub pinned: bool, |
| 19 | | pub milestone_id: Option<i64>, |
| 20 | | pub created_ms: i64, |
| 21 | | pub updated_ms: i64, |
| 22 | | pub closed_ms: Option<i64>, |
| 23 | | } |
| 24 | |
|
| 25 | | #[derive(Clone, Debug, FromRow)] |
| 26 | | pub struct IssueComment { |
| 27 | | pub id: i64, |
| 28 | | pub issue_id: i64, |
| 29 | | pub author_id: Option<i64>, |
| 30 | | pub author: String, |
| 31 | | pub body: String, |
| 32 | | pub created_ms: i64, |
| 33 | | pub edited_ms: Option<i64>, |
| 34 | | } |
| 35 | |
|
| 36 | | #[derive(Clone, Debug, FromRow)] |
| 37 | | pub struct IssueEvent { |
| 38 | | pub id: i64, |
| 39 | | pub at_ms: i64, |
| 40 | | pub actor: String, |
| 41 | | pub event: String, |
| 42 | | pub detail: String, |
| 43 | | } |
| 44 | |
|
| 45 | | #[derive(Clone, Debug, FromRow, PartialEq, Eq)] |
| 46 | | pub struct Label { |
| 47 | | pub id: i64, |
| 48 | | pub repo_id: i64, |
| 49 | | pub name: String, |
| 50 | | pub colour: String, |
| 51 | | pub description: String, |
| 52 | | } |
| 53 | |
|
| 54 | | #[derive(Clone, Debug, FromRow)] |
| 55 | | pub struct Milestone { |
| 56 | | pub id: i64, |
| 57 | | pub repo_id: i64, |
| 58 | | pub title: String, |
| 59 | | pub description: String, |
| 60 | | pub due_ms: Option<i64>, |
| 61 | | pub state: String, |
| 62 | | } |
| 63 | |
|
| 64 | | pub struct NewIssue<'a> { |
| 65 | | pub repo_id: i64, |
| 66 | | pub title: &'a str, |
| 67 | | pub body: &'a str, |
| 68 | | pub author_id: Option<i64>, |
| 69 | | pub author: &'a str, |
| 70 | | } |
| 71 | |
|
| 72 | 66× | pub async fn create(pool: &Pool, i: NewIssue<'_>) -> sqlx::Result<IssueRow> { |
| 73 | 66× | let number = super::proposals::next_number(pool, i.repo_id).await?; |
| 74 | 66× | let now = now_ms(); |
| 75 | 66× | sqlx::query_as("INSERT INTO issues (repo_id, number, title, body, author_id, author, created_ms, updated_ms) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *") |
| 76 | 66× | .bind(i.repo_id) |
| 77 | 66× | .bind(number) |
| 78 | 66× | .bind(i.title) |
| 79 | 66× | .bind(i.body) |
| 80 | 66× | .bind(i.author_id) |
| 81 | 66× | .bind(i.author) |
| 82 | 66× | .bind(now) |
| 83 | 66× | .bind(now) |
| 84 | 66× | .fetch_one(pool) |
| 85 | 66× | .await |
| 86 | 66× | } |
| 87 | |
|
| 88 | 124× | pub async fn get(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Option<IssueRow>> { |
| 89 | 124× | sqlx::query_as("SELECT * FROM issues WHERE repo_id = $1 AND number = $2").bind(repo_id).bind(number).fetch_optional(pool).await |
| 90 | 124× | } |
| 91 | |
|
| 92 | 0× | pub async fn get_by_id(pool: &Pool, id: i64) -> sqlx::Result<Option<IssueRow>> { |
| 93 | 0× | sqlx::query_as("SELECT * FROM issues WHERE id = $1").bind(id).fetch_optional(pool).await |
| 94 | 0× | } |
| 95 | |
|
| 96 | 60× | pub async fn list(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<IssueRow>> { |
| 97 | 60× | sqlx::query_as("SELECT * FROM issues WHERE repo_id = $1 ORDER BY pinned DESC, number DESC").bind(repo_id).fetch_all(pool).await |
| 98 | 60× | } |
| 99 | |
|
| 100 | |
|
| 101 | 395× | pub async fn numbers(pool: &Pool, repo_id: i64) -> sqlx::Result<(Vec<i64>, Vec<i64>)> { |
| 102 | 395× | let issues: Vec<i64> = sqlx::query_scalar("SELECT number FROM issues WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?; |
| 103 | 395× | let proposals: Vec<i64> = sqlx::query_scalar("SELECT number FROM proposals WHERE repo_id = $1").bind(repo_id).fetch_all(pool).await?; |
| 104 | 395× | Ok((issues, proposals)) |
| 105 | 395× | } |
| 106 | |
|
| 107 | 0× | pub async fn assigned_to(pool: &Pool, account_id: i64) -> sqlx::Result<Vec<IssueRow>> { |
| 108 | 0× | sqlx::query_as("SELECT i.* FROM issues i JOIN issue_assignees a ON a.issue_id = i.id WHERE a.account_id = $1 AND i.state = 'open' ORDER BY i.updated_ms DESC") |
| 109 | 0× | .bind(account_id) |
| 110 | 0× | .fetch_all(pool) |
| 111 | 0× | .await |
| 112 | 0× | } |
| 113 | |
|
| 114 | 2× | pub async fn edit(pool: &Pool, id: i64, title: &str, body: &str) -> sqlx::Result<()> { |
| 115 | 2× | sqlx::query("UPDATE issues SET title = $1, body = $2, updated_ms = $3 WHERE id = $4") |
| 116 | 2× | .bind(title) |
| 117 | 2× | .bind(body) |
| 118 | 2× | .bind(now_ms()) |
| 119 | 2× | .bind(id) |
| 120 | 2× | .execute(pool) |
| 121 | 2× | .await?; |
| 122 | 2× | Ok(()) |
| 123 | 2× | } |
| 124 | |
|
| 125 | 11× | pub async fn set_state(pool: &Pool, id: i64, state: &str, reason: &str) -> sqlx::Result<()> { |
| 126 | 11× | let now = now_ms(); |
| 127 | 11× | let closed: Option<i64> = if state == "closed" { Some(now) } else { None }; |
| 128 | 11× | sqlx::query("UPDATE issues SET state = $1, reason = $2, closed_ms = $3, updated_ms = $4 WHERE id = $5") |
| 129 | 11× | .bind(state) |
| 130 | 11× | .bind(reason) |
| 131 | 11× | .bind(closed) |
| 132 | 11× | .bind(now) |
| 133 | 11× | .bind(id) |
| 134 | 11× | .execute(pool) |
| 135 | 11× | .await?; |
| 136 | 11× | if state == "closed" { |
| 137 | 11× | finished(pool, "issues", id).await?; |
| 138 | 0× | } |
| 139 | 11× | Ok(()) |
| 140 | 11× | } |
| 141 | |
|
| 142 | |
|
| 143 | 35× | pub(super) async fn finished(pool: &Pool, table: &str, id: i64) -> sqlx::Result<()> { |
| 144 | 35× | let at: Option<(i64, i64)> = |
| 145 | 35× | sqlx::query_as(&format!("SELECT repo_id, number FROM {table} WHERE id = $1")).bind(id).fetch_optional(pool).await?; |
| 146 | 35× | match at { |
| 147 | 35× | Some((repo, number)) => super::projects::finished(pool, repo, number).await, |
| 148 | 0× | None => Ok(()), |
| 149 | | } |
| 150 | 35× | } |
| 151 | |
|
| 152 | 1× | pub async fn set_flags(pool: &Pool, id: i64, locked: Option<bool>, pinned: Option<bool>) -> sqlx::Result<()> { |
| 153 | 1× | if let Some(l) = locked { |
| 154 | 0× | sqlx::query("UPDATE issues SET locked = $1 WHERE id = $2").bind(l).bind(id).execute(pool).await?; |
| 155 | 1× | } |
| 156 | 1× | if let Some(p) = pinned { |
| 157 | 1× | sqlx::query("UPDATE issues SET pinned = $1 WHERE id = $2").bind(p).bind(id).execute(pool).await?; |
| 158 | 0× | } |
| 159 | 1× | Ok(()) |
| 160 | 1× | } |
| 161 | |
|
| 162 | 2× | pub async fn set_milestone(pool: &Pool, id: i64, milestone_id: Option<i64>) -> sqlx::Result<()> { |
| 163 | 2× | sqlx::query("UPDATE issues SET milestone_id = $1, updated_ms = $2 WHERE id = $3") |
| 164 | 2× | .bind(milestone_id) |
| 165 | 2× | .bind(now_ms()) |
| 166 | 2× | .bind(id) |
| 167 | 2× | .execute(pool) |
| 168 | 2× | .await?; |
| 169 | 2× | Ok(()) |
| 170 | 2× | } |
| 171 | |
|
| 172 | 13× | pub async fn touch(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 173 | 13× | sqlx::query("UPDATE issues SET updated_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; |
| 174 | 13× | Ok(()) |
| 175 | 13× | } |
| 176 | |
|
| 177 | 65× | pub async fn comments(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<IssueComment>> { |
| 178 | 65× | sqlx::query_as("SELECT * FROM issue_comments WHERE issue_id = $1 ORDER BY created_ms, id").bind(issue_id).fetch_all(pool).await |
| 179 | 65× | } |
| 180 | |
|
| 181 | 9× | pub async fn add_comment(pool: &Pool, issue_id: i64, author_id: Option<i64>, author: &str, body: &str) -> sqlx::Result<IssueComment> { |
| 182 | 9× | let row = sqlx::query_as( |
| 183 | 9× | "INSERT INTO issue_comments (issue_id, author_id, author, body, created_ms) VALUES ($1, $2, $3, $4, $5) RETURNING *", |
| 184 | 9× | ) |
| 185 | 9× | .bind(issue_id) |
| 186 | 9× | .bind(author_id) |
| 187 | 9× | .bind(author) |
| 188 | 9× | .bind(body) |
| 189 | 9× | .bind(now_ms()) |
| 190 | 9× | .fetch_one(pool) |
| 191 | 9× | .await?; |
| 192 | 9× | touch(pool, issue_id).await?; |
| 193 | 9× | Ok(row) |
| 194 | 9× | } |
| 195 | |
|
| 196 | 0× | pub async fn edit_comment(pool: &Pool, id: i64, author_id: i64, body: &str) -> sqlx::Result<bool> { |
| 197 | 0× | let n = sqlx::query("UPDATE issue_comments SET body = $1, edited_ms = $2 WHERE id = $3 AND author_id = $4") |
| 198 | 0× | .bind(body) |
| 199 | 0× | .bind(now_ms()) |
| 200 | 0× | .bind(id) |
| 201 | 0× | .bind(author_id) |
| 202 | 0× | .execute(pool) |
| 203 | 0× | .await?; |
| 204 | 0× | Ok(n.rows_affected() > 0) |
| 205 | 0× | } |
| 206 | |
|
| 207 | 0× | pub async fn delete_comment(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 208 | 0× | sqlx::query("DELETE FROM issue_comments WHERE id = $1").bind(id).execute(pool).await?; |
| 209 | 0× | Ok(()) |
| 210 | 0× | } |
| 211 | |
|
| 212 | 29× | pub async fn events(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<IssueEvent>> { |
| 213 | 29× | sqlx::query_as("SELECT id, at_ms, actor, event, detail FROM issue_events WHERE issue_id = $1 ORDER BY at_ms, id") |
| 214 | 29× | .bind(issue_id) |
| 215 | 29× | .fetch_all(pool) |
| 216 | 29× | .await |
| 217 | 29× | } |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | 1× | pub async fn referenced_by(pool: &Pool, issue_id: i64, hash: &str) -> sqlx::Result<bool> { |
| 222 | 1× | let n: i64 = sqlx::query_scalar( |
| 223 | 1× | "SELECT COUNT(*) FROM issue_events WHERE issue_id = $1 AND event = 'referenced by change' AND (detail = $2 OR detail LIKE $3)", |
| 224 | 1× | ) |
| 225 | 1× | .bind(issue_id) |
| 226 | 1× | .bind(hash) |
| 227 | 1× | .bind(format!("{hash} %")) |
| 228 | 1× | .fetch_one(pool) |
| 229 | 1× | .await?; |
| 230 | 1× | Ok(n > 0) |
| 231 | 1× | } |
| 232 | |
|
| 233 | 47× | pub async fn add_event(pool: &Pool, issue_id: i64, actor: &str, event: &str, detail: &str) -> sqlx::Result<()> { |
| 234 | 47× | sqlx::query("INSERT INTO issue_events (issue_id, at_ms, actor, event, detail) VALUES ($1, $2, $3, $4, $5)") |
| 235 | 47× | .bind(issue_id) |
| 236 | 47× | .bind(now_ms()) |
| 237 | 47× | .bind(actor) |
| 238 | 47× | .bind(event) |
| 239 | 47× | .bind(detail) |
| 240 | 47× | .execute(pool) |
| 241 | 47× | .await?; |
| 242 | 47× | Ok(()) |
| 243 | 47× | } |
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | 110× | pub async fn labels(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Label>> { |
| 248 | 110× | sqlx::query_as("SELECT * FROM labels WHERE repo_id = $1 ORDER BY name").bind(repo_id).fetch_all(pool).await |
| 249 | 110× | } |
| 250 | |
|
| 251 | 24× | pub async fn label_by_name(pool: &Pool, repo_id: i64, name: &str) -> sqlx::Result<Option<Label>> { |
| 252 | 24× | sqlx::query_as("SELECT * FROM labels WHERE repo_id = $1 AND name = $2").bind(repo_id).bind(name).fetch_optional(pool).await |
| 253 | 24× | } |
| 254 | |
|
| 255 | 23× | pub async fn upsert_label(pool: &Pool, repo_id: i64, name: &str, colour: &str, description: &str) -> sqlx::Result<Label> { |
| 256 | 23× | sqlx::query_as("INSERT INTO labels (repo_id, name, colour, description) VALUES ($1, $2, $3, $4) ON CONFLICT (repo_id, name) DO UPDATE SET colour = excluded.colour, description = excluded.description RETURNING *") |
| 257 | 23× | .bind(repo_id) |
| 258 | 23× | .bind(name) |
| 259 | 23× | .bind(colour) |
| 260 | 23× | .bind(description) |
| 261 | 23× | .fetch_one(pool) |
| 262 | 23× | .await |
| 263 | 23× | } |
| 264 | |
|
| 265 | 1× | pub async fn delete_label(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 266 | 1× | sqlx::query("DELETE FROM labels WHERE id = $1").bind(id).execute(pool).await?; |
| 267 | 1× | Ok(()) |
| 268 | 1× | } |
| 269 | |
|
| 270 | 63× | pub async fn issue_labels(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<Label>> { |
| 271 | 63× | sqlx::query_as("SELECT l.* FROM labels l JOIN issue_labels il ON il.label_id = l.id WHERE il.issue_id = $1 ORDER BY l.name") |
| 272 | 63× | .bind(issue_id) |
| 273 | 63× | .fetch_all(pool) |
| 274 | 63× | .await |
| 275 | 63× | } |
| 276 | |
|
| 277 | 10× | pub async fn set_issue_label(pool: &Pool, issue_id: i64, label_id: i64, on: bool) -> sqlx::Result<()> { |
| 278 | 10× | if on { |
| 279 | 8× | sqlx::query("INSERT INTO issue_labels (issue_id, label_id) VALUES ($1, $2) ON CONFLICT DO NOTHING") |
| 280 | 8× | .bind(issue_id) |
| 281 | 8× | .bind(label_id) |
| 282 | 8× | .execute(pool) |
| 283 | 8× | .await?; |
| 284 | | } else { |
| 285 | 2× | sqlx::query("DELETE FROM issue_labels WHERE issue_id = $1 AND label_id = $2").bind(issue_id).bind(label_id).execute(pool).await?; |
| 286 | | } |
| 287 | 10× | Ok(()) |
| 288 | 10× | } |
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | 16× | pub async fn labels_by_proposal(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<(i64, Label)>> { |
| 293 | 16× | let rows: Vec<(i64, i64, i64, String, String, String)> = sqlx::query_as( |
| 294 | 16× | "SELECT pl.proposal_id, l.id, l.repo_id, l.name, l.colour, l.description FROM proposal_labels pl JOIN labels l ON l.id = pl.label_id JOIN proposals p ON p.id = pl.proposal_id WHERE p.repo_id = $1 ORDER BY l.name", |
| 295 | 16× | ) |
| 296 | 16× | .bind(repo_id) |
| 297 | 16× | .fetch_all(pool) |
| 298 | 16× | .await?; |
| 299 | 16× | Ok(rows |
| 300 | 16× | .into_iter() |
| 301 | 16× | .map(|(proposal_id, id, repo_id, name, colour, description)| (proposal_id, Label { id, repo_id, name, colour, description })) |
| 302 | 16× | .collect()) |
| 303 | 16× | } |
| 304 | |
|
| 305 | 55× | pub async fn labels_by_issue(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<(i64, Label)>> { |
| 306 | 55× | let rows: Vec<(i64, i64, i64, String, String, String)> = sqlx::query_as( |
| 307 | 55× | "SELECT il.issue_id, l.id, l.repo_id, l.name, l.colour, l.description FROM issue_labels il JOIN labels l ON l.id = il.label_id JOIN issues i ON i.id = il.issue_id WHERE i.repo_id = $1 ORDER BY l.name", |
| 308 | 55× | ) |
| 309 | 55× | .bind(repo_id) |
| 310 | 55× | .fetch_all(pool) |
| 311 | 55× | .await?; |
| 312 | 55× | Ok(rows |
| 313 | 55× | .into_iter() |
| 314 | 55× | .map(|(issue_id, id, repo_id, name, colour, description)| (issue_id, Label { id, repo_id, name, colour, description })) |
| 315 | 55× | .collect()) |
| 316 | 55× | } |
| 317 | |
|
| 318 | 118× | pub async fn proposal_labels(pool: &Pool, proposal_id: i64) -> sqlx::Result<Vec<Label>> { |
| 319 | 118× | sqlx::query_as("SELECT l.* FROM labels l JOIN proposal_labels pl ON pl.label_id = l.id WHERE pl.proposal_id = $1 ORDER BY l.name") |
| 320 | 118× | .bind(proposal_id) |
| 321 | 118× | .fetch_all(pool) |
| 322 | 118× | .await |
| 323 | 118× | } |
| 324 | |
|
| 325 | 14× | pub async fn set_proposal_label(pool: &Pool, proposal_id: i64, label_id: i64, on: bool) -> sqlx::Result<()> { |
| 326 | 14× | if on { |
| 327 | 13× | sqlx::query("INSERT INTO proposal_labels (proposal_id, label_id) VALUES ($1, $2) ON CONFLICT DO NOTHING") |
| 328 | 13× | .bind(proposal_id) |
| 329 | 13× | .bind(label_id) |
| 330 | 13× | .execute(pool) |
| 331 | 13× | .await?; |
| 332 | | } else { |
| 333 | 1× | sqlx::query("DELETE FROM proposal_labels WHERE proposal_id = $1 AND label_id = $2") |
| 334 | 1× | .bind(proposal_id) |
| 335 | 1× | .bind(label_id) |
| 336 | 1× | .execute(pool) |
| 337 | 1× | .await?; |
| 338 | | } |
| 339 | 14× | Ok(()) |
| 340 | 14× | } |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | 69× | pub async fn assignees(pool: &Pool, issue_id: i64) -> sqlx::Result<Vec<String>> { |
| 345 | 69× | sqlx::query_scalar( |
| 346 | 69× | "SELECT a.username FROM accounts a JOIN issue_assignees ia ON ia.account_id = a.id WHERE ia.issue_id = $1 ORDER BY a.username", |
| 347 | 69× | ) |
| 348 | 69× | .bind(issue_id) |
| 349 | 69× | .fetch_all(pool) |
| 350 | 69× | .await |
| 351 | 69× | } |
| 352 | |
|
| 353 | 10× | pub async fn set_assignee(pool: &Pool, issue_id: i64, account_id: i64, on: bool) -> sqlx::Result<()> { |
| 354 | 10× | if on { |
| 355 | 7× | sqlx::query("INSERT INTO issue_assignees (issue_id, account_id) VALUES ($1, $2) ON CONFLICT DO NOTHING") |
| 356 | 7× | .bind(issue_id) |
| 357 | 7× | .bind(account_id) |
| 358 | 7× | .execute(pool) |
| 359 | 7× | .await?; |
| 360 | | } else { |
| 361 | 3× | sqlx::query("DELETE FROM issue_assignees WHERE issue_id = $1 AND account_id = $2") |
| 362 | 3× | .bind(issue_id) |
| 363 | 3× | .bind(account_id) |
| 364 | 3× | .execute(pool) |
| 365 | 3× | .await?; |
| 366 | | } |
| 367 | 10× | Ok(()) |
| 368 | 10× | } |
| 369 | |
|
| 370 | |
|
| 371 | 55× | pub async fn assignees_by_issue(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<(i64, String)>> { |
| 372 | 55× | sqlx::query_as("SELECT ia.issue_id, a.username FROM issue_assignees ia JOIN accounts a ON a.id = ia.account_id JOIN issues i ON i.id = ia.issue_id WHERE i.repo_id = $1") |
| 373 | 55× | .bind(repo_id) |
| 374 | 55× | .fetch_all(pool) |
| 375 | 55× | .await |
| 376 | 55× | } |
| 377 | |
|
| 378 | |
|
| 379 | |
|
| 380 | 43× | pub async fn milestones(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Milestone>> { |
| 381 | 43× | sqlx::query_as("SELECT * FROM milestones WHERE repo_id = $1 ORDER BY state, due_ms, title").bind(repo_id).fetch_all(pool).await |
| 382 | 43× | } |
| 383 | |
|
| 384 | 11× | pub async fn milestone(pool: &Pool, id: i64) -> sqlx::Result<Option<Milestone>> { |
| 385 | 11× | sqlx::query_as("SELECT * FROM milestones WHERE id = $1").bind(id).fetch_optional(pool).await |
| 386 | 11× | } |
| 387 | |
|
| 388 | 3× | pub async fn upsert_milestone(pool: &Pool, repo_id: i64, title: &str, description: &str, due_ms: Option<i64>) -> sqlx::Result<Milestone> { |
| 389 | 3× | sqlx::query_as("INSERT INTO milestones (repo_id, title, description, due_ms) VALUES ($1, $2, $3, $4) ON CONFLICT (repo_id, title) DO UPDATE SET description = excluded.description, due_ms = excluded.due_ms RETURNING *") |
| 390 | 3× | .bind(repo_id) |
| 391 | 3× | .bind(title) |
| 392 | 3× | .bind(description) |
| 393 | 3× | .bind(due_ms) |
| 394 | 3× | .fetch_one(pool) |
| 395 | 3× | .await |
| 396 | 3× | } |
| 397 | |
|
| 398 | |
|
| 399 | 1× | pub async fn update_milestone(pool: &Pool, id: i64, title: &str, description: &str, due_ms: Option<i64>) -> sqlx::Result<()> { |
| 400 | 1× | sqlx::query("UPDATE milestones SET title = $1, description = $2, due_ms = $3 WHERE id = $4") |
| 401 | 1× | .bind(title) |
| 402 | 1× | .bind(description) |
| 403 | 1× | .bind(due_ms) |
| 404 | 1× | .bind(id) |
| 405 | 1× | .execute(pool) |
| 406 | 1× | .await?; |
| 407 | 1× | Ok(()) |
| 408 | 1× | } |
| 409 | |
|
| 410 | 1× | pub async fn set_milestone_state(pool: &Pool, id: i64, state: &str) -> sqlx::Result<()> { |
| 411 | 1× | sqlx::query("UPDATE milestones SET state = $1 WHERE id = $2").bind(state).bind(id).execute(pool).await?; |
| 412 | 1× | Ok(()) |
| 413 | 1× | } |
| 414 | |
|
| 415 | |
|
| 416 | 5× | pub async fn milestone_counts(pool: &Pool, milestone_id: i64) -> sqlx::Result<(i64, i64)> { |
| 417 | 5× | let open: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM issues WHERE milestone_id = $1 AND state = 'open'") |
| 418 | 5× | .bind(milestone_id) |
| 419 | 5× | .fetch_one(pool) |
| 420 | 5× | .await?; |
| 421 | 5× | let closed: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM issues WHERE milestone_id = $1 AND state = 'closed'") |
| 422 | 5× | .bind(milestone_id) |
| 423 | 5× | .fetch_one(pool) |
| 424 | 5× | .await?; |
| 425 | 5× | Ok((open, closed)) |
| 426 | 5× | } |