use ::session::Session; use export::http::{Json, Path, State}; use export::rest::{AppResponseData, NoData}; use uuid::Uuid; use crate::errors::{ApiError, GitApiError}; use crate::handler::{require_repo_admin, require_writer}; use crate::service::GitService; use crate::service::code_comments::{CodeCommentDto, UpdateCodeCommentCmd}; #[derive(Debug, Clone, serde::Deserialize, utoipa::IntoParams)] pub struct CodeCommentPath { pub space_name: String, pub repo_name: String, pub comment_id: Uuid, } #[utoipa::path( patch, path = "/repos/{space_name}/{repo_name}/code-comments/{comment_id}", tag = "code-comments", description = "修改行级评论正文;仅作者或仓库管理员可操作。", params(CodeCommentPath), request_body = UpdateCodeCommentCmd, responses( (status = 200, description = "updated code comment", body = AppResponseData) ) )] pub async fn update_code_comment( State(service): State, session: Session, Path(path): Path, Json(cmd): Json, ) -> Result, ApiError> { let (ctx, uid) = require_writer( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await?; let admin = require_repo_admin( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await .is_ok(); let out = service .update_code_comment(&ctx, path.comment_id, uid, admin, cmd) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( delete, path = "/repos/{space_name}/{repo_name}/code-comments/{comment_id}", tag = "code-comments", description = "删除行级评论及其回复;仅作者或仓库管理员可操作。", params(CodeCommentPath), responses( (status = 200, description = "code comment deleted", body = AppResponseData) ) )] pub async fn delete_code_comment( State(service): State, session: Session, Path(path): Path, ) -> Result, ApiError> { let (ctx, uid) = require_writer( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await?; let admin = require_repo_admin( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await .is_ok(); service .delete_code_comment(&ctx, path.comment_id, uid, admin) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(NoData)) }