From f0c5c4cb61d4ac54a7a5b9c16630769fb93f24b8 Mon Sep 17 00:00:00 2001 From: Madeline Date: Thu, 21 Jan 2021 15:53:24 -0800 Subject: [PATCH 1/3] added video create method --- .idea/.generators | 8 ++ .idea/.gitignore | 6 + .idea/inspectionProfiles/Project_Default.xml | 6 + .idea/misc.xml | 4 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + .idea/video-store-consumer-api.iml | 121 +++++++++++++++++++ app/controllers/videos_controller.rb | 21 ++++ config/routes.rb | 2 +- 9 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 .idea/.generators create mode 100644 .idea/.gitignore create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/video-store-consumer-api.iml diff --git a/.idea/.generators b/.idea/.generators new file mode 100644 index 00000000..bd02a3a3 --- /dev/null +++ b/.idea/.generators @@ -0,0 +1,8 @@ + + diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..8bf4d45d --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,6 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..b0db9b0f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..510e7fcc --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..f00f0a95 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/video-store-consumer-api.iml b/.idea/video-store-consumer-api.iml new file mode 100644 index 00000000..4d9d070b --- /dev/null +++ b/.idea/video-store-consumer-api.iml @@ -0,0 +1,121 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..e50ec0b1 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( + json: @video.as_json(only: [:title, :overview, :release_date, :inventory, :image_url, :external_id), + status: :ok + return + else + render json: { + errors: @video.errors.messages, + ok: false, + }, status: :bad_request + return + 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 + return params.permit(:title, :overview, :release_date, :inventory, :image_url, :external_id) + end end 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" From f6278ec5b5aa33e34965ce93701bc061b380d147 Mon Sep 17 00:00:00 2001 From: roshni-patel Date: Thu, 21 Jan 2021 21:49:15 -0600 Subject: [PATCH 2/3] Fixed syntax error --- app/controllers/videos_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index e50ec0b1..1f6aa414 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -26,8 +26,8 @@ def create if @video.save render( - json: @video.as_json(only: [:title, :overview, :release_date, :inventory, :image_url, :external_id), - status: :ok + json: @video.as_json(only: [:title, :overview, :release_date, :inventory, :image_url, :external_id]), + status: :ok) return else render json: { From ea0c9f63c95dfee918e014fe7bea51a2dc469aa5 Mon Sep 17 00:00:00 2001 From: roshni-patel Date: Fri, 22 Jan 2021 01:32:45 -0600 Subject: [PATCH 3/3] Added test for create method --- app/models/video.rb | 1 + test/controllers/videos_controller_test.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/models/video.rb b/app/models/video.rb index f47b7f0b..ff7124fe 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -1,6 +1,7 @@ class Video < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :title, presence: true, uniqueness: true def available_inventory self.inventory - self.rentals.where(returned: false).length diff --git a/test/controllers/videos_controller_test.rb b/test/controllers/videos_controller_test.rb index 730915ed..7e438684 100644 --- a/test/controllers/videos_controller_test.rb +++ b/test/controllers/videos_controller_test.rb @@ -77,4 +77,25 @@ class VideosControllerTest < ActionDispatch::IntegrationTest expect(data["errors"]).must_include "title" end end + + describe "create" do + let(:video_hash) { + { + title: "Alf the movie", + overview: "The most early 90s movie of all time", + release_date: "December 16th 2025", + inventory: 10, + img_url: "http://google.com/images'", + external_id: 123, + } + } + + it "creates a video" do + expect { + post videos_path, params: video_hash + }.must_change 'Video.count', 1 + + must_respond_with :success + end + end end