Skip to content

Commit

Permalink
Update api with CRUD.
Browse files Browse the repository at this point in the history
  • Loading branch information
MacKLess committed Dec 15, 2017
1 parent 9031363 commit 5d67f73
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions app/controllers/api/v1/cats_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Api::V1::CatsController < ApplicationController

def index
name = params[:name]
breed = params[:breed]
if breed
@cats = Cat.search_breed(breed)
else
@cats = Cat.all
end
json_response(@cats)
end

def create
@cat = Cat.create!(cat_params)
json_response(@cat, :created)
end

def update
@cat = Cat.find(params[:id])
if @cat.update!(cat_params)
render status: 200, json {
message: "Your cat has been updated successfully!"
}
end
end

def destroy
@cat = Cat.find(params[:id])
if @cat.destroy!
render status: 200, json: {
message: "This cat is no longer available for adoption. "
}
end
end

private
def cat_params
params.permit(:name, :breed)
end
end

0 comments on commit 5d67f73

Please sign in to comment.