gitdataai/libs/api/git/star.rs
2026-04-14 19:02:01 +08:00

150 lines
5.1 KiB
Rust

use crate::{ApiResponse, error::ApiError};
use actix_web::{HttpResponse, Result, web};
use service::AppService;
use service::git::star::{StarCountResponse, StarUserListResponse};
use session::Session;
#[derive(serde::Deserialize, utoipa::IntoParams)]
pub struct StarPagerQuery {
pub page: Option<i64>,
pub par_page: Option<i64>,
}
impl From<StarPagerQuery> for service::Pager {
fn from(q: StarPagerQuery) -> Self {
service::Pager {
page: q.page.unwrap_or(1),
par_page: q.par_page.unwrap_or(20),
}
}
}
#[utoipa::path(
post,
path = "/api/repos/{namespace}/{repo}/git/star",
params(
("namespace" = String, Path, description = "Project namespace"),
("repo" = String, Path, description = "Repository name"),
),
responses(
(status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
(status = 200, description = "Star the repository"),
(status = 404, description = "Not found", body = ApiResponse<ApiError>),
),
tag = "Git"
)]
pub async fn git_star(
service: web::Data<AppService>,
session: Session,
path: web::Path<(String, String)>,
) -> Result<HttpResponse, ApiError> {
let (namespace, repo_name) = path.into_inner();
service.git_star(namespace, repo_name, &session).await?;
Ok(crate::api_success())
}
#[utoipa::path(
delete,
path = "/api/repos/{namespace}/{repo}/git/star",
params(
("namespace" = String, Path, description = "Project namespace"),
("repo" = String, Path, description = "Repository name"),
),
responses(
(status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
(status = 200, description = "Unstar the repository"),
(status = 404, description = "Not found", body = ApiResponse<ApiError>),
),
tag = "Git"
)]
pub async fn git_unstar(
service: web::Data<AppService>,
session: Session,
path: web::Path<(String, String)>,
) -> Result<HttpResponse, ApiError> {
let (namespace, repo_name) = path.into_inner();
service.git_unstar(namespace, repo_name, &session).await?;
Ok(crate::api_success())
}
#[utoipa::path(
get,
path = "/api/repos/{namespace}/{repo}/git/star/is-starred",
params(
("namespace" = String, Path, description = "Project namespace"),
("repo" = String, Path, description = "Repository name"),
),
responses(
(status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
(status = 200, description = "Check if the current user has starred the repository", body = ApiResponse<serde_json::Value>),
(status = 404, description = "Not found", body = ApiResponse<ApiError>),
),
tag = "Git"
)]
pub async fn git_is_starred(
service: web::Data<AppService>,
session: Session,
path: web::Path<(String, String)>,
) -> Result<HttpResponse, ApiError> {
let (namespace, repo_name) = path.into_inner();
let resp = service
.git_is_starred(namespace, repo_name, &session)
.await?;
Ok(ApiResponse::ok(serde_json::json!({"is_starred": resp})).to_response())
}
#[utoipa::path(
get,
path = "/api/repos/{namespace}/{repo}/git/star/count",
params(
("namespace" = String, Path, description = "Project namespace"),
("repo" = String, Path, description = "Repository name"),
),
responses(
(status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
(status = 200, description = "Get star count for the repository", body = ApiResponse<StarCountResponse>),
(status = 404, description = "Not found", body = ApiResponse<ApiError>),
),
tag = "Git"
)]
pub async fn git_star_count(
service: web::Data<AppService>,
session: Session,
path: web::Path<(String, String)>,
) -> Result<HttpResponse, ApiError> {
let (namespace, repo_name) = path.into_inner();
let resp = service
.git_star_count(namespace, repo_name, &session)
.await?;
Ok(ApiResponse::ok(resp).to_response())
}
#[utoipa::path(
get,
path = "/api/repos/{namespace}/{repo}/git/star/users",
params(
("namespace" = String, Path, description = "Project namespace"),
("repo" = String, Path, description = "Repository name"),
("page" = Option<i64>, Query, description = "Page number"),
("per_page" = Option<i64>, Query, description = "Items per page"),
),
responses(
(status = 401, description = "Unauthorized", body = ApiResponse<ApiError>),
(status = 200, description = "List users who starred the repository", body = ApiResponse<StarUserListResponse>),
(status = 404, description = "Not found", body = ApiResponse<ApiError>),
),
tag = "Git"
)]
pub async fn git_star_user_list(
service: web::Data<AppService>,
session: Session,
path: web::Path<(String, String)>,
query: web::Query<StarPagerQuery>,
) -> Result<HttpResponse, ApiError> {
let (namespace, repo_name) = path.into_inner();
let resp = service
.git_star_user_list(namespace, repo_name, query.into_inner().into(), &session)
.await?;
Ok(ApiResponse::ok(resp).to_response())
}