feat: init
This commit is contained in:
@@ -0,0 +1,268 @@
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::models::common::Role;
|
||||
use crate::models::issues::{IssueLabel, IssueLabelRelation};
|
||||
use crate::service::IssueService;
|
||||
use crate::session::Session;
|
||||
|
||||
use super::util::{clamp_limit_offset, ensure_affected, required_text};
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct CreateLabelParams {
|
||||
pub name: String,
|
||||
pub color: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(serde::Deserialize, serde::Serialize, Clone, Debug, utoipa::ToSchema)]
|
||||
pub struct UpdateLabelParams {
|
||||
pub name: Option<String>,
|
||||
pub color: Option<String>,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
impl IssueService {
|
||||
pub(crate) async fn resolve_repo_id(
|
||||
&self,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
) -> Result<Uuid, AppError> {
|
||||
let ws = self.resolve_workspace(wk_name).await?;
|
||||
crate::models::repos::Repo::find_by_name(self.ctx.db.reader(), ws.id, repo_name)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.map(|r| r.id)
|
||||
.ok_or(AppError::NotFound("repo not found".into()))
|
||||
}
|
||||
|
||||
pub(crate) async fn ensure_repo_role(
|
||||
&self,
|
||||
repo_id: Uuid,
|
||||
user_uid: Uuid,
|
||||
min: Role,
|
||||
) -> Result<(), AppError> {
|
||||
let repo = crate::models::repos::Repo::find_by_id(self.ctx.db.reader(), repo_id)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("repo not found".into()))?;
|
||||
let role = crate::models::repos::Repo::user_role(
|
||||
self.ctx.db.reader(),
|
||||
repo.id,
|
||||
user_uid,
|
||||
repo.owner_id,
|
||||
)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.unwrap_or(Role::Unknown);
|
||||
if crate::service::util::role_level(role) < crate::service::util::role_level(min) {
|
||||
return Err(AppError::Unauthorized);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn issue_labels(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
) -> Result<Vec<IssueLabel>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo_id = self.resolve_repo_id(wk_name, repo_name).await?;
|
||||
let repo = crate::models::repos::Repo::find_by_id(self.ctx.db.reader(), repo_id)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("repo not found".into()))?;
|
||||
self.ensure_repo_readable(user_uid, &repo).await?;
|
||||
sqlx::query_as::<_, IssueLabel>(
|
||||
"SELECT id, repo_id, name, color, description, created_by, created_at, updated_at \
|
||||
FROM issue_label WHERE repo_id = $1 ORDER BY name ASC",
|
||||
)
|
||||
.bind(repo_id)
|
||||
.fetch_all(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn issue_create_label(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
params: CreateLabelParams,
|
||||
) -> Result<IssueLabel, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo_id = self.resolve_repo_id(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role(repo_id, user_uid, Role::Member)
|
||||
.await?;
|
||||
let name = required_text(params.name, "name")?;
|
||||
let color = required_text(params.color, "color")?;
|
||||
let now = chrono::Utc::now();
|
||||
sqlx::query_as::<_, IssueLabel>(
|
||||
"INSERT INTO issue_label (id, repo_id, name, color, description, created_by, created_at, updated_at) \
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $7) \
|
||||
RETURNING id, repo_id, name, color, description, created_by, created_at, updated_at",
|
||||
)
|
||||
.bind(Uuid::now_v7()).bind(repo_id).bind(&name).bind(&color)
|
||||
.bind(params.description.as_deref()).bind(user_uid).bind(now)
|
||||
.fetch_one(self.ctx.db.writer()).await.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn issue_update_label(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
label_id: Uuid,
|
||||
params: UpdateLabelParams,
|
||||
) -> Result<IssueLabel, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo_id = self.resolve_repo_id(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role(repo_id, user_uid, Role::Admin)
|
||||
.await?;
|
||||
let current = sqlx::query_as::<_, IssueLabel>(
|
||||
"SELECT id, repo_id, name, color, description, created_by, created_at, updated_at \
|
||||
FROM issue_label WHERE id = $1 AND repo_id = $2",
|
||||
)
|
||||
.bind(label_id)
|
||||
.bind(repo_id)
|
||||
.fetch_optional(self.ctx.db.reader())
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::NotFound("label not found".into()))?;
|
||||
|
||||
let name = params.name.unwrap_or(current.name);
|
||||
let color = params.color.unwrap_or(current.color);
|
||||
let description = super::util::merge_optional_text(params.description, current.description);
|
||||
let now = chrono::Utc::now();
|
||||
sqlx::query_as::<_, IssueLabel>(
|
||||
"UPDATE issue_label SET name = $1, color = $2, description = $3, updated_at = $4 \
|
||||
WHERE id = $5 RETURNING id, repo_id, name, color, description, created_by, created_at, updated_at",
|
||||
)
|
||||
.bind(&name).bind(&color).bind(&description).bind(now).bind(label_id)
|
||||
.fetch_one(self.ctx.db.writer()).await.map_err(AppError::Database)
|
||||
}
|
||||
|
||||
pub async fn issue_delete_label(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
repo_name: &str,
|
||||
label_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let repo_id = self.resolve_repo_id(wk_name, repo_name).await?;
|
||||
self.ensure_repo_role(repo_id, user_uid, Role::Admin)
|
||||
.await?;
|
||||
let result = sqlx::query("DELETE FROM issue_label WHERE id = $1 AND repo_id = $2")
|
||||
.bind(label_id)
|
||||
.bind(repo_id)
|
||||
.execute(self.ctx.db.writer())
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
ensure_affected(result.rows_affected(), "label not found")
|
||||
}
|
||||
|
||||
pub async fn issue_assign_label(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
number: i64,
|
||||
label_id: Uuid,
|
||||
) -> Result<IssueLabelRelation, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let issue = self.resolve_issue(wk_name, number).await?;
|
||||
let issue_id = issue.id;
|
||||
self.ensure_issue_editable(user_uid, &issue).await?;
|
||||
let now = chrono::Utc::now();
|
||||
let mut txn = self
|
||||
.ctx
|
||||
.db
|
||||
.writer()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
let rel = sqlx::query_as::<_, IssueLabelRelation>(
|
||||
"INSERT INTO issue_label_relation (id, issue_id, label_id, created_by, created_at) \
|
||||
VALUES ($1, $2, $3, $4, $5) ON CONFLICT (issue_id, label_id) DO NOTHING \
|
||||
RETURNING id, issue_id, label_id, created_by, created_at",
|
||||
)
|
||||
.bind(Uuid::now_v7())
|
||||
.bind(issue_id)
|
||||
.bind(label_id)
|
||||
.bind(user_uid)
|
||||
.bind(now)
|
||||
.fetch_optional(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?
|
||||
.ok_or(AppError::Conflict("label already assigned".into()))?;
|
||||
sqlx::query("UPDATE issue_stats SET labels_count = labels_count + 1, updated_at = $1 WHERE issue_id = $2")
|
||||
.bind(now).bind(issue_id).execute(&mut *txn).await.map_err(AppError::Database)?;
|
||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||
Ok(rel)
|
||||
}
|
||||
|
||||
pub async fn issue_unassign_label(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
number: i64,
|
||||
label_id: Uuid,
|
||||
) -> Result<(), AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let issue = self.resolve_issue(wk_name, number).await?;
|
||||
let issue_id = issue.id;
|
||||
self.ensure_issue_editable(user_uid, &issue).await?;
|
||||
let now = chrono::Utc::now();
|
||||
let mut txn = self
|
||||
.ctx
|
||||
.db
|
||||
.writer()
|
||||
.begin()
|
||||
.await
|
||||
.map_err(|_| AppError::TxnError)?;
|
||||
sqlx::query("SET LOCAL app.current_user_id = $1")
|
||||
.bind(user_uid)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
let result =
|
||||
sqlx::query("DELETE FROM issue_label_relation WHERE issue_id = $1 AND label_id = $2")
|
||||
.bind(issue_id)
|
||||
.bind(label_id)
|
||||
.execute(&mut *txn)
|
||||
.await
|
||||
.map_err(AppError::Database)?;
|
||||
ensure_affected(result.rows_affected(), "label not assigned")?;
|
||||
sqlx::query("UPDATE issue_stats SET labels_count = GREATEST(labels_count - 1, 0), updated_at = $1 WHERE issue_id = $2")
|
||||
.bind(now).bind(issue_id).execute(&mut *txn).await.map_err(AppError::Database)?;
|
||||
txn.commit().await.map_err(|_| AppError::TxnError)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn issue_label_relations(
|
||||
&self,
|
||||
ctx: &Session,
|
||||
wk_name: &str,
|
||||
number: i64,
|
||||
limit: i64,
|
||||
offset: i64,
|
||||
) -> Result<Vec<IssueLabelRelation>, AppError> {
|
||||
let user_uid = ctx.user().ok_or(AppError::Unauthorized)?;
|
||||
let issue = self.resolve_issue(wk_name, number).await?;
|
||||
let issue_id = issue.id;
|
||||
self.ensure_issue_readable(user_uid, &issue).await?;
|
||||
let (limit, offset) = clamp_limit_offset(limit, offset);
|
||||
sqlx::query_as::<_, IssueLabelRelation>(
|
||||
"SELECT id, issue_id, label_id, created_by, created_at \
|
||||
FROM issue_label_relation WHERE issue_id = $1 ORDER BY created_at ASC LIMIT $2 OFFSET $3",
|
||||
)
|
||||
.bind(issue_id).bind(limit).bind(offset)
|
||||
.fetch_all(self.ctx.db.reader()).await.map_err(AppError::Database)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user