From 8d479ef9946061ceed4c821dfc25ee50642b1519 Mon Sep 17 00:00:00 2001 From: Hanh Solo Date: Wed, 20 Jan 2021 17:48:27 -0800 Subject: [PATCH 1/2] Create method to add video in video controller --- app/controllers/videos_controller.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..33e2935d 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -21,6 +21,23 @@ def show ) end + def create + video = Video.new(video_params) + if video.save + render( + status: :ok, + # json: video.as_json, + # only: [:title, :overview, :release_date, :inventory, :image_url] + ) + else + render( + status: :bad_request, + errors: video.errors + ) + end + + end + private def require_video @@ -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, :image_url, :external_id) + end end From 1eb69307fa798934e2772e7be0415e58980212bd Mon Sep 17 00:00:00 2001 From: Hanh Solo Date: Thu, 21 Jan 2021 14:33:50 -0800 Subject: [PATCH 2/2] Create method updated --- app/controllers/videos_controller.rb | 28 ++++++++++++++-------------- app/models/video.rb | 2 +- config/routes.rb | 2 +- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index 33e2935d..35bd0b98 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -22,20 +22,20 @@ def show end def create - video = Video.new(video_params) - if video.save - render( - status: :ok, - # json: video.as_json, - # only: [:title, :overview, :release_date, :inventory, :image_url] - ) - else - render( - status: :bad_request, - errors: video.errors - ) + 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 @@ -48,6 +48,6 @@ def require_video end def video_params - params.permit(:title, :overview, :release_date, :inventory, :image_url, :external_id) + params.permit(:title, :overview, :release_date, :inventory, :created_at, :updated_at, :image_url, :external_id) end end diff --git a/app/models/video.rb b/app/models/video.rb index f47b7f0b..fbf0fe07 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 16fc2214..1111b4bf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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"