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
25 changes: 25 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ def show
)
end

def create
movie = Movie.new(title: params[:title], overview: params[:overview], release_date: params[:release_date], inventory: params[:inventory], image_url: params[:image_url])

# movie.inventory ||= rand(8)

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

def destroy
movie = Movie.find_by(id: params[:id])
if movie
if movie.destroy
render json: {id: movie.id}, status: :ok
else
render json: { errors: movie.errors.messages }, status: :bad_request
end
else
render json: { errors: "This Movie does not exist in the database" }, status: :not_found
end
end

private

def require_movie
Expand Down
37 changes: 29 additions & 8 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
class RentalsController < ApplicationController
before_action :require_movie, only: [:check_out, :check_in]
before_action :require_customer, only: [:check_out, :check_in]
before_action :require_customer, only: [:check_out, :check_in, :customer_rental]

def index
data = Rental.all
render status: :ok, json: data
end

def customer_rental
rentals = Rental.where(customer: @customer)
rentals = rentals.map do |rental|
{
title: rental.movie.title,
customer_id: rental.customer_id,
checkout_date: rental.checkout_date,
due_date: rental.due_date,
returned: rental.returned
}
end
render status: :ok, json: rentals
end

def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date], returned: false)
Expand Down Expand Up @@ -32,18 +51,20 @@ def check_in
def overdue
rentals = Rental.overdue.map do |rental|
{
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
}
end
render status: :ok, json: rentals
end

private


private
# TODO: make error payloads arrays
def require_movie
@movie = Movie.find_by title: params[:title]
Expand Down
6 changes: 4 additions & 2 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Movie < ApplicationRecord
attr_accessor :external_id

has_many :rentals
has_many :rentals, dependent: :destroy
has_many :customers, through: :rentals

def available_inventory
Expand All @@ -12,8 +12,10 @@ def image_url
orig_value = read_attribute :image_url
if !orig_value
MovieWrapper::DEFAULT_IMG_URL
else
elsif external_id
MovieWrapper.construct_image_url(orig_value)
else
orig_value
end
end
end
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
resources :customers, only: [:index]

resources :movies, only: [:index, :show], param: :title
post "/movies", to: "movies#create", as: "new_movie"
delete "/movies/:id", to:"movies#destroy", as: "delete_movie"

post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out"
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

get "/rentals", to: "rentals#index", as: "rentals"
get "/rentals/by-customer", to: "rentals#customer_rental", as: "rentals_by_customer"

end
9 changes: 6 additions & 3 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class MovieWrapper
BASE_URL = "https://api.themoviedb.org/3/"
KEY = ENV["MOVIEDB_KEY"]

# this is a comment
BASE_IMG_URL = "https://image.tmdb.org/t/p/"
DEFAULT_IMG_SIZE = "w185"
DEFAULT_IMG_URL = "http://lorempixel.com/185/278/"

def self.search(query)
puts KEY
url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query
# puts url
response = HTTParty.get(url)
Expand All @@ -27,8 +28,10 @@ def self.construct_movie(api_result)
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),
external_id: api_result["id"])
image_url: api_result["poster_path"],
#image_url: api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil,
external_id: api_result["id"],
inventory: 1)
end

def self.construct_image_url(img_name)
Expand Down
44 changes: 44 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,50 @@
require 'test_helper'

class MoviesControllerTest < ActionDispatch::IntegrationTest
describe "create" do
it "creates a new movie" do
post new_movie_path params: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 }
assert_response :success
Movie.last.title.must_equal movies(:one).title
end

it "returns the id of the movie if successful" do
post new_movie_path(movies(:one))
body = JSON.parse(response.body)
body.must_be_kind_of Hash
body["id"].must_equal Movie.last.id
end

# checks that if the movie already exisit in our library, then it return error message
end

describe "destroy" do
it "deletes a movie" do
Movie.destroy_all
post new_movie_path params: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 }
movie_id = Movie.first.id
delete delete_movie_path(movie_id)
assert_response :success
body = JSON.parse(response.body)
body.must_be_kind_of Hash
body["id"].must_equal movie_id
Movie.count.must_equal 0
end

it "Doens't delete movie if the id passed is invalid" do
Movie.destroy_all
post new_movie_path params: { title: "WowMovie!", overview: "MyText", release_date: "2017-01-11", inventory: 4 }
delete delete_movie_path(23424556)
assert_response :not_found
Movie.count.must_equal 1
body = JSON.parse(response.body)
body.must_be_kind_of Hash
body["errors"].must_equal "This Movie does not exist in the database"
end
end



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