use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::prs::PrReaction; use crate::service::AppService; use crate::service::pr::reactions::CreateReactionParams; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PrPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// PR number (unique within the repository) pub number: i64, } #[derive(Debug, Deserialize, IntoParams)] pub struct ReactionPath { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// PR number (unique within the repository) pub number: i64, /// Reaction ID (UUID) pub reaction_id: uuid::Uuid, } #[derive(Debug, Deserialize, IntoParams)] pub struct QP { pub limit: Option, pub offset: Option, } /// List reactions on a PR #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/reactions", tag = "Pull Requests", operation_id = "prListReactions", params(PrPath, QP), responses( (status = 200, description = "Reactions listed.", body = ApiResponse>), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "PR not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn list_reactions( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let reactions = service .pr .pr_reactions( &session, &path.workspace_name, &path.repo_name, path.number, query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(reactions))) } /// Add a reaction to a PR #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/reactions", tag = "Pull Requests", operation_id = "prAddReaction", params(PrPath), request_body(content = CreateReactionParams, description = "Reaction parameters", content_type = "application/json"), responses( (status = 201, description = "Reaction added.", body = ApiResponse), (status = 400, description = "Invalid content or target type", body = ApiErrorResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 404, description = "PR not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn add_reaction( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let reaction = service .pr .pr_add_reaction( &session, &path.workspace_name, &path.repo_name, path.number, params.into_inner(), ) .await?; Ok(HttpResponse::Created().json(ApiResponse::new(reaction))) } /// Remove a reaction. Only the reaction author can remove it. #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/prs/{number}/reactions/{reaction_id}", tag = "Pull Requests", operation_id = "prRemoveReaction", params(ReactionPath), responses( (status = 200, description = "Reaction removed.", body = ApiResponse), (status = 401, description = "Authentication required", body = ApiErrorResponse), (status = 403, description = "Cannot remove another user's reaction", body = ApiErrorResponse), (status = 404, description = "Reaction not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security(("session_cookie" = [])) )] pub async fn remove_reaction( service: web::Data, session: Session, path: web::Path, ) -> Result { service .pr .pr_remove_reaction( &session, &path.workspace_name, &path.repo_name, path.number, path.reaction_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Reaction removed".to_string()))) }