From c7186bc2998e8983e0ca9be97a1f25b5b1863b11 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 11:04:27 -0700 Subject: [PATCH 01/12] initialized create movie action --- app/controllers/movies_controller.rb | 8 ++++++++ config/routes.rb | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..ac700fa3 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,6 +11,14 @@ def index render status: :ok, json: data end + # TODO: create a new movie instance in our rental library + def create + # step 1. to find the specific movie, we need to use the external_id from the params that we get from the React Movies.js + # https://developers.themoviedb.org/3/movies/get-movie-details + + # setp 2. + end + def show render( status: :ok, diff --git a/config/routes.rb b/config/routes.rb index f4c99688..797fb769 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,8 +2,10 @@ # 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 + + # TODO: new to add the create action in the movie controller + # so that user can add a movie from the search results to the rental library + 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" From 047f25d92b4df84ca826bd61663fd0ea4f1c6ad6 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 13:35:03 -0700 Subject: [PATCH 02/12] created hompages controller --- app/assets/stylesheets/homepages.css | 4 ++++ app/controllers/homepages_controller.rb | 4 ++++ app/helpers/homepages_helper.rb | 2 ++ test/controllers/homepages_controller_test.rb | 7 +++++++ 4 files changed, 17 insertions(+) create mode 100644 app/assets/stylesheets/homepages.css create mode 100644 app/controllers/homepages_controller.rb create mode 100644 app/helpers/homepages_helper.rb create mode 100644 test/controllers/homepages_controller_test.rb diff --git a/app/assets/stylesheets/homepages.css b/app/assets/stylesheets/homepages.css new file mode 100644 index 00000000..afad32db --- /dev/null +++ b/app/assets/stylesheets/homepages.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/homepages_controller.rb b/app/controllers/homepages_controller.rb new file mode 100644 index 00000000..f200c8ac --- /dev/null +++ b/app/controllers/homepages_controller.rb @@ -0,0 +1,4 @@ +class HomepagesController < ApplicationController + def index + end +end diff --git a/app/helpers/homepages_helper.rb b/app/helpers/homepages_helper.rb new file mode 100644 index 00000000..4bd8098f --- /dev/null +++ b/app/helpers/homepages_helper.rb @@ -0,0 +1,2 @@ +module HomepagesHelper +end diff --git a/test/controllers/homepages_controller_test.rb b/test/controllers/homepages_controller_test.rb new file mode 100644 index 00000000..d73f39b9 --- /dev/null +++ b/test/controllers/homepages_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class HomepagesControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end From b00617a3801c28c200e64e6e291350d9e0444540 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 13:35:45 -0700 Subject: [PATCH 03/12] added actions to the Movies controller --- app/controllers/movies_controller.rb | 46 +++++++++++++++++++++++----- config/routes.rb | 9 ++++-- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ac700fa3..2fa267b3 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,21 +2,51 @@ class MoviesController < ApplicationController before_action :require_movie, only: [:show] def index - if params[:query] - data = MovieWrapper.search(params[:query]) + data = Movie.all + render status: :ok, json: data + end + + def search_input + end + + def search + @movie = Movie.find_by(title: params[:title]) + if !@movie + @data = MovieWrapper.search(params[:title]) + else - data = Movie.all + render( + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) end - - render status: :ok, json: data end # TODO: create a new movie instance in our rental library def create - # step 1. to find the specific movie, we need to use the external_id from the params that we get from the React Movies.js - # https://developers.themoviedb.org/3/movies/get-movie-details + @movie = Movie.new( + title: params["title"], + overview: params["overview"], + release_date: params["release_date"], + image_url: self.image_url(params["poster_path"]), + external_id: params["id"], + inventory: 10 + ) - # setp 2. + if @movie.save + render( + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) + else + render status: :bad_request, json: { errors: { title: ["Can't successfully create the movie with title #{params["title"]}"] } } + end end def show diff --git a/config/routes.rb b/config/routes.rb index 797fb769..3f8c5332 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,19 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + root 'homepages#index' resources :customers, only: [:index] # TODO: new to add the create action in the movie controller # so that user can add a movie from the search results to the rental library - resources :movies, only: [:index, :show, :create], param: :title - + resources :movies, only: [:show, :create], param: :title + get "/library", to: "movies#index", as: "library" + get "/input", to: "movies#search_input", as: "search_input" + get "/search", to: "movies#search", as: "search" 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 From 820feae2a1c61ac9670dec63d70f5187ccdcec42 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 15:05:36 -0700 Subject: [PATCH 04/12] removed hompages controller --- app/assets/stylesheets/homepages.css | 4 ---- app/controllers/homepages_controller.rb | 4 ---- app/helpers/homepages_helper.rb | 2 -- test/controllers/homepages_controller_test.rb | 7 ------- 4 files changed, 17 deletions(-) delete mode 100644 app/assets/stylesheets/homepages.css delete mode 100644 app/controllers/homepages_controller.rb delete mode 100644 app/helpers/homepages_helper.rb delete mode 100644 test/controllers/homepages_controller_test.rb diff --git a/app/assets/stylesheets/homepages.css b/app/assets/stylesheets/homepages.css deleted file mode 100644 index afad32db..00000000 --- a/app/assets/stylesheets/homepages.css +++ /dev/null @@ -1,4 +0,0 @@ -/* - Place all the styles related to the matching controller here. - They will automatically be included in application.css. -*/ diff --git a/app/controllers/homepages_controller.rb b/app/controllers/homepages_controller.rb deleted file mode 100644 index f200c8ac..00000000 --- a/app/controllers/homepages_controller.rb +++ /dev/null @@ -1,4 +0,0 @@ -class HomepagesController < ApplicationController - def index - end -end diff --git a/app/helpers/homepages_helper.rb b/app/helpers/homepages_helper.rb deleted file mode 100644 index 4bd8098f..00000000 --- a/app/helpers/homepages_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module HomepagesHelper -end diff --git a/test/controllers/homepages_controller_test.rb b/test/controllers/homepages_controller_test.rb deleted file mode 100644 index d73f39b9..00000000 --- a/test/controllers/homepages_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'test_helper' - -class HomepagesControllerTest < ActionDispatch::IntegrationTest - # test "the truth" do - # assert true - # end -end From 9189f7672ca6cfdeabdf698e2f603363d48d97c5 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 15:09:56 -0700 Subject: [PATCH 05/12] added create action in Movies Controller --- config/routes.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 3f8c5332..39908a82 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,19 +1,14 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - root 'homepages#index' resources :customers, only: [:index] - - # TODO: new to add the create action in the movie controller - # so that user can add a movie from the search results to the rental library - resources :movies, only: [:show, :create], param: :title - get "/library", to: "movies#index", as: "library" - get "/input", to: "movies#search_input", as: "search_input" - get "/search", to: "movies#search", as: "search" + + resources :movies, only: [:index,:create, :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" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" - + root 'movies#index' -end +end \ No newline at end of file From 852e6e349471635b83b9d8a54c61e51f6e944189 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 15:10:18 -0700 Subject: [PATCH 06/12] rollbacked to original --- app/controllers/movies_controller.rb | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 2fa267b3..890dae8e 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,27 +2,13 @@ class MoviesController < ApplicationController before_action :require_movie, only: [:show] def index - data = Movie.all - render status: :ok, json: data - end - - def search_input - end - - def search - @movie = Movie.find_by(title: params[:title]) - if !@movie - @data = MovieWrapper.search(params[:title]) - + if params[:query] + data = MovieWrapper.search(params[:query]) else - render( - status: :ok, - json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + data = Movie.all end + + render status: :ok, json: data end # TODO: create a new movie instance in our rental library From 6b63dcf752ff49373804cff7ff9fb29476c96c41 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Tue, 23 Jun 2020 15:10:44 -0700 Subject: [PATCH 07/12] assigned due date to the Rental check_out --- app/controllers/rentals_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..734bda9c 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,7 +4,7 @@ class RentalsController < ApplicationController # TODO: make sure that wave 2 works all the way def check_out - rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) + rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7) if rental.save render status: :ok, json: {} From 6bc0e59fb8dd814406e277a4d885287597dd8866 Mon Sep 17 00:00:00 2001 From: Yieni Date: Wed, 24 Jun 2020 13:26:07 -0700 Subject: [PATCH 08/12] add image url to json --- app/controllers/movies_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 890dae8e..e112c485 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,7 +17,7 @@ def create title: params["title"], overview: params["overview"], release_date: params["release_date"], - image_url: self.image_url(params["poster_path"]), + image_url: params["image_url"], external_id: params["id"], inventory: 10 ) @@ -39,7 +39,7 @@ def show render( status: :ok, json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], + only: [:title, :overview, :release_date, :inventory, :image_url], methods: [:available_inventory] ) ) From a603e08123266394ebfcdccd81661f7ab9940430 Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 25 Jun 2020 00:20:12 -0700 Subject: [PATCH 09/12] changed movie create method to not create a movie if it already exists in the database. --- Gemfile | 2 +- Gemfile.lock | 2 +- app/controllers/movies_controller.rb | 27 +++++++----- db/schema.rb | 63 +++++++++++++++------------- 4 files changed, 51 insertions(+), 43 deletions(-) diff --git a/Gemfile b/Gemfile index 25fddaae..3a2c5fdc 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.6.5' +ruby '2.6.4' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.3', '>= 6.0.3.1' diff --git a/Gemfile.lock b/Gemfile.lock index a1969bba..a46a2cbd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -238,7 +238,7 @@ DEPENDENCIES will_paginate RUBY VERSION - ruby 2.6.5p114 + ruby 2.6.4p104 BUNDLED WITH 2.1.4 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index e112c485..c9d6a881 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -13,7 +13,7 @@ def index # TODO: create a new movie instance in our rental library def create - @movie = Movie.new( + movie = Movie.new( title: params["title"], overview: params["overview"], release_date: params["release_date"], @@ -21,17 +21,22 @@ def create external_id: params["id"], inventory: 10 ) - - if @movie.save - render( - status: :ok, - json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + @movie = Movie.find_by(external_id: movie.external_id) + if @movie + render status: :bad_request, json: { errors: { title: ["#{params["title"]} already exisits in our library"] } } else - render status: :bad_request, json: { errors: { title: ["Can't successfully create the movie with title #{params["title"]}"] } } + if @movie.save + render( + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) + else + render status: :bad_request, json: { errors: { title: ["#{params["title"]} already exisits in our library"] } } + end + end end diff --git a/db/schema.rb b/db/schema.rb index ffb28f7e..8c8b42d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,48 +2,51 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # 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 + + # 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 "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" + 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 b897db7ac02e20e153095a4fe1ed47f636e1f0b8 Mon Sep 17 00:00:00 2001 From: Yieni Date: Thu, 25 Jun 2020 00:36:17 -0700 Subject: [PATCH 10/12] refactor previous code for stopping exisiting movie creation by adding validations to stop exisiting movie from be created --- app/controllers/movies_controller.rb | 31 +++++++++++++--------------- app/models/movie.rb | 2 ++ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c9d6a881..44c2ec9b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -13,31 +13,28 @@ def index # TODO: create a new movie instance in our rental library def create - movie = Movie.new( + @movie = Movie.new( title: params["title"], overview: params["overview"], release_date: params["release_date"], image_url: params["image_url"], - external_id: params["id"], + external_id: params["external_id"], inventory: 10 ) - @movie = Movie.find_by(external_id: movie.external_id) - if @movie - render status: :bad_request, json: { errors: { title: ["#{params["title"]} already exisits in our library"] } } + + if @movie.save! + render( + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) else - if @movie.save - render( - status: :ok, - json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) - else - render status: :bad_request, json: { errors: { title: ["#{params["title"]} already exisits in our library"] } } - end - + render status: :bad_request, json: { errors: { title: ["#{params["title"]} already exisits in our library"] } } end + + end def show diff --git a/app/models/movie.rb b/app/models/movie.rb index fda94941..62af281a 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,6 +1,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :title, :overview, :release_date, :inventory, :image_url, :external_id, presence: true + validates :external_id, uniqueness: true def available_inventory self.inventory - Rental.where(movie: self, returned: false).length From 3a03b89e6fd4b79338855c91d8eeac461dfffcdf Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Thu, 25 Jun 2020 14:18:49 -0700 Subject: [PATCH 11/12] changed ruby version --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 3a2c5fdc..25fddaae 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.6.4' +ruby '2.6.5' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 6.0.3', '>= 6.0.3.1' From ca06d694f677e407f0d2104962a180c529ee63b8 Mon Sep 17 00:00:00 2001 From: Rosalyn Date: Thu, 25 Jun 2020 14:19:30 -0700 Subject: [PATCH 12/12] added return json data for a post rental request --- app/controllers/rentals_controller.rb | 7 ++++++- app/models/rental.rb | 2 +- config/routes.rb | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 734bda9c..2426dce8 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,7 +7,12 @@ def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7) if rental.save - render status: :ok, json: {} + render( + status: :ok, + json: rental.as_json( + only: [:customer_id, :move_id, :checkout_date, :due_date, :returned] + ) + ) else render status: :bad_request, json: { errors: rental.errors.messages } end diff --git a/app/models/rental.rb b/app/models/rental.rb index 18654f04..f0539e94 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -2,7 +2,7 @@ class Rental < ApplicationRecord belongs_to :movie belongs_to :customer - # validates :movie, uniqueness: { scope: :customer } + validates :movie, uniqueness: { scope: :customer } validates :due_date, presence: true validate :due_date_in_future, on: :create diff --git a/config/routes.rb b/config/routes.rb index 39908a82..12bdc0e4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,4 +11,4 @@ root 'movies#index' -end \ No newline at end of file +end \ No newline at end of file