Skip to content
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
8 changes: 8 additions & 0 deletions .generators
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<Settings><!--This file was automatically generated by Ruby plugin.
You are allowed to:
1. Reorder generators
2. Remove generators
3. Add installed generators
To add new installed generators automatically delete this file and reload the project.
--><GeneratorsGroup><Generator name="active_record:migration" /><Generator name="active_record:model" /><Generator name="active_record:observer" /><Generator name="active_record:session_migration" /><Generator name="controller" /><Generator name="erb:controller" /><Generator name="erb:mailer" /><Generator name="erb:scaffold" /><Generator name="generator" /><Generator name="helper" /><Generator name="integration_test" /><Generator name="mailer" /><Generator name="metal" /><Generator name="migration" /><Generator name="model" /><Generator name="model_subclass" /><Generator name="observer" /><Generator name="performance_test" /><Generator name="plugin" /><Generator name="resource" /><Generator name="scaffold" /><Generator name="scaffold_controller" /><Generator name="session_migration" /><Generator name="stylesheets" /><Generator name="test_unit:controller" /><Generator name="test_unit:helper" /><Generator name="test_unit:integration" /><Generator name="test_unit:mailer" /><Generator name="test_unit:model" /><Generator name="test_unit:observer" /><Generator name="test_unit:performance" /><Generator name="test_unit:plugin" /><Generator name="test_unit:scaffold" /></GeneratorsGroup></Settings>
8 changes: 8 additions & 0 deletions .idea/.generators

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/.rakeTasks

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

121 changes: 121 additions & 0 deletions .idea/video-store-consumer-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 28 additions & 6 deletions app/controllers/videos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ class VideosController < ApplicationController
before_action :require_video, only: [:show]

def index
if params[:query]
data = VideoWrapper.search(params[:query])
data = if params[:query]
VideoWrapper.search(params[:query])
else
data = Video.all
end
Video.all
end

render status: :ok, json: data
end
Expand All @@ -15,18 +15,40 @@ def show
render(
status: :ok,
json: @video.as_json(
only: [:title, :overview, :release_date, :inventory],
only: %i[title overview release_date inventory],
methods: [:available_inventory]
)
)
end

def create
video = Video.new(video_params)
result = video.save
if result
render(
status: :ok,
json: result.as_json(
only: %i[title overview release_date inventory],
methods: [:available_inventory]
)
)
else
render status: :bad_request, json: {
errors: video.errors.messages
}
end
end

private

def require_video
@video = Video.find_by(title: params[:title])
unless @video
render status: :not_found, json: { errors: { title: ["No video with title #{params["title"]}"] } }
render status: :not_found, json: { errors: { title: ["No video with title #{params['title']}"] } }
end
end

def video_params
return params.permit(:title, :release_date, :overview, :inventory, :image_url, :external_id)
end
end
3 changes: 3 additions & 0 deletions app/models/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class Video < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :external_id, uniqueness: true
validates :title, presence: true

def available_inventory
self.inventory - self.rentals.where(returned: false).length
end
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
42 changes: 42 additions & 0 deletions test/controllers/videos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'test_helper'

class VideosControllerTest < ActionDispatch::IntegrationTest

describe "index" do
it "returns a JSON array" do
get videos_url
Expand Down Expand Up @@ -77,4 +78,45 @@ class VideosControllerTest < ActionDispatch::IntegrationTest
expect(data["errors"]).must_include "title"
end
end

describe "create" do
it "creates a video" do
video1 = {
"title": 'Hidden Figures',
"overview": 'Some text',
"release_date": '1960-06-16',
"inventory": 8,
"external_id": 99999
}

expect {
post videos_path, params: video1
}.must_differ "Video.count", 1
end

it "won't add duplicate videos" do
video1 = {
"title": 'Hidden Figures',
"overview": 'Some text',
"release_date": '1960-06-16',
"inventory": 8,
"external_id": 99999
}

post videos_path, params: video1

video2 = {
"title": 'Video',
"overview": 'Some new text',
"release_date": '1990-06-16',
"inventory": 4,
"external_id": 99999
}

expect {
post videos_path, params: video2
}.wont_differ "Video.count"
end
end

end
Loading