149bec6
use axum::Json;
use axum::extract::{Path, State};
use export::rest::{AppResponseData, NoData};
use session_layer::Session;
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<CodeCommentDto>)
)
)]
pub async fn update_code_comment(
State(service): State<GitService>,
session: Session,
Path(path): Path<CodeCommentPath>,
Json(cmd): Json<UpdateCodeCommentCmd>,
) -> Result<AppResponseData<CodeCommentDto>, 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(
path.space_name,
path.repo_name,
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<NoData>)
)
)]
pub async fn delete_code_comment(
State(service): State<GitService>,
session: Session,
Path(path): Path<CodeCommentPath>,
) -> Result<AppResponseData<NoData>, 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(path.space_name, path.repo_name, path.comment_id, uid, admin)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(NoData))
}