use axum::extract::{Path, Query, State}; use export::rest::AppResponseData; use jwt::Session; use serde::Deserialize; use utoipa::IntoParams; use crate::errors::{ApiError, GitApiError}; use crate::handler::{RepoPath, resolve_reader}; use crate::service::GitService; use crate::service::stars::{RepoStarResp, StargazerDto}; #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct StarsPath { pub space_name: String, pub repo_name: String, } #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct StarQuery { pub limit: Option, pub offset: Option, } #[utoipa::path( put, path = "/repos/{space_name}/{repo_name}/star", tag = "repos", description = "为仓库加星并返回加星后的总星数。", params(RepoPath), responses( (status = 200, description = "star repo", body = AppResponseData) ) )] pub async fn star_repo( State(service): State, session: Session, Path(path): Path, ) -> Result, 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 .star_repo(&ctx, uid) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( delete, path = "/repos/{space_name}/{repo_name}/star", tag = "repos", description = "取消当前用户对仓库的加星并返回最新星数。", params(RepoPath), responses( (status = 200, description = "unstar repo", body = AppResponseData) ) )] pub async fn unstar_repo( State(service): State, session: Session, Path(path): Path, ) -> Result, 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 .unstar_repo(&ctx, uid) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( get, path = "/repos/{space_name}/{repo_name}/stargazers", tag = "repos", description = "分页列出最近为仓库加星的用户。", params(RepoPath, StarQuery), responses( (status = 200, description = "list stargazers", body = AppResponseData>) ) )] pub async fn list_stargazers( State(service): State, session: Session, Path(path): Path, Query(q): Query, ) -> Result>, 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_stargazers(&ctx, limit, offset) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) }