Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Water @ Hanh-Iris -Video Store API #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def show
)
end

def create
unless Video.find_by(external_id: params[:external_id])
video = Video.new(video_params)
if video.save
render(
status: :ok,
json: video.as_json
)
else
render(
status: :bad_request,
errors: video.errors
)
end
end
end

private

def require_video
Expand All @@ -29,4 +46,8 @@ def require_video
render status: :not_found, json: { errors: { title: ["No video with title #{params["title"]}"] } }
end
end

def video_params
params.permit(:title, :overview, :release_date, :inventory, :created_at, :updated_at, :image_url, :external_id)
end
end
2 changes: 1 addition & 1 deletion app/models/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Video < ApplicationRecord
def available_inventory
self.inventory - self.rentals.where(returned: false).length
end

def image_url
raw_value = read_attribute :image_url
if !raw_value
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

resources :customers, only: [:index]

resources :videos, only: [:index, :show], param: :title
resources :videos, only: [:index, :show, :create], param: :title

post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out"
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
Expand Down