Class: CompletionKit::Api::V1::PromptsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/completion_kit/api/v1/prompts_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



17
18
19
20
21
22
23
24
# File 'app/controllers/completion_kit/api/v1/prompts_controller.rb', line 17

def create
  prompt = Prompt.new(prompt_params)
  if prompt.save
    render json: prompt, status: :created
  else
    render json: {errors: prompt.errors}, status: :unprocessable_entity
  end
end

#destroyObject



39
40
41
42
# File 'app/controllers/completion_kit/api/v1/prompts_controller.rb', line 39

def destroy
  @prompt.destroy!
  head :no_content
end

#indexObject



7
8
9
10
11
# File 'app/controllers/completion_kit/api/v1/prompts_controller.rb', line 7

def index
  scope = Prompt.includes(:tags)
  scope = filter_by_tags(scope)
  render json: paginate(scope.order(created_at: :desc))
end

#publishObject



44
45
46
47
# File 'app/controllers/completion_kit/api/v1/prompts_controller.rb', line 44

def publish
  @prompt.publish!
  render json: @prompt.reload
end

#showObject



13
14
15
# File 'app/controllers/completion_kit/api/v1/prompts_controller.rb', line 13

def show
  render json: @prompt
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/completion_kit/api/v1/prompts_controller.rb', line 26

def update
  if @prompt.runs.exists?
    new_prompt = @prompt.clone_as_new_version(prompt_params.except(:tag_names).to_h)
    new_prompt.publish!
    new_prompt.update!(tag_names: prompt_params[:tag_names]) if prompt_params.key?(:tag_names)
    render json: new_prompt.reload
  elsif @prompt.update(prompt_params)
    render json: @prompt
  else
    render json: {errors: @prompt.errors}, status: :unprocessable_entity
  end
end