use actix_web::{HttpResponse, web}; use serde::Deserialize; use utoipa::IntoParams; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::models::issues::IssueTemplate; use crate::service::AppService; use crate::service::issues::templates::{CreateTemplateParams, UpdateTemplateParams}; use crate::session::Session; #[derive(Debug, Deserialize, IntoParams)] pub struct PathParams { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, } #[derive(Debug, Deserialize, IntoParams)] pub struct QueryParams { /// Maximum number of templates to return (default: 50, max: 100) pub limit: Option, /// Number of templates to skip for pagination (default: 0) pub offset: Option, } /// List issue templates in a repository /// /// Returns a paginated list of all active issue templates in the repository. /// Templates provide pre-filled content for creating new issues. /// Sorted alphabetically by name. /// Requires read access to the repository. #[utoipa::path( get, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/issues/templates", tag = "Issues", operation_id = "issueListTemplates", params(PathParams, QueryParams), responses( (status = 200, description = "Templates listed successfully. Returns array of template objects.", body = ApiResponse>), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions to access this repository", body = ApiErrorResponse), (status = 404, description = "Repository or workspace not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn list_templates( service: web::Data, session: Session, path: web::Path, query: web::Query, ) -> Result { let templates = service .issue .issue_templates( &session, &path.workspace_name, &path.repo_name, query.limit.unwrap_or(50), query.offset.unwrap_or(0), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(templates))) } /// Create an issue template /// /// Creates a new issue template in the repository. /// Requires at least Member role in the repository. /// /// Parameters: /// - name: Template name (required) /// - description: Template description (optional) /// - title_template: Default title for issues (optional, supports placeholders) /// - body_template: Default body content in markdown (required) /// - labels: List of label names to auto-apply (optional) /// /// Returns the created template with metadata. #[utoipa::path( post, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/issues/templates", tag = "Issues", operation_id = "issueCreateTemplate", params(PathParams), request_body( content = CreateTemplateParams, description = "Template creation parameters", content_type = "application/json" ), responses( (status = 201, description = "Template created successfully. Returns the newly created template.", body = ApiResponse), (status = 400, description = "Invalid parameters: empty name or body template", body = ApiErrorResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions (requires Member role or higher)", body = ApiErrorResponse), (status = 404, description = "Repository or workspace not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn create_template( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let template = service .issue .issue_create_template( &session, &path.workspace_name, &path.repo_name, params.into_inner(), ) .await?; Ok(HttpResponse::Created().json(ApiResponse::new(template))) } #[derive(Debug, Deserialize, IntoParams)] pub struct TemplatePathParams { /// Workspace name (unique identifier) pub workspace_name: String, /// Repository name (unique within the workspace) pub repo_name: String, /// Template ID (UUID) pub template_id: uuid::Uuid, } /// Update an issue template /// /// Updates an existing issue template's metadata and content. /// Requires Admin role in the repository. /// /// All fields are optional; only provided fields are updated. /// Returns the updated template. #[utoipa::path( put, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/issues/templates/{template_id}", tag = "Issues", operation_id = "issueUpdateTemplate", params(TemplatePathParams), request_body( content = UpdateTemplateParams, description = "Template update parameters (all fields optional)", content_type = "application/json" ), responses( (status = 200, description = "Template updated successfully. Returns the updated template.", body = ApiResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions (requires Admin role)", body = ApiErrorResponse), (status = 404, description = "Repository, workspace, or template not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn update_template( service: web::Data, session: Session, path: web::Path, params: web::Json, ) -> Result { let template = service .issue .issue_update_template( &session, &path.workspace_name, &path.repo_name, path.template_id, params.into_inner(), ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(template))) } /// Delete an issue template /// /// Permanently removes an issue template from the repository. /// Requires Admin role in the repository. /// /// Returns success message on completion. #[utoipa::path( delete, path = "/api/v1/workspaces/{workspace_name}/repos/{repo_name}/issues/templates/{template_id}", tag = "Issues", operation_id = "issueDeleteTemplate", params(TemplatePathParams), responses( (status = 200, description = "Template deleted successfully.", body = ApiResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 403, description = "Insufficient permissions (requires Admin role)", body = ApiErrorResponse), (status = 404, description = "Template not found", body = ApiErrorResponse), (status = 500, description = "Internal server error", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn delete_template( service: web::Data, session: Session, path: web::Path, ) -> Result { service .issue .issue_delete_template( &session, &path.workspace_name, &path.repo_name, path.template_id, ) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new("Template deleted".to_string()))) }