| 1 | |
|
| 2 | |
|
| 3 | | use super::{now_ms, Pool}; |
| 4 | | use sqlx::FromRow; |
| 5 | |
|
| 6 | | #[derive(Clone, Debug, FromRow)] |
| 7 | | pub struct Project { |
| 8 | | pub id: i64, |
| 9 | | pub owner: String, |
| 10 | | pub number: i64, |
| 11 | | pub title: String, |
| 12 | | pub description: String, |
| 13 | | pub public: bool, |
| 14 | | pub closed: bool, |
| 15 | | pub created_ms: i64, |
| 16 | | pub updated_ms: i64, |
| 17 | | } |
| 18 | |
|
| 19 | | #[derive(Clone, Debug, FromRow)] |
| 20 | | pub struct Column { |
| 21 | | pub id: i64, |
| 22 | | pub project_id: i64, |
| 23 | | pub name: String, |
| 24 | | pub position: i64, |
| 25 | | |
| 26 | | pub archive_after_days: Option<i64>, |
| 27 | | } |
| 28 | |
|
| 29 | | #[derive(Clone, Debug, FromRow)] |
| 30 | | pub struct Card { |
| 31 | | pub id: i64, |
| 32 | | pub project_id: i64, |
| 33 | | pub column_id: i64, |
| 34 | | pub position: i64, |
| 35 | | pub kind: String, |
| 36 | | pub repo_id: Option<i64>, |
| 37 | | pub number: Option<i64>, |
| 38 | | pub note: String, |
| 39 | | pub added_by: String, |
| 40 | | pub created_ms: i64, |
| 41 | | |
| 42 | | pub archived_ms: Option<i64>, |
| 43 | | |
| 44 | | pub moved_ms: i64, |
| 45 | | } |
| 46 | |
|
| 47 | |
|
| 48 | | const DAY_MS: i64 = 86_400_000; |
| 49 | |
|
| 50 | | pub const DEFAULT_COLUMNS: [&str; 3] = ["Backlog", "Doing", "Done"]; |
| 51 | |
|
| 52 | 9× | pub async fn create(pool: &Pool, owner: &str, title: &str, description: &str, public: bool) -> sqlx::Result<Project> { |
| 53 | 9× | let mut tx = super::begin_write(pool).await?; |
| 54 | 9× | let number: i64 = |
| 55 | 9× | sqlx::query_scalar("SELECT COALESCE(MAX(number), 0) + 1 FROM projects WHERE owner = $1").bind(owner).fetch_one(&mut *tx).await?; |
| 56 | 9× | let now = now_ms(); |
| 57 | 9× | let p: Project = sqlx::query_as( |
| 58 | 9× | "INSERT INTO projects (owner, number, title, description, public, created_ms, updated_ms) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *", |
| 59 | 9× | ) |
| 60 | 9× | .bind(owner) |
| 61 | 9× | .bind(number) |
| 62 | 9× | .bind(title) |
| 63 | 9× | .bind(description) |
| 64 | 9× | .bind(public) |
| 65 | 9× | .bind(now) |
| 66 | 9× | .bind(now) |
| 67 | 9× | .fetch_one(&mut *tx) |
| 68 | 9× | .await?; |
| 69 | | |
| 70 | 9× | sqlx::query("DELETE FROM project_redirects WHERE owner = $1 AND number = $2").bind(owner).bind(number).execute(&mut *tx).await?; |
| 71 | 27× | for (i, name) in DEFAULT_COLUMNS.iter().enumerate() { |
| 72 | 27× | sqlx::query("INSERT INTO project_columns (project_id, name, position) VALUES ($1, $2, $3)") |
| 73 | 27× | .bind(p.id) |
| 74 | 27× | .bind(name) |
| 75 | 27× | .bind(i as i64) |
| 76 | 27× | .execute(&mut *tx) |
| 77 | 27× | .await?; |
| 78 | | } |
| 79 | 9× | tx.commit().await?; |
| 80 | 9× | Ok(p) |
| 81 | 9× | } |
| 82 | |
|
| 83 | 67× | pub async fn of_owner(pool: &Pool, owner: &str) -> sqlx::Result<Vec<Project>> { |
| 84 | 67× | sqlx::query_as("SELECT * FROM projects WHERE owner = $1 ORDER BY closed, updated_ms DESC").bind(owner).fetch_all(pool).await |
| 85 | 67× | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | 1× | pub async fn transfer(pool: &Pool, id: i64, to: &str) -> sqlx::Result<Project> { |
| 90 | 1× | let mut tx = super::begin_write(pool).await?; |
| 91 | 1× | let old: Project = sqlx::query_as("SELECT * FROM projects WHERE id = $1").bind(id).fetch_one(&mut *tx).await?; |
| 92 | 1× | let number: i64 = |
| 93 | 1× | sqlx::query_scalar("SELECT COALESCE(MAX(number), 0) + 1 FROM projects WHERE owner = $1").bind(to).fetch_one(&mut *tx).await?; |
| 94 | 1× | sqlx::query("INSERT INTO project_redirects (owner, number, project_id) VALUES ($1, $2, $3) ON CONFLICT (owner, number) DO UPDATE SET project_id = excluded.project_id") |
| 95 | 1× | .bind(&old.owner) |
| 96 | 1× | .bind(old.number) |
| 97 | 1× | .bind(id) |
| 98 | 1× | .execute(&mut *tx) |
| 99 | 1× | .await?; |
| 100 | 1× | sqlx::query("DELETE FROM project_redirects WHERE owner = $1 AND number = $2").bind(to).bind(number).execute(&mut *tx).await?; |
| 101 | 1× | let moved: Project = sqlx::query_as("UPDATE projects SET owner = $1, number = $2, updated_ms = $3 WHERE id = $4 RETURNING *") |
| 102 | 1× | .bind(to) |
| 103 | 1× | .bind(number) |
| 104 | 1× | .bind(now_ms()) |
| 105 | 1× | .bind(id) |
| 106 | 1× | .fetch_one(&mut *tx) |
| 107 | 1× | .await?; |
| 108 | 1× | tx.commit().await?; |
| 109 | 1× | Ok(moved) |
| 110 | 1× | } |
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | 2× | pub async fn redirected(pool: &Pool, owner: &str, number: i64) -> sqlx::Result<Option<Project>> { |
| 115 | 2× | sqlx::query_as("SELECT p.* FROM project_redirects r JOIN projects p ON p.id = r.project_id WHERE r.owner = $1 AND r.number = $2") |
| 116 | 2× | .bind(owner) |
| 117 | 2× | .bind(number) |
| 118 | 2× | .fetch_optional(pool) |
| 119 | 2× | .await |
| 120 | 2× | } |
| 121 | |
|
| 122 | 145× | pub async fn get(pool: &Pool, owner: &str, number: i64) -> sqlx::Result<Option<Project>> { |
| 123 | 145× | sqlx::query_as("SELECT * FROM projects WHERE owner = $1 AND number = $2").bind(owner).bind(number).fetch_optional(pool).await |
| 124 | 145× | } |
| 125 | |
|
| 126 | 6× | pub async fn set(pool: &Pool, id: i64, title: &str, description: &str, public: bool, closed: bool) -> sqlx::Result<()> { |
| 127 | 6× | sqlx::query("UPDATE projects SET title = $1, description = $2, public = $3, closed = $4, updated_ms = $5 WHERE id = $6") |
| 128 | 6× | .bind(title) |
| 129 | 6× | .bind(description) |
| 130 | 6× | .bind(public) |
| 131 | 6× | .bind(closed) |
| 132 | 6× | .bind(now_ms()) |
| 133 | 6× | .bind(id) |
| 134 | 6× | .execute(pool) |
| 135 | 6× | .await?; |
| 136 | 6× | Ok(()) |
| 137 | 6× | } |
| 138 | |
|
| 139 | 1× | pub async fn delete(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 140 | 1× | sqlx::query("DELETE FROM projects WHERE id = $1").bind(id).execute(pool).await?; |
| 141 | 1× | Ok(()) |
| 142 | 1× | } |
| 143 | |
|
| 144 | 26× | async fn touch(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 145 | 26× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now_ms()).bind(id).execute(pool).await?; |
| 146 | 26× | Ok(()) |
| 147 | 26× | } |
| 148 | |
|
| 149 | 109× | pub async fn columns(pool: &Pool, project_id: i64) -> sqlx::Result<Vec<Column>> { |
| 150 | 109× | sqlx::query_as("SELECT * FROM project_columns WHERE project_id = $1 ORDER BY position, id").bind(project_id).fetch_all(pool).await |
| 151 | 109× | } |
| 152 | |
|
| 153 | 67× | pub async fn cards(pool: &Pool, project_id: i64) -> sqlx::Result<Vec<Card>> { |
| 154 | 67× | sqlx::query_as("SELECT * FROM project_cards WHERE project_id = $1 ORDER BY position, id").bind(project_id).fetch_all(pool).await |
| 155 | 67× | } |
| 156 | |
|
| 157 | 0× | pub async fn add_column(pool: &Pool, project_id: i64, name: &str) -> sqlx::Result<()> { |
| 158 | 0× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_columns WHERE project_id = $1") |
| 159 | 0× | .bind(project_id) |
| 160 | 0× | .fetch_one(pool) |
| 161 | 0× | .await?; |
| 162 | 0× | sqlx::query("INSERT INTO project_columns (project_id, name, position) VALUES ($1, $2, $3)") |
| 163 | 0× | .bind(project_id) |
| 164 | 0× | .bind(name) |
| 165 | 0× | .bind(pos) |
| 166 | 0× | .execute(pool) |
| 167 | 0× | .await?; |
| 168 | 0× | touch(pool, project_id).await |
| 169 | 0× | } |
| 170 | |
|
| 171 | 0× | pub async fn rename_column(pool: &Pool, project_id: i64, column_id: i64, name: &str) -> sqlx::Result<()> { |
| 172 | 0× | sqlx::query("UPDATE project_columns SET name = $1 WHERE id = $2 AND project_id = $3") |
| 173 | 0× | .bind(name) |
| 174 | 0× | .bind(column_id) |
| 175 | 0× | .bind(project_id) |
| 176 | 0× | .execute(pool) |
| 177 | 0× | .await?; |
| 178 | 0× | touch(pool, project_id).await |
| 179 | 0× | } |
| 180 | |
|
| 181 | |
|
| 182 | 0× | pub async fn shift_column(pool: &Pool, project_id: i64, column_id: i64, by: i64) -> sqlx::Result<()> { |
| 183 | 0× | let cols = columns(pool, project_id).await?; |
| 184 | 0× | let Some(i) = cols.iter().position(|c| c.id == column_id) else { return Ok(()) }; |
| 185 | 0× | let j = i as i64 + by; |
| 186 | 0× | if j < 0 || j as usize >= cols.len() { |
| 187 | 0× | return Ok(()); |
| 188 | 0× | } |
| 189 | 0× | let mut ids: Vec<i64> = cols.iter().map(|c| c.id).collect(); |
| 190 | 0× | ids.swap(i, j as usize); |
| 191 | 0× | for (pos, id) in ids.iter().enumerate() { |
| 192 | 0× | sqlx::query("UPDATE project_columns SET position = $1 WHERE id = $2").bind(pos as i64).bind(id).execute(pool).await?; |
| 193 | | } |
| 194 | 0× | touch(pool, project_id).await |
| 195 | 0× | } |
| 196 | |
|
| 197 | |
|
| 198 | 3× | pub async fn remove_column(pool: &Pool, project_id: i64, column_id: i64) -> sqlx::Result<bool> { |
| 199 | 3× | let cols = columns(pool, project_id).await?; |
| 200 | 3× | let Some(to) = cols.iter().find(|c| c.id != column_id) else { return Ok(false) }; |
| 201 | 4× | if !cols.iter().any(|c| c.id == column_id) { |
| 202 | 0× | return Ok(false); |
| 203 | 2× | } |
| 204 | 2× | let mut tx = super::begin_write(pool).await?; |
| 205 | 2× | let base: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 206 | 2× | .bind(to.id) |
| 207 | 2× | .fetch_one(&mut *tx) |
| 208 | 2× | .await?; |
| 209 | 2× | sqlx::query("UPDATE project_cards SET column_id = $1, position = position + $2 WHERE column_id = $3") |
| 210 | 2× | .bind(to.id) |
| 211 | 2× | .bind(base) |
| 212 | 2× | .bind(column_id) |
| 213 | 2× | .execute(&mut *tx) |
| 214 | 2× | .await?; |
| 215 | 2× | sqlx::query("DELETE FROM project_columns WHERE id = $1").bind(column_id).execute(&mut *tx).await?; |
| 216 | 2× | tx.commit().await?; |
| 217 | 2× | touch(pool, project_id).await?; |
| 218 | 2× | Ok(true) |
| 219 | 3× | } |
| 220 | |
|
| 221 | | pub struct NewCard<'a> { |
| 222 | | pub column_id: i64, |
| 223 | | pub kind: &'a str, |
| 224 | | pub repo_id: Option<i64>, |
| 225 | | pub number: Option<i64>, |
| 226 | | pub note: &'a str, |
| 227 | | pub added_by: &'a str, |
| 228 | | } |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | 15× | pub async fn add_card(pool: &Pool, project_id: i64, c: NewCard<'_>) -> sqlx::Result<bool> { |
| 233 | 15× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 234 | 15× | .bind(c.column_id) |
| 235 | 15× | .fetch_one(pool) |
| 236 | 15× | .await?; |
| 237 | 15× | let r = sqlx::query( |
| 238 | 15× | "INSERT INTO project_cards (project_id, column_id, position, kind, repo_id, number, note, added_by, created_ms, moved_ms) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $9) ON CONFLICT DO NOTHING", |
| 239 | 15× | ) |
| 240 | 15× | .bind(project_id) |
| 241 | 15× | .bind(c.column_id) |
| 242 | 15× | .bind(pos) |
| 243 | 15× | .bind(c.kind) |
| 244 | 15× | .bind(c.repo_id) |
| 245 | 15× | .bind(c.number) |
| 246 | 15× | .bind(c.note) |
| 247 | 15× | .bind(c.added_by) |
| 248 | 15× | .bind(now_ms()) |
| 249 | 15× | .execute(pool) |
| 250 | 15× | .await?; |
| 251 | 15× | touch(pool, project_id).await?; |
| 252 | 15× | Ok(r.rows_affected() == 1) |
| 253 | 15× | } |
| 254 | |
|
| 255 | |
|
| 256 | 3× | pub async fn move_card(pool: &Pool, project_id: i64, card_id: i64, column_id: i64) -> sqlx::Result<()> { |
| 257 | 3× | let ok: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM project_columns WHERE id = $1 AND project_id = $2") |
| 258 | 3× | .bind(column_id) |
| 259 | 3× | .bind(project_id) |
| 260 | 3× | .fetch_one(pool) |
| 261 | 3× | .await?; |
| 262 | 3× | if ok == 0 { |
| 263 | 0× | return Ok(()); |
| 264 | 3× | } |
| 265 | 3× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 266 | 3× | .bind(column_id) |
| 267 | 3× | .fetch_one(pool) |
| 268 | 3× | .await?; |
| 269 | 3× | sqlx::query("UPDATE project_cards SET moved_ms = CASE WHEN column_id = $1 THEN moved_ms ELSE $3 END, column_id = $1, position = $2 WHERE id = $4 AND project_id = $5") |
| 270 | 3× | .bind(column_id) |
| 271 | 3× | .bind(pos) |
| 272 | 3× | .bind(now_ms()) |
| 273 | 3× | .bind(card_id) |
| 274 | 3× | .bind(project_id) |
| 275 | 3× | .execute(pool) |
| 276 | 3× | .await?; |
| 277 | 3× | touch(pool, project_id).await |
| 278 | 3× | } |
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | 1× | pub async fn move_cards(pool: &Pool, project_id: i64, card_ids: &[i64], column_id: i64) -> sqlx::Result<()> { |
| 283 | 1× | let mut tx = pool.begin().await?; |
| 284 | 1× | let pos: i64 = sqlx::query_scalar("SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = $1") |
| 285 | 1× | .bind(column_id) |
| 286 | 1× | .fetch_one(&mut *tx) |
| 287 | 1× | .await?; |
| 288 | 1× | let now = now_ms(); |
| 289 | 1× | for (i, id) in card_ids.iter().enumerate() { |
| 290 | 1× | sqlx::query("UPDATE project_cards SET column_id = $1, position = $2, moved_ms = $3 WHERE id = $4 AND project_id = $5") |
| 291 | 1× | .bind(column_id) |
| 292 | 1× | .bind(pos + i as i64) |
| 293 | 1× | .bind(now) |
| 294 | 1× | .bind(id) |
| 295 | 1× | .bind(project_id) |
| 296 | 1× | .execute(&mut *tx) |
| 297 | 1× | .await?; |
| 298 | | } |
| 299 | 1× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 300 | 1× | tx.commit().await |
| 301 | 1× | } |
| 302 | |
|
| 303 | |
|
| 304 | 3× | pub async fn archive_cards(pool: &Pool, project_id: i64, card_ids: &[i64]) -> sqlx::Result<u64> { |
| 305 | 3× | let mut tx = pool.begin().await?; |
| 306 | 3× | let now = now_ms(); |
| 307 | 3× | let mut n = 0; |
| 308 | 3× | for id in card_ids { |
| 309 | 3× | n += sqlx::query("UPDATE project_cards SET archived_ms = $1 WHERE id = $2 AND project_id = $3 AND archived_ms IS NULL") |
| 310 | 3× | .bind(now) |
| 311 | 3× | .bind(id) |
| 312 | 3× | .bind(project_id) |
| 313 | 3× | .execute(&mut *tx) |
| 314 | 3× | .await? |
| 315 | 3× | .rows_affected(); |
| 316 | | } |
| 317 | 3× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 318 | 3× | tx.commit().await?; |
| 319 | 3× | Ok(n) |
| 320 | 3× | } |
| 321 | |
|
| 322 | |
|
| 323 | |
|
| 324 | |
|
| 325 | 2× | pub async fn restore_card(pool: &Pool, project_id: i64, card_id: i64) -> sqlx::Result<bool> { |
| 326 | 2× | let mut tx = pool.begin().await?; |
| 327 | 2× | let pos: i64 = sqlx::query_scalar( |
| 328 | 2× | "SELECT COALESCE(MAX(position), -1) + 1 FROM project_cards WHERE column_id = (SELECT column_id FROM project_cards WHERE id = $1)", |
| 329 | 2× | ) |
| 330 | 2× | .bind(card_id) |
| 331 | 2× | .fetch_one(&mut *tx) |
| 332 | 2× | .await?; |
| 333 | 2× | let now = now_ms(); |
| 334 | 2× | let r = sqlx::query( |
| 335 | 2× | "UPDATE project_cards SET archived_ms = NULL, moved_ms = $1, position = $2 WHERE id = $3 AND project_id = $4 AND archived_ms IS NOT NULL", |
| 336 | 2× | ) |
| 337 | 2× | .bind(now) |
| 338 | 2× | .bind(pos) |
| 339 | 2× | .bind(card_id) |
| 340 | 2× | .bind(project_id) |
| 341 | 2× | .execute(&mut *tx) |
| 342 | 2× | .await?; |
| 343 | 2× | sqlx::query("UPDATE projects SET updated_ms = $1 WHERE id = $2").bind(now).bind(project_id).execute(&mut *tx).await?; |
| 344 | 2× | tx.commit().await?; |
| 345 | 2× | Ok(r.rows_affected() == 1) |
| 346 | 2× | } |
| 347 | |
|
| 348 | |
|
| 349 | 8× | pub fn older_than(card: &Card, days: i64, now: i64) -> bool { |
| 350 | 8× | card.moved_ms < now - days * DAY_MS |
| 351 | 8× | } |
| 352 | |
|
| 353 | |
|
| 354 | |
|
| 355 | |
|
| 356 | 3× | pub async fn set_archive_after(pool: &Pool, project_id: i64, column_id: i64, days: Option<i64>) -> sqlx::Result<()> { |
| 357 | 3× | sqlx::query("UPDATE project_columns SET archive_after_days = $1 WHERE id = $2 AND project_id = $3") |
| 358 | 3× | .bind(days) |
| 359 | 3× | .bind(column_id) |
| 360 | 3× | .bind(project_id) |
| 361 | 3× | .execute(pool) |
| 362 | 3× | .await?; |
| 363 | 3× | touch(pool, project_id).await |
| 364 | 3× | } |
| 365 | |
|
| 366 | |
|
| 367 | |
|
| 368 | | pub const FINISHED_KEPT_DAYS: i64 = 14; |
| 369 | |
|
| 370 | |
|
| 371 | | pub const NEVER: i64 = -1; |
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
|
| 377 | |
|
| 378 | 191× | pub async fn archive_due(pool: &Pool, now: i64) -> sqlx::Result<u64> { |
| 379 | 191× | let set = sqlx::query( |
| 380 | 191× | "UPDATE project_cards SET archived_ms = $1 WHERE archived_ms IS NULL AND EXISTS \ |
| 381 | 191× | (SELECT 1 FROM project_columns c WHERE c.id = project_cards.column_id AND c.archive_after_days >= 0 \ |
| 382 | 191× | AND project_cards.moved_ms < $1 - c.archive_after_days * $2)", |
| 383 | 191× | ) |
| 384 | 191× | .bind(now) |
| 385 | 191× | .bind(DAY_MS) |
| 386 | 191× | .execute(pool) |
| 387 | 191× | .await?; |
| 388 | 191× | let finished = sqlx::query( |
| 389 | 191× | "UPDATE project_cards SET archived_ms = $1 WHERE archived_ms IS NULL AND moved_ms < $2 \ |
| 390 | 191× | AND EXISTS (SELECT 1 FROM project_columns c WHERE c.id = project_cards.column_id AND c.archive_after_days IS NULL) \ |
| 391 | 191× | AND ((kind = 'issue' AND EXISTS (SELECT 1 FROM issues i WHERE i.repo_id = project_cards.repo_id \ |
| 392 | 191× | AND i.number = project_cards.number AND i.state = 'closed')) \ |
| 393 | 191× | OR (kind = 'proposal' AND EXISTS (SELECT 1 FROM proposals p WHERE p.repo_id = project_cards.repo_id \ |
| 394 | 191× | AND p.number = project_cards.number AND p.state IN ('closed', 'landed'))))", |
| 395 | 191× | ) |
| 396 | 191× | .bind(now) |
| 397 | 191× | .bind(now - FINISHED_KEPT_DAYS * DAY_MS) |
| 398 | 191× | .execute(pool) |
| 399 | 191× | .await?; |
| 400 | 190× | Ok(set.rows_affected() + finished.rows_affected()) |
| 401 | 190× | } |
| 402 | |
|
| 403 | |
|
| 404 | 0× | pub async fn shift_card(pool: &Pool, project_id: i64, card_id: i64, by: i64) -> sqlx::Result<()> { |
| 405 | 0× | let Some(card): Option<Card> = sqlx::query_as("SELECT * FROM project_cards WHERE id = $1 AND project_id = $2") |
| 406 | 0× | .bind(card_id) |
| 407 | 0× | .bind(project_id) |
| 408 | 0× | .fetch_optional(pool) |
| 409 | 0× | .await? |
| 410 | | else { |
| 411 | 0× | return Ok(()); |
| 412 | | }; |
| 413 | 0× | let col: Vec<Card> = sqlx::query_as("SELECT * FROM project_cards WHERE column_id = $1 ORDER BY position, id") |
| 414 | 0× | .bind(card.column_id) |
| 415 | 0× | .fetch_all(pool) |
| 416 | 0× | .await?; |
| 417 | 0× | let Some(i) = col.iter().position(|c| c.id == card_id) else { return Ok(()) }; |
| 418 | 0× | let j = i as i64 + by; |
| 419 | 0× | if j < 0 || j as usize >= col.len() { |
| 420 | 0× | return Ok(()); |
| 421 | 0× | } |
| 422 | 0× | let mut ids: Vec<i64> = col.iter().map(|c| c.id).collect(); |
| 423 | 0× | ids.swap(i, j as usize); |
| 424 | 0× | for (pos, id) in ids.iter().enumerate() { |
| 425 | 0× | sqlx::query("UPDATE project_cards SET position = $1 WHERE id = $2").bind(pos as i64).bind(id).execute(pool).await?; |
| 426 | | } |
| 427 | 0× | touch(pool, project_id).await |
| 428 | 0× | } |
| 429 | |
|
| 430 | 3× | pub async fn remove_card(pool: &Pool, project_id: i64, card_id: i64) -> sqlx::Result<()> { |
| 431 | 3× | sqlx::query("DELETE FROM project_cards WHERE id = $1 AND project_id = $2").bind(card_id).bind(project_id).execute(pool).await?; |
| 432 | 3× | touch(pool, project_id).await |
| 433 | 3× | } |
| 434 | |
|
| 435 | |
|
| 436 | 99× | pub async fn placements(pool: &Pool, repo_id: i64, number: i64) -> sqlx::Result<Vec<(Project, String)>> { |
| 437 | 99× | let rows: Vec<(i64, String, Option<i64>)> = sqlx::query_as( |
| 438 | 99× | "SELECT c.project_id, col.name, c.archived_ms FROM project_cards c JOIN project_columns col ON col.id = c.column_id WHERE c.repo_id = $1 AND c.number = $2 AND c.kind <> 'note'", |
| 439 | 99× | ) |
| 440 | 99× | .bind(repo_id) |
| 441 | 99× | .bind(number) |
| 442 | 99× | .fetch_all(pool) |
| 443 | 99× | .await?; |
| 444 | 99× | let mut out = Vec::new(); |
| 445 | 99× | for (pid, col, archived) in rows { |
| 446 | 3× | let col = if archived.is_some() { format!("{col}, archived") } else { col }; |
| 447 | 3× | if let Some(p) = sqlx::query_as::<_, Project>("SELECT * FROM projects WHERE id = $1").bind(pid).fetch_optional(pool).await? { |
| 448 | 3× | out.push((p, col)); |
| 449 | 3× | } |
| 450 | | } |
| 451 | 99× | Ok(out) |
| 452 | 99× | } |