From 3cfe45758d5fc266f4fa953490a0e68c1a10c3c0 Mon Sep 17 00:00:00 2001 From: jackie Date: Tue, 18 Dec 2018 14:41:06 -0800 Subject: [PATCH 1/5] Added movies#create and related route --- Gemfile | 1 + Gemfile.lock | 5 +++ app/controllers/movies_controller.rb | 19 +++++++++ config/routes.rb | 3 +- db/schema.rb | 59 +++++++++++++++------------- 5 files changed, 58 insertions(+), 29 deletions(-) diff --git a/Gemfile b/Gemfile index 2d53ba9c..3290f667 100644 --- a/Gemfile +++ b/Gemfile @@ -54,6 +54,7 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] group :development, :test do gem 'pry-rails' gem 'dotenv-rails' + gem 'better_errors' end group :test do diff --git a/Gemfile.lock b/Gemfile.lock index a0a07df1..65b2908b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -50,6 +50,10 @@ GEM ansi (1.5.0) arel (9.0.0) awesome_print (1.8.0) + better_errors (2.5.0) + coderay (>= 1.0.0) + erubi (>= 1.0.0) + rack (>= 0.9.0) bootsnap (1.3.2) msgpack (~> 1.0) builder (3.2.3) @@ -177,6 +181,7 @@ PLATFORMS DEPENDENCIES active_model_serializers awesome_print + better_errors bootsnap (>= 1.1.0) byebug dotenv-rails diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..d284cf24 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,3 +1,5 @@ +require 'pry' + class MoviesController < ApplicationController before_action :require_movie, only: [:show] @@ -21,6 +23,23 @@ def show ) end + def create + params["image_url"].slice! "https://image.tmdb.org/t/p/w185" + + movie = Movie.new( + title: params["title"], + overview: params["overview"], + release_date: params["release_date"], + image_url: params["image_url"], + external_id: params["external_id"]) + + if movie.save + render status: :ok, json: { movie: movie } + else + render status: :bad_request, json: { errors: movie.errors.messages } + end + end + private def require_movie diff --git a/config/routes.rb b/config/routes.rb index f4c99688..e8d60aa1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,8 @@ resources :customers, only: [:index] - resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:index, :show,], param: :title + post "/movies/", to: "movies#create", as: "add_to_library" post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" diff --git a/db/schema.rb b/db/schema.rb index ffb28f7e..d4b52b3c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 From c3b799f9f64c7c1c62088aeadc4b47c24a3afe85 Mon Sep 17 00:00:00 2001 From: jackie Date: Tue, 18 Dec 2018 18:49:31 -0800 Subject: [PATCH 2/5] Added customer model methods --- app/controllers/customers_controller.rb | 2 +- app/models/customer.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..f75b0e6f 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -14,7 +14,7 @@ def index render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit], - methods: [:movies_checked_out_count] + methods: [:movies_checked_out_count, :movies_checked_out] ) end diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..e2fdca17 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -5,4 +5,12 @@ class Customer < ApplicationRecord def movies_checked_out_count self.rentals.where(returned: false).length end + + def movies_checked_out + self.movies + end + + # def overdue_movies_checked_out + # self.rentals.where(returned: false) + # end end From 7256e8be68220dc75cf35029fc3fe072c0cf31f5 Mon Sep 17 00:00:00 2001 From: jackie Date: Tue, 18 Dec 2018 22:10:28 -0800 Subject: [PATCH 3/5] Modified Customer#movies_checked_out to include information necessary for display and check-in --- app/models/customer.rb | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index e2fdca17..f431f2c6 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -7,10 +7,17 @@ def movies_checked_out_count end def movies_checked_out - self.movies - end + outstanding_rentals = self.rentals.where(returned: false) + + checked_out = outstanding_rentals.map do |rental| + { + title: rental.movie.title, + checkout_date: rental.checkout_date, + due_date: rental.due_date, + overdue: (rental.due_date < Date.today ? true : false) + } + end - # def overdue_movies_checked_out - # self.rentals.where(returned: false) - # end + return checked_out + end end From 19039794f835a670fce16389531d378394ac3695 Mon Sep 17 00:00:00 2001 From: Danielle Metzner Date: Wed, 19 Dec 2018 10:10:28 -0800 Subject: [PATCH 4/5] Added validation so there are no repeat titles --- app/models/movie.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..f530c5cf 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -2,6 +2,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :external_id, uniqueness: true + def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end From d149bf54eb165499e3faa9a80b31b4b52dab16df Mon Sep 17 00:00:00 2001 From: Danielle Metzner Date: Fri, 21 Dec 2018 11:23:36 -0800 Subject: [PATCH 5/5] Removed Pry --- app/controllers/movies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index d284cf24..3be0c6ff 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,4 +1,4 @@ -require 'pry' + class MoviesController < ApplicationController before_action :require_movie, only: [:show]