use sqlx::PgPool; use uuid::Uuid; use super::pull_request::PullRequest; impl PullRequest { pub async fn find_by_id(pool: &PgPool, id: Uuid) -> Result, sqlx::Error> { sqlx::query_as::<_, PullRequest>( "SELECT id, repo_id, author_id, number, title, body, state, source_repo_id, \ source_branch, target_repo_id, target_branch, base_commit_sha, head_commit_sha, \ merge_commit_sha, draft, locked, merged_by, merged_at, closed_by, closed_at, \ created_at, updated_at, deleted_at \ FROM pull_request WHERE id = $1 AND deleted_at IS NULL", ) .bind(id) .fetch_optional(pool) .await } pub async fn find_by_number( pool: &PgPool, repo_id: Uuid, number: i64, ) -> Result, sqlx::Error> { sqlx::query_as::<_, PullRequest>( "SELECT id, repo_id, author_id, number, title, body, state, source_repo_id, \ source_branch, target_repo_id, target_branch, base_commit_sha, head_commit_sha, \ merge_commit_sha, draft, locked, merged_by, merged_at, closed_by, closed_at, \ created_at, updated_at, deleted_at \ FROM pull_request WHERE repo_id = $1 AND number = $2 AND deleted_at IS NULL", ) .bind(repo_id) .bind(number) .fetch_optional(pool) .await } pub async fn next_number<'e, E>(executor: E, repo_id: Uuid) -> Result where E: sqlx::PgExecutor<'e>, { sqlx::query_scalar( "SELECT COALESCE(MAX(number), 0) + 1 FROM pull_request WHERE repo_id = $1", ) .bind(repo_id) .fetch_one(executor) .await } }