| 1 | |
|
| 2 | |
|
| 3 | | use super::{now_ms, Pool}; |
| 4 | | use sqlx::FromRow; |
| 5 | |
|
| 6 | | #[derive(Clone, Debug, FromRow)] |
| 7 | | pub struct Release { |
| 8 | | pub id: i64, |
| 9 | | pub repo_id: i64, |
| 10 | | pub tag: String, |
| 11 | | pub title: String, |
| 12 | | pub notes: String, |
| 13 | | pub prerelease: bool, |
| 14 | | pub author_id: Option<i64>, |
| 15 | | pub author: String, |
| 16 | | pub created_ms: i64, |
| 17 | | } |
| 18 | |
|
| 19 | | #[derive(Clone, Debug, FromRow)] |
| 20 | | pub struct Asset { |
| 21 | | pub id: i64, |
| 22 | | pub release_id: i64, |
| 23 | | pub name: String, |
| 24 | | pub size: i64, |
| 25 | | pub hash: String, |
| 26 | | pub content_type: String, |
| 27 | | pub downloads: i64, |
| 28 | | pub created_ms: i64, |
| 29 | | } |
| 30 | |
|
| 31 | 89× | pub async fn list(pool: &Pool, repo_id: i64) -> sqlx::Result<Vec<Release>> { |
| 32 | 89× | sqlx::query_as("SELECT * FROM releases WHERE repo_id = $1 ORDER BY created_ms DESC, id DESC").bind(repo_id).fetch_all(pool).await |
| 33 | 89× | } |
| 34 | |
|
| 35 | 13× | pub async fn get(pool: &Pool, repo_id: i64, tag: &str) -> sqlx::Result<Option<Release>> { |
| 36 | 13× | sqlx::query_as("SELECT * FROM releases WHERE repo_id = $1 AND tag = $2").bind(repo_id).bind(tag).fetch_optional(pool).await |
| 37 | 13× | } |
| 38 | |
|
| 39 | |
|
| 40 | 66× | pub async fn latest(pool: &Pool, repo_id: i64) -> sqlx::Result<Option<Release>> { |
| 41 | 66× | sqlx::query_as("SELECT * FROM releases WHERE repo_id = $1 AND prerelease = FALSE ORDER BY created_ms DESC, id DESC LIMIT 1") |
| 42 | 66× | .bind(repo_id) |
| 43 | 66× | .fetch_optional(pool) |
| 44 | 66× | .await |
| 45 | 66× | } |
| 46 | |
|
| 47 | 10× | pub async fn create( |
| 48 | 10× | pool: &Pool, |
| 49 | 10× | repo_id: i64, |
| 50 | 10× | tag: &str, |
| 51 | 10× | title: &str, |
| 52 | 10× | notes: &str, |
| 53 | 10× | prerelease: bool, |
| 54 | 10× | author: Option<(i64, &str)>, |
| 55 | 10× | ) -> sqlx::Result<Release> { |
| 56 | 10× | sqlx::query_as("INSERT INTO releases (repo_id, tag, title, notes, prerelease, author_id, author, created_ms) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *") |
| 57 | 10× | .bind(repo_id) |
| 58 | 10× | .bind(tag) |
| 59 | 10× | .bind(title) |
| 60 | 10× | .bind(notes) |
| 61 | 10× | .bind(prerelease) |
| 62 | 10× | .bind(author.map(|a| a.0)) |
| 63 | 10× | .bind(author.map(|a| a.1).unwrap_or("")) |
| 64 | 10× | .bind(now_ms()) |
| 65 | 10× | .fetch_one(pool) |
| 66 | 10× | .await |
| 67 | 10× | } |
| 68 | |
|
| 69 | 1× | pub async fn edit(pool: &Pool, id: i64, title: &str, notes: &str, prerelease: bool) -> sqlx::Result<()> { |
| 70 | 1× | sqlx::query("UPDATE releases SET title = $1, notes = $2, prerelease = $3 WHERE id = $4") |
| 71 | 1× | .bind(title) |
| 72 | 1× | .bind(notes) |
| 73 | 1× | .bind(prerelease) |
| 74 | 1× | .bind(id) |
| 75 | 1× | .execute(pool) |
| 76 | 1× | .await?; |
| 77 | 1× | Ok(()) |
| 78 | 1× | } |
| 79 | |
|
| 80 | 0× | pub async fn delete(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 81 | 0× | sqlx::query("DELETE FROM releases WHERE id = $1").bind(id).execute(pool).await?; |
| 82 | 0× | Ok(()) |
| 83 | 0× | } |
| 84 | |
|
| 85 | 50× | pub async fn assets(pool: &Pool, release_id: i64) -> sqlx::Result<Vec<Asset>> { |
| 86 | 50× | sqlx::query_as("SELECT * FROM release_assets WHERE release_id = $1 ORDER BY name").bind(release_id).fetch_all(pool).await |
| 87 | 50× | } |
| 88 | |
|
| 89 | 0× | pub async fn asset(pool: &Pool, release_id: i64, name: &str) -> sqlx::Result<Option<Asset>> { |
| 90 | 0× | sqlx::query_as("SELECT * FROM release_assets WHERE release_id = $1 AND name = $2") |
| 91 | 0× | .bind(release_id) |
| 92 | 0× | .bind(name) |
| 93 | 0× | .fetch_optional(pool) |
| 94 | 0× | .await |
| 95 | 0× | } |
| 96 | |
|
| 97 | 10× | pub async fn add_asset(pool: &Pool, release_id: i64, name: &str, size: i64, hash: &str, content_type: &str) -> sqlx::Result<Asset> { |
| 98 | 10× | sqlx::query_as( |
| 99 | 10× | "INSERT INTO release_assets (release_id, name, size, hash, content_type, created_ms) VALUES ($1, $2, $3, $4, $5, $6) \ |
| 100 | 10× | ON CONFLICT (release_id, name) DO UPDATE SET size = excluded.size, hash = excluded.hash, content_type = excluded.content_type, created_ms = excluded.created_ms RETURNING *", |
| 101 | 10× | ) |
| 102 | 10× | .bind(release_id) |
| 103 | 10× | .bind(name) |
| 104 | 10× | .bind(size) |
| 105 | 10× | .bind(hash) |
| 106 | 10× | .bind(content_type) |
| 107 | 10× | .bind(now_ms()) |
| 108 | 10× | .fetch_one(pool) |
| 109 | 10× | .await |
| 110 | 10× | } |
| 111 | |
|
| 112 | 0× | pub async fn delete_asset(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 113 | 0× | sqlx::query("DELETE FROM release_assets WHERE id = $1").bind(id).execute(pool).await?; |
| 114 | 0× | Ok(()) |
| 115 | 0× | } |
| 116 | |
|
| 117 | 1× | pub async fn count_download(pool: &Pool, id: i64) -> sqlx::Result<()> { |
| 118 | 1× | sqlx::query("UPDATE release_assets SET downloads = downloads + 1 WHERE id = $1").bind(id).execute(pool).await?; |
| 119 | 1× | Ok(()) |
| 120 | 1× | } |
| 121 | |
|
| 122 | |
|
| 123 | 0× | pub async fn hash_in_use(pool: &Pool, hash: &str) -> sqlx::Result<bool> { |
| 124 | 0× | let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM release_assets WHERE hash = $1").bind(hash).fetch_one(pool).await?; |
| 125 | 0× | Ok(n > 0) |
| 126 | 0× | } |