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 - Dianna and Tiffany #5

Open
wants to merge 5 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
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ RUBY VERSION
ruby 2.5.5p157

BUNDLED WITH
1.17.3
2.0.2
53 changes: 38 additions & 15 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
class CustomersController < ApplicationController
SORT_FIELDS = %w(name registered_at postal_code)

before_action :parse_query_args

def index
if @sort
data = Customer.all.order(@sort)
else
data = Customer.all
end

data = data.paginate(page: params[:p], per_page: params[:n])

render json: data.as_json(
only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],
methods: [:movies_checked_out_count]
)
end

private
def parse_query_args
errors = {}
@sort = params[:sort]
if @sort and not SORT_FIELDS.include? @sort
errors[:sort] = ["Invalid sort field '#{@sort}'"]

def show
@customer = Customer.find_by(id: params[:id])

if @customer
@rental_list = @customer.rentals.map{ |rental| {title: rental.movie.title,
checkout_date: rental.checkout_date,
due_date: rental.due_date,
returned: rental.returned } }


render json: @rental_list.as_json(), status: :ok
return
else
render json: {
ok: false,
errors: "Customer Not Found"
},
status: :bad_request
return
end
end

unless errors.empty?
render status: :bad_request, json: { errors: errors }

private
def parse_query_args
errors = {}
@sort = params[:sort]
if @sort and not SORT_FIELDS.include? @sort
errors[:sort] = ["Invalid sort field '#{@sort}'"]
end

unless errors.empty?
render status: :bad_request, json: { errors: errors }
end
end
end
end
21 changes: 21 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

def create
movie = Movie.new(movie_params)

if movie.save
render json: movie.as_json(only: [:id]), status: :ok
return
else
render json: {
ok: false,
errors: movie.errors.messages
},
status: :bad_request
return
end
end

def index
if params[:query]
data = MovieWrapper.search(params[:query])
Expand Down Expand Up @@ -29,4 +45,9 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

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

end
6 changes: 4 additions & 2 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals


validates :external_id, presence: true, uniqueness: true

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

def image_url
raw_value = read_attribute :image_url
if !raw_value
Expand Down
14 changes: 7 additions & 7 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :customers, only: [:index]

resources :movies, only: [:index, :show], param: :title

resources :customers, only: [:index, :show]
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"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

root 'movies#index'

end
59 changes: 31 additions & 28 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_06_18_042754) do

create_table "customers", force: :cascade do |t|
t.string "name"
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "customers", id: :serial, force: :cascade do |t|
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"
create_table "movies", id: :serial, 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"
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"
create_table "rentals", id: :serial, 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.index ["customer_id"], name: "index_rentals_on_customer_id"
t.index ["movie_id"], name: "index_rentals_on_movie_id"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MovieWrapper

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

def self.search(query, retries_left=3)
raise ArgumentError.new("Can't search without a MOVIEDB_KEY. Please check your .env file!") unless KEY
Expand Down
63 changes: 47 additions & 16 deletions test/models/movie_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,59 @@ class MovieTest < ActiveSupport::TestCase
"title": "Hidden Figures",
"overview": "Some text",
"release_date": "1960-06-16",
"inventory": 8
"inventory": 8,
"external_id": 100
}
}

before do
@movie = Movie.new(movie_data)
end

describe "Constructor" do
it "Can be created" do
Movie.create!(movie_data)
end

it "Has rentals" do
@movie.must_respond_to :rentals
end

it "Has customers" do
@movie.must_respond_to :customers
end
end


describe "Validation" do
it "cannot create a movie that has the same external_id as another movie" do
Movie.create!(movie_data)

duplicate_movie = Movie.new(
title: "Hello World",
overview: "test test test",
release_date: "2019-12-12",
inventory: 8,
external_id: 100
)

expect(duplicate_movie.valid?).must_equal false
expect(duplicate_movie.errors.messages).must_include :external_id
expect(duplicate_movie.errors.messages[:external_id]).must_include "has already been taken"
end

it "cannot create a movie with no external_id" do
missing_movie = Movie.new(
title: "Hello World",
overview: "test test test",
release_date: "2019-12-12",
inventory: 8,
)

expect(missing_movie.valid?).must_equal false
expect(missing_movie.errors.messages).must_include :external_id
end
end

describe "available_inventory" do
it "Matches inventory if the movie isn't checked out" do
# Make sure no movies are checked out
Expand All @@ -36,43 +67,43 @@ class MovieTest < ActiveSupport::TestCase
movie.available_inventory().must_equal movie.inventory
end
end

it "Decreases when a movie is checked out" do
Rental.destroy_all

movie = movies(:one)
before_ai = movie.available_inventory

Rental.create!(
customer: customers(:one),
movie: movie,
due_date: Date.today + 7,
returned: false
)

movie.reload
after_ai = movie.available_inventory
after_ai.must_equal before_ai - 1
end

it "Increases when a movie is checked in" do
Rental.destroy_all

movie = movies(:one)

rental =Rental.create!(
customer: customers(:one),
movie: movie,
due_date: Date.today + 7,
returned: false
)

movie.reload
before_ai = movie.available_inventory

rental.returned = true
rental.save!

movie.reload
after_ai = movie.available_inventory
after_ai.must_equal before_ai + 1
Expand Down