use ::session::{AuthedUser, OptionalUser}; use api::http::{Path, Query, State}; use api::rest::AppResponseData; 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, } #[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, AuthedUser(uid): AuthedUser, Path(path): Path, ) -> Result, ApiError> { let (ctx, _) = resolve_reader( &service, Some(uid), path.space_name.clone(), path.repo_name.clone(), ) .await?; 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, AuthedUser(uid): AuthedUser, Path(path): Path, ) -> Result, ApiError> { let (ctx, _) = resolve_reader( &service, Some(uid), path.space_name.clone(), path.repo_name.clone(), ) .await?; 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, api::pagination::PageQuery), responses( (status = 200, description = "list stargazers", body = AppResponseData>) ) )] pub async fn list_stargazers( State(service): State, OptionalUser(viewer): OptionalUser, Path(path): Path, Query(q): Query, ) -> Result>, ApiError> { let ctx = resolve_reader( &service, viewer, path.space_name.clone(), path.repo_name.clone(), ) .await? .0; let (limit, offset) = q.bounds()?; let out = service .list_stargazers(&ctx, limit, offset) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) }