use axum::Json; use axum::extract::{Path, Query, State}; use export::rest::{AppRequestData, AppResponseData}; use models::common::watch_mode::WatchMode; use serde::Deserialize; use session::Session; use utoipa::IntoParams; use crate::errors::{ApiError, GitApiError}; use crate::handler::{RepoPath, require_writer}; use crate::service::GitService; use crate::service::watches::{RepoWatchDto, UpsertWatchCmd, WatcherDto}; #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct WatchQuery { pub limit: Option, pub offset: Option, pub mode: Option, } #[utoipa::path( put, path = "/repos/{space_name}/{repo_name}/watch", tag = "repos", params(RepoPath), request_body = UpsertWatchCmd, responses( (status = 200, description = "upsert watch", body = AppResponseData) ) )] pub async fn upsert_watch( State(service): State, session: Session, Path(path): Path, Json(cmd): Json, ) -> Result, ApiError> { let (ctx, uid) = require_writer( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await?; let out = service .upsert_watch(&ctx, uid, cmd) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( delete, path = "/repos/{space_name}/{repo_name}/watch", tag = "repos", params(RepoPath), responses( (status = 200, description = "unwatch repo", body = AppResponseData) ) )] pub async fn unwatch( State(service): State, session: Session, Path(path): Path, ) -> Result, ApiError> { let (ctx, uid) = require_writer( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await?; service .unwatch(&ctx, uid) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(RepoWatchDto { user_id: uid, repo_id: ctx.model.id, mode: WatchMode::NoWatch, created_at: time::OffsetDateTime::now_utc(), updated_at: time::OffsetDateTime::now_utc(), })) } #[utoipa::path( get, path = "/repos/{space_name}/{repo_name}/watch", tag = "repos", params(RepoPath), responses( (status = 200, description = "current watch mode", body = AppResponseData>) ) )] pub async fn get_watch_mode( State(service): State, session: Session, Path(path): Path, ) -> Result>, ApiError> { let (ctx, uid) = require_writer( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await?; let mode = service .get_watch_mode(&ctx, uid) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(mode)) } #[utoipa::path( get, path = "/repos/{space_name}/{repo_name}/watchers", tag = "repos", params(RepoPath, WatchQuery), responses( (status = 200, description = "list watchers", body = AppResponseData>) ) )] pub async fn list_watchers( State(service): State, session: Session, Path(path): Path, Query(q): Query, ) -> Result>, ApiError> { let (ctx, _uid) = require_writer( &service, &session, path.space_name.clone(), path.repo_name.clone(), ) .await?; let limit = q.limit.unwrap_or(50).clamp(1, 100); let offset = q.offset.unwrap_or(0); let out = service .list_watchers(&ctx, q.mode, limit, offset) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[allow(dead_code)] fn _typecheck(_: AppRequestData) {}