From 4a0d910b9806f526cb2ef49ce86004cba3675993 Mon Sep 17 00:00:00 2001 From: KayKay-git Date: Tue, 19 Jan 2021 13:47:29 -0500 Subject: [PATCH 1/5] Initial set up --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index cb5b9b3e..96dffa92 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,8 @@ .node_modules package-lock.json node_modules +.idea +.env +.Gemfile +.Gemfile.lock From b57298b8715520fe804b3ab2de41b3a9ec9aa6f4 Mon Sep 17 00:00:00 2001 From: KayKay-git Date: Tue, 19 Jan 2021 19:55:55 -0500 Subject: [PATCH 2/5] Added Video Create Method with routes, tests and validations, pass --- app/controllers/videos_controller.rb | 14 +++ app/models/video.rb | 2 + config/routes.rb | 2 +- test/controllers/videos_controller_test.rb | 104 ++++++++++++++++----- 4 files changed, 100 insertions(+), 22 deletions(-) diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..462e3d45 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -21,6 +21,16 @@ def show ) end + def create + video = Video.new(video_params) + if video.save + render json: video.as_json(only: [:title, :overview, :release_date, :image_url, :external_id]), status: :created + return + else + render json: {errors: video.errors.messages}, status: :bad_request + end + end + private def require_video @@ -29,4 +39,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, :image_url, :external_id, :inventory) + end end diff --git a/app/models/video.rb b/app/models/video.rb index f47b7f0b..97d05fbd 100644 --- a/app/models/video.rb +++ b/app/models/video.rb @@ -2,6 +2,8 @@ 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 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" diff --git a/test/controllers/videos_controller_test.rb b/test/controllers/videos_controller_test.rb index 730915ed..795f178c 100644 --- a/test/controllers/videos_controller_test.rb +++ b/test/controllers/videos_controller_test.rb @@ -1,8 +1,8 @@ require 'test_helper' class VideosControllerTest < ActionDispatch::IntegrationTest - describe "index" do - it "returns a JSON array" do + describe 'index' do + it 'returns a JSON array' do get videos_url assert_response :success expect(@response.headers['Content-Type']).must_include 'json' @@ -12,18 +12,18 @@ class VideosControllerTest < ActionDispatch::IntegrationTest expect(data).must_be_kind_of Array end - it "should return many video fields" do + it 'should return many video fields' do get videos_url assert_response :success data = JSON.parse @response.body data.each do |video| - expect(video).must_include "title" - expect(video).must_include "release_date" + expect(video).must_include 'title' + expect(video).must_include 'release_date' end end - it "returns all videos when no query params are given" do + it 'returns all videos when no query params are given' do get videos_url assert_response :success @@ -32,21 +32,21 @@ class VideosControllerTest < ActionDispatch::IntegrationTest expected_names = {} Video.all.each do |video| - expected_names[video["title"]] = false + expected_names[video['title']] = false end data.each do |video| expect( - expected_names[video["title"]] + expected_names[video['title']] ).must_equal false, "Got back duplicate video #{video["title"]}" - expected_names[video["title"]] = true + expected_names[video['title']] = true end end end - describe "show" do - it "Returns a JSON object" do + describe 'show' do + it 'Returns a JSON object' do get video_url(title: videos(:one).title) assert_response :success expect(@response.headers['Content-Type']).must_include 'json' @@ -56,25 +56,87 @@ class VideosControllerTest < ActionDispatch::IntegrationTest expect(data).must_be_kind_of Hash end - it "Returns expected fields" do + it 'Returns expected fields' do get video_url(title: videos(:one).title) assert_response :success video = JSON.parse @response.body - expect(video).must_include "title" - expect(video).must_include "overview" - expect(video).must_include "release_date" - expect(video).must_include "inventory" - expect(video).must_include "available_inventory" + expect(video).must_include 'title' + expect(video).must_include 'overview' + expect(video).must_include 'release_date' + expect(video).must_include 'inventory' + expect(video).must_include 'available_inventory' end it "Returns an error when the video doesn't exist" do - get video_url(title: "does_not_exist") + get video_url(title: 'does_not_exist') assert_response :not_found data = JSON.parse @response.body - expect(data).must_include "errors" - expect(data["errors"]).must_include "title" + expect(data).must_include 'errors' + expect(data['errors']).must_include 'title' end end -end + + describe 'create' do + before do + # Arrange + @video_hash = { + title: 'Test Movie', + overview: 'A movie about testing code', + release_date: 'January 1, 2021', + inventory: 5, + image_url: 'https://upload.wikimedia.org/wikipedia/en/9/95/Test_image.jpg', + external_id: 112233 + } + end + it 'can create a valid video' do + # Assert + expect { + post videos_path, params: @video_hash + }.must_change 'Video.count', 1 + + must_respond_with :created + end + + it 'will respond with bad request and errors for an invalid movie' do + # Arrange + @video_hash[:title] = nil + + # Assert + expect { + post videos_path, params: @video_hash + }.wont_change "Video.count" + body = JSON.parse(response.body) + + expect(body.keys).must_include "errors" + expect(body["errors"].keys).must_include "title" + expect(body["errors"]["title"]).must_include "can't be blank" + + must_respond_with :bad_request + end + + it 'will not add video that is not in the db' do + # TODO + end + + it 'will not add the same video twice' do + same_video = { + title: 'Test Movie', + overview: 'A movie about testing code', + release_date: 'January 1, 2021', + inventory: 5, + image_url: 'https://upload.wikimedia.org/wikipedia/en/9/95/Test_image.jpg', + external_id: 112233 + } + + expect { + post videos_path, params: @video_hash + }.must_change 'Video.count', 1 + + expect { + post videos_path, params: same_video + }.wont_change "Video.count" + end + end +end \ No newline at end of file From 70f22b3a4249e35e7bd7c136f5dae4e0473cedea Mon Sep 17 00:00:00 2001 From: KayKay-git Date: Tue, 19 Jan 2021 22:19:10 -0500 Subject: [PATCH 3/5] Working on Deployment to heroku --- Gemfile.lock | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 91cf9988..ad055612 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,8 +76,8 @@ GEM coderay (1.1.3) concurrent-ruby (1.1.7) crass (1.0.6) - debase (0.2.4.1) - debase-ruby_core_source (>= 0.10.2) + debase (2.3.0) + debase-ruby_core_source (~> 0.10.10) debase-ruby_core_source (0.10.12) dotenv (2.7.6) dotenv-rails (2.7.6) @@ -126,7 +126,6 @@ GEM mime-types-data (3.2020.1104) mimemagic (0.3.5) mini_mime (1.0.2) - mini_portile2 (2.5.0) minitest (5.14.3) minitest-rails (6.1.0) minitest (~> 5.10) @@ -140,8 +139,7 @@ GEM multi_xml (0.6.0) nenv (0.3.0) nio4r (2.5.4) - nokogiri (1.11.1) - mini_portile2 (~> 2.5.0) + nokogiri (1.11.1-x86_64-darwin) racc (~> 1.4) notiffany (0.1.3) nenv (~> 0.1) @@ -190,8 +188,8 @@ GEM rb-fsevent (0.10.4) rb-inotify (0.10.1) ffi (~> 1.0) - ruby-debug-ide (0.7.2) - rake (>= 0.8.1) + ruby-debug-ide (2.3.0) + debase (~> 2.3.0) ruby-progressbar (1.11.0) shellany (0.0.1) spring (2.1.1) From 8c6bc8e8452774b141cbac017911a513aeb38feb Mon Sep 17 00:00:00 2001 From: KayKay-git Date: Tue, 19 Jan 2021 22:31:12 -0500 Subject: [PATCH 4/5] Testing for deployment --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 96dffa92..9001cc0b 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,5 @@ package-lock.json node_modules .idea .env -.Gemfile -.Gemfile.lock + From 4a41911bba610bca609e12eb4e116ed9e3e018c8 Mon Sep 17 00:00:00 2001 From: KayKay-git Date: Tue, 19 Jan 2021 22:33:08 -0500 Subject: [PATCH 5/5] Reversed changes --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 9001cc0b..96dffa92 100644 --- a/.gitignore +++ b/.gitignore @@ -30,5 +30,6 @@ package-lock.json node_modules .idea .env - +.Gemfile +.Gemfile.lock