From 951971342ced7d2ea2e1ec91ce3063203f86df2d Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Mon, 22 Jun 2020 11:27:27 -0700 Subject: [PATCH 01/10] Added .env to gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 4a494a75..3b91322a 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +.env \ No newline at end of file From 2495feb0d9b40fa0500e437379549556bc7a0773 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Mon, 22 Jun 2020 11:29:20 -0700 Subject: [PATCH 02/10] Rails setup: ran first migrations already included --- db/schema.rb | 69 +++++++++++++++++++++++++++------------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index ffb28f7e..c97444b1 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 - 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 4bade8e0d1991f8b7b644c7c63d70b6605bb51a8 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Mon, 22 Jun 2020 16:47:31 -0700 Subject: [PATCH 03/10] Changed default port to 4000 to run rails server --- config/puma.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/puma.rb b/config/puma.rb index c7f311f8..f3398437 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -9,7 +9,7 @@ # Specifies the `port` that Puma will listen on to receive requests, default is 3000. # -port ENV.fetch("PORT") { 3000 } +port ENV.fetch("PORT") { 4000 } # Specifies the `environment` that Puma will run in. # From 1e9db0acffd989f0105afbbcc434aa0bab315999 Mon Sep 17 00:00:00 2001 From: Catherina Date: Tue, 23 Jun 2020 17:45:14 -0700 Subject: [PATCH 04/10] Add create route and create action --- app/controllers/movies_controller.rb | 30 +++++++++++++++++++++++++--- config/routes.rb | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..f6083fad 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -16,9 +16,33 @@ def show status: :ok, json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + methods: [:available_inventory], + ), + ) + end + + def create + 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 json: movie.as_json(only: [:id]), status: :created + return + else + + end + + # render( + # status: :ok, + # json: { + # message: "All good", + # }, + # ) end private diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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" From 0abd0b12f2fa031dac1a635d445c4376d16453e9 Mon Sep 17 00:00:00 2001 From: Catherina Date: Wed, 24 Jun 2020 11:40:52 -0700 Subject: [PATCH 05/10] Add validation for movie external_id uniqueness --- app/models/movie.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/movie.rb b/app/models/movie.rb index fda94941..6517a74d 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 92ec77374acf743c645c0628b6410a69ec4d0d22 Mon Sep 17 00:00:00 2001 From: Catherina Date: Wed, 24 Jun 2020 15:37:52 -0700 Subject: [PATCH 06/10] Add json to be rendered as bad request if there is already a movie in the library --- 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 f6083fad..6c2d6436 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -34,7 +34,7 @@ def create render json: movie.as_json(only: [:id]), status: :created return else - + render status: :bad_request, json: { errors: movie.errors.messages } end # render( From 077c1d1e57f820416017f0f5b71f2bdc7b4f9117 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Wed, 24 Jun 2020 16:28:11 -0700 Subject: [PATCH 07/10] Added tests for movies#create, all but edge case complete. --- app/controllers/movies_controller.rb | 7 ---- test/controllers/movies_controller_test.rb | 42 ++++++++++++++++++++++ test/fixtures/movies.yml | 2 ++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6c2d6436..b805677c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -36,13 +36,6 @@ def create else render status: :bad_request, json: { errors: movie.errors.messages } end - - # render( - # status: :ok, - # json: { - # message: "All good", - # }, - # ) end private diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 9172cf6e..0aeb1dec 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'pry' class MoviesControllerTest < ActionDispatch::IntegrationTest describe "index" do @@ -75,4 +76,45 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest end end + + describe "create" do + it "Returns a JSON object and 201 created status for new movies from external db with valid params" do + new_movie_params = { + title: "Our test movie", + overview: "Awesome times ahead", + release_date: Date.today, + external_id: 38583, + image_url: "test url string for url" + } + + expect { post movies_url, params: new_movie_params }.must_differ "Movie.count", 1 + must_respond_with :created + expect(response.header['Content-Type']).must_include 'json' + + # Attempt to parse + data = JSON.parse response.body + data.must_be_kind_of Hash + end + + + it "Returns a JSON object with errors and 400 bad request status if movie already exists" do + duplicate_movie_params = { + title: "WowMovie!", + overview: "MyText", + release_date: "2017-01-11", + external_id: 16373 + } + + expect { post movies_url, params: duplicate_movie_params }.wont_change "Movie.count" + must_respond_with :bad_request + expect(response.header['Content-Type']).must_include 'json' + # puts "HERE IS ERRORS:::: #{response.body["errors"]}" + # must include external_id + # expect(response.body).must_include + + # Attempt to parse + # data = JSON.parse response.body + # data.must_be_kind_of Hash + end + end end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index caf6e68b..77320b9c 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -5,9 +5,11 @@ one: overview: MyText release_date: 2017-01-11 inventory: 4 + external_id: 16373 two: title: MuchFilm overview: MyText release_date: 2017-01-11 inventory: 7 + external_id: 21111 From 862bd5d5c8d833bbfc96c6d1e2f3275ee10b45ce Mon Sep 17 00:00:00 2001 From: Catherina Date: Wed, 24 Jun 2020 20:41:34 -0700 Subject: [PATCH 08/10] Update checkout logic in rentals controller --- app/controllers/movies_controller.rb | 3 ++- app/controllers/rentals_controller.rb | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index b805677c..a080cb86 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -27,7 +27,8 @@ def create overview: params[:overview], release_date: params[:release_date], image_url: params[:image_url], - external_id: params[:external_id] + external_id: params[:external_id], + inventory: 3 ) if movie.save diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..bcc0caac 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -6,9 +6,14 @@ class RentalsController < ApplicationController def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) + if @movie.available_inventory < 1 + render status: :bad_request, json: { errors: rental.errors.messages } + return + end + if rental.save render status: :ok, json: {} - else + else render status: :bad_request, json: { errors: rental.errors.messages } end end From a9cf6556eae161f3f718796a9ccac2ecbf4e7cf5 Mon Sep 17 00:00:00 2001 From: RebeccaRoach Date: Thu, 25 Jun 2020 14:08:10 -0700 Subject: [PATCH 09/10] Sorted movies index action to display alphabetically in the library. --- 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 a080cb86..462c1da5 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -5,7 +5,7 @@ def index if params[:query] data = MovieWrapper.search(params[:query]) else - data = Movie.all + data = Movie.all.order('title ASC') end render status: :ok, json: data From 776288c179dbc1aeb26bfb5fa89df9ca7c891299 Mon Sep 17 00:00:00 2001 From: Catherina Date: Thu, 25 Jun 2020 14:32:22 -0700 Subject: [PATCH 10/10] Add more assertions for external_id of the newly created movie --- test/controllers/movies_controller_test.rb | 9 ++------- test/controllers/rentals_controller_test.rb | 5 ----- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 0aeb1dec..5489216e 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -108,13 +108,8 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest expect { post movies_url, params: duplicate_movie_params }.wont_change "Movie.count" must_respond_with :bad_request expect(response.header['Content-Type']).must_include 'json' - # puts "HERE IS ERRORS:::: #{response.body["errors"]}" - # must include external_id - # expect(response.body).must_include - - # Attempt to parse - # data = JSON.parse response.body - # data.must_be_kind_of Hash + expect(response.body).must_include "external_id" + expect(response.body).must_include "has already been taken" end end end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 831b5230..379a33d3 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -115,11 +115,6 @@ class RentalsControllerTest < ActionDispatch::IntegrationTest expect(rental.movie_id).must_equal movie.id expect(rental.due_date).must_equal Date.today + 5 expect(rental.returned).must_equal true - - - - - end it "requires a valid movie title" do