Class: ActionAgent::Api::TemplatesController

Inherits:
BaseController show all
Defined in:
app/controllers/action_agent/api/templates_controller.rb

Instance Method Summary collapse

Methods inherited from ActionAgent::ApplicationController

allow_unauthenticated_access

Instance Method Details

#indexObject

GET /api/templates



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/action_agent/api/templates_controller.rb', line 12

def index
  @templates = AgentTemplate.public_templates.order(usage_count: :desc)

  # Filter by category
  @templates = @templates.by_category(params[:category]) if params[:category].present?

  # Featured only
  @templates = @templates.featured if params[:featured].present?

  render json: {
    templates: @templates.map { |t| template_json(t) },
    categories: AgentTemplate::CATEGORIES
  }
end

#showObject

GET /api/templates/:id



28
29
30
31
# File 'app/controllers/action_agent/api/templates_controller.rb', line 28

def show
  @template = AgentTemplate.find(params[:id])
  render json: { template: template_json(@template, include_details: true) }
end

#useObject

POST /api/templates/:id/use



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/action_agent/api/templates_controller.rb', line 34

def use
  @template = AgentTemplate.find(params[:id])

  agent = @template.create_agent_for(
    current_user,
    name: params[:name] || @template.name
  )

  if agent.persisted?
    render json: { agent: agent_json(agent) }, status: :created
  else
    render json: { errors: agent.errors.full_messages }, status: :unprocessable_entity
  end
end