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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ group :development do
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'
end


Expand Down
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class ApplicationController < ActionController::Base
#protect_from_forgery with: :exception
def render_error(status, errors)
render json: {errors: errors}, status: status
end
end
14 changes: 14 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,23 @@ def show
)
)
end
def create
newMovie = Movie.new(movie_params)

if newMovie.save
render json: {id: newMovie.id}
else
render_error(:bad_request, newMovie.errors.messages)
end
end


private

def movie_params
params.permit(:title, :overview, :release_date, :inventory, :external_id, :poster_path)
end

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
Expand Down
3 changes: 2 additions & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals
validates :title, presence: true, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end

def image_url
orig_value = read_attribute :image_url
orig_value = read_attribute :poster_path
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
elsif external_id
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/movie_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MovieSerializer < ActiveModel::Serializer
attribute :id, if: -> { object.id != nil }

attributes :title, :overview, :release_date, :image_url, :external_id
attributes :title, :overview, :release_date, :image_url, :external_id, :poster_path
end
9 changes: 5 additions & 4 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ test:

production:
<<: *default
database: <APPLICATION_NAME>_production
username: <APPLICATION_NAME>
password: <%= ENV['<APPLICATION_NAME>_DATABASE_PASSWORD'] %>

database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
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: [: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
5 changes: 5 additions & 0 deletions db/migrate/20181218222308_rename_image_url_poster_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenameImageUrlPosterPath < ActiveRecord::Migration[5.2]
def change
rename_column :movies, :image_url, :poster_path
end
end
53 changes: 28 additions & 25 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180618042754) do
ActiveRecord::Schema.define(version: 2018_12_18_222308) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "customers", force: :cascade do |t|
t.string "name"
t.string "name"
t.datetime "registered_at"
t.string "address"
t.string "city"
t.string "state"
t.string "postal_code"
t.string "phone"
t.float "account_credit"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "address"
t.string "city"
t.string "state"
t.string "postal_code"
t.string "phone"
t.float "account_credit"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "movies", force: :cascade do |t|
t.string "title"
t.text "overview"
t.date "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_url"
t.integer "external_id"
t.string "title"
t.text "overview"
t.date "release_date"
t.integer "inventory"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "poster_path"
t.integer "external_id"
end

create_table "rentals", force: :cascade do |t|
t.integer "customer_id"
t.integer "movie_id"
t.date "checkout_date"
t.date "due_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "returned"
t.integer "customer_id"
t.integer "movie_id"
t.date "checkout_date"
t.date "due_date"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "returned"
t.index ["customer_id"], name: "index_rentals_on_customer_id"
t.index ["movie_id"], name: "index_rentals_on_movie_id"
end
Expand Down
7 changes: 3 additions & 4 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ class MovieWrapper

def self.search(query)
url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query
puts url
# puts url

response = HTTParty.get(url)
if response["total_results"] == 0
return []
else
puts response
movies = response["results"].map do |result|
self.construct_movie(result)
end
Expand All @@ -26,11 +24,12 @@ def self.search(query)
private

def self.construct_movie(api_result)

Movie.new(
title: api_result["title"],
overview: api_result["overview"],
release_date: api_result["release_date"],
image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),
poster_path: api_result["poster_path"],
external_id: api_result["id"])
end

Expand Down
44 changes: 44 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,48 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest

end
end

describe "create" do
let(:movie_data){
{
"title" => "Test Movie"
}
}

it "create a new a movie given valid data" do

expect{
post movies_path, params: movie_data
}.must_change "Movie.count", 1

must_respond_with :success


body = JSON.parse(response.body)
movie = Movie.find(body["id"].to_i)


expect(body).must_be_kind_of Hash
expect(body).must_include "id"

expect(movie.title).must_equal movie_data["title"]

end

it "returns an error for invalid movie data" do
# arrange
movie_data["title"] = nil

expect {
post movies_path, params: movie_data
}.wont_change "Movie.count"

body = JSON.parse(response.body)

expect(body).must_be_kind_of Hash
expect(body).must_include "errors"
# expect(body["errors"]).must_include "title"
must_respond_with :bad_request
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require "rails/test_help"
require "minitest/rails"
require "minitest/reporters" # for Colorized output

require "pry"

# For colorful output!
Minitest::Reporters.use!(
Expand Down