Skip to content
Clone
149bec6
87 lines 2.5 KB Rust
use axum::Json;
use axum::extract::{Path, Query, State};
use export::rest::AppResponseData;
use models::common::watch_mode::WatchMode;
use serde::Deserialize;
use session_layer::Session;
use utoipa::IntoParams;
use crate::errors::{ApiError, GitApiError};
use crate::handler::{RepoPath, resolve_reader};
use crate::service::GitService;
use crate::service::watches::{RepoWatchDto, UpsertWatchCmd, WatcherDto};
#[derive(Debug, Clone, Deserialize, IntoParams)]
pub struct WatchQuery {
pub limit: Option<u64>,
pub offset: Option<u64>,
pub mode: Option<WatchMode>,
}
#[utoipa::path(
put,
path = "/repos/{space_name}/{repo_name}/watch",
tag = "repos",
description = "设置或更新当前用户对仓库的关注、不关注或忽略模式。",
params(RepoPath),
request_body = UpsertWatchCmd,
responses(
(status = 200, description = "upsert watch", body = AppResponseData<RepoWatchDto>)
)
)]
pub async fn upsert_watch(
State(service): State<GitService>,
session: Session,
Path(path): Path<RepoPath>,
Json(cmd): Json<UpsertWatchCmd>,
) -> Result<AppResponseData<RepoWatchDto>, ApiError> {
let (ctx, viewer) = resolve_reader(
&service,
&session,
path.space_name.clone(),
path.repo_name.clone(),
)
.await?;
let uid = viewer.ok_or_else(|| ApiError::from(GitApiError::Unauthorized))?;
let out = service
.upsert_watch(&ctx, uid, cmd)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}
#[utoipa::path(
get,
path = "/repos/{space_name}/{repo_name}/watchers",
tag = "repos",
description = "分页列出仓库关注者,并可按关注模式过滤。",
params(RepoPath, WatchQuery),
responses(
(status = 200, description = "list watchers", body = AppResponseData<Vec<WatcherDto>>)
)
)]
pub async fn list_watchers(
State(service): State<GitService>,
session: Session,
Path(path): Path<RepoPath>,
Query(q): Query<WatchQuery>,
) -> Result<AppResponseData<Vec<WatcherDto>>, ApiError> {
let ctx = resolve_reader(
&service,
&session,
path.space_name.clone(),
path.repo_name.clone(),
)
.await?
.0;
let limit = q.limit.unwrap_or(50).clamp(1, 100);
let offset = q
.offset
.unwrap_or(0)
.clamp(0, export::pagination::MAX_OFFSET);
let out = service
.list_watchers(&ctx, q.mode, limit, offset)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}