Skip to content
Clone
9818851
89 lines 2.6 KB Rust
use ::session::Session;
use api::http::{Json, Path, Query, State};
use api::rest::AppResponseData;
use models::common::watch_mode::WatchMode;
use serde::Deserialize;
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)]
#[serde(deny_unknown_fields)]
pub struct WatchQuery {
pub limit: Option<u64>,
pub offset: Option<u64>,
pub mode: Option<WatchMode>,
}
impl WatchQuery {
fn bounds(&self) -> Result<(u64, u64), api::pagination::PageError> {
api::pagination::page_bounds(self.limit, self.offset, 50, 100)
}
}
#[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<api::pagination::Page<WatcherDto>>)
)
)]
pub async fn list_watchers(
State(service): State<GitService>,
session: Session,
Path(path): Path<RepoPath>,
Query(q): Query<WatchQuery>,
) -> Result<AppResponseData<api::pagination::Page<WatcherDto>>, ApiError> {
let ctx = resolve_reader(
&service,
&session,
path.space_name.clone(),
path.repo_name.clone(),
)
.await?
.0;
let (limit, offset) = q.bounds()?;
let out = service
.list_watchers(&ctx, q.mode, limit, offset)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}