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

Leaves - Emily and Raisah #2

Open
wants to merge 9 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
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: ruby
rvm:
- 2.2
- jruby
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ GEM
method_source
rake (>= 0.8.7)
thor (>= 0.19.0, < 2.0)
rake (12.3.2)
rake (13.0.0)
rb-fsevent (0.10.3)
rb-inotify (0.10.0)
ffi (~> 1.0)
Expand Down Expand Up @@ -197,4 +197,4 @@ RUBY VERSION
ruby 2.5.5p157

BUNDLED WITH
1.17.3
2.0.2
25 changes: 19 additions & 6 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]


def create
new_movie = MovieWrapper.construct_movie(params)
new_movie.inventory = 10
new_movie.image_url = params["image_url"]
new_movie.external_id = params["external_id"]

if new_movie.save
render status: :ok, json: {}
else
render status: :bad_request, json: { errors: new_movie.errors.messages }
end
end

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

render status: :ok, json: data
end

def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
)
end

private

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
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 :movies, only: [:index, :show], param: :title
resources :movies, only: [:create, :index, :show], 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
5 changes: 4 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'dotenv'
Dotenv.load

class MovieWrapper
BASE_URL = "https://api.themoviedb.org/3/"
KEY = ENV["MOVIEDB_KEY"]
KEY = ENV['MOVIEDB_KEY']

BASE_IMG_URL = "https://image.tmdb.org/t/p/"
DEFAULT_IMG_SIZE = "w185"
Expand Down
18 changes: 18 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
require 'test_helper'

class MoviesControllerTest < ActionDispatch::IntegrationTest
describe "create" do
it "saves movie with valid parameters" do
params = {
title: "New Movie",
overview: "Some text",
release_date: "1960-06-16",
inventory: 8
}

start_count = Movie.count
post movies_path(params)
expect(Movie.count).must_equal start_count + 1
expect(Movie.last.title).must_equal params[:title]

must_respond_with :success
end
end

describe "index" do
it "returns a JSON array" do
get movies_url
Expand Down