From 7eb941b9a9a827b74332f6dc7e3f021c6e057749 Mon Sep 17 00:00:00 2001 From: Olga Patrakova Date: Tue, 23 Jun 2020 10:10:43 -0700 Subject: [PATCH 1/5] Migrated DB --- db/schema.rb | 63 +++++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) 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 5a9c2c4c61c81c2b3f41462d50464bb87ca7b775 Mon Sep 17 00:00:00 2001 From: Olga Patrakova Date: Tue, 23 Jun 2020 22:32:36 -0700 Subject: [PATCH 2/5] Added routes and create functionality --- app/controllers/movies_controller.rb | 23 ++++++++++++++++++++++- config/environments/development.rb | 4 ++-- config/routes.rb | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..fbfb7952 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,7 +3,12 @@ class MoviesController < ApplicationController def index if params[:query] - data = MovieWrapper.search(params[:query]) + @movies = Movie.where("title ilike ?", "%#{params[:query]}%").all + if !@movies.empty? + data = @movies + else + data = MovieWrapper.search(params[:query]) + end else data = Movie.all end @@ -21,6 +26,22 @@ def show ) end + def create + raise + movie = Movie.new( + title: params[:title], + overview: params[:overview], + release_date: params[:release_date], + image_url: params[:image_url], + inventory: 1 + ) + if movie.save + render status: :ok, json: { success: "success" } + else + render status: :bad_request, json: { errors: rental.errors.messages } + end + end + private def require_movie diff --git a/config/environments/development.rb b/config/environments/development.rb index 6f719704..a230e5de 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -21,7 +21,7 @@ 'Cache-Control' => 'public, max-age=172800' } else - config.action_controller.perform_caching = false + config.action_controller.perform_caching = true config.cache_store = :null_store end @@ -29,7 +29,7 @@ # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false - config.action_mailer.perform_caching = false + config.action_mailer.perform_caching = true # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log 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 777c1324e35c60dbea4a63afeacb8dd8096273af Mon Sep 17 00:00:00 2001 From: Olga Patrakova Date: Wed, 24 Jun 2020 10:09:27 -0700 Subject: [PATCH 3/5] Fixed create function in movies controller --- app/controllers/movies_controller.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index fbfb7952..b8dd2b9a 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -27,7 +27,6 @@ def show end def create - raise movie = Movie.new( title: params[:title], overview: params[:overview], @@ -36,7 +35,7 @@ def create inventory: 1 ) if movie.save - render status: :ok, json: { success: "success" } + render status: :ok, json: movie else render status: :bad_request, json: { errors: rental.errors.messages } end From 27dcd397e48c12db06f66c83d3bd3feeb75308e5 Mon Sep 17 00:00:00 2001 From: Olga Patrakova Date: Wed, 24 Jun 2020 12:38:15 -0700 Subject: [PATCH 4/5] Added functionality of filtering movies based on ids for getting a button in react --- app/controllers/movies_controller.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index b8dd2b9a..b75c8d01 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,11 +3,11 @@ class MoviesController < ApplicationController def index if params[:query] - @movies = Movie.where("title ilike ?", "%#{params[:query]}%").all - if !@movies.empty? - data = @movies - else - data = MovieWrapper.search(params[:query]) + data = MovieWrapper.search(params[:query]) # all ids are null, external_id is not null + hash = {} # {external_id => id} + Movie.all.select(:id, :external_id).each {|movie| hash[movie.external_id] = movie.id } + data.each_with_index do |item,i| + data[i][:id] = hash[item[:external_id]] end else data = Movie.all @@ -32,7 +32,8 @@ def create overview: params[:overview], release_date: params[:release_date], image_url: params[:image_url], - inventory: 1 + inventory: 1, + external_id: params[:external_id] ) if movie.save render status: :ok, json: movie From e91f775799201197d433271393e88d3809be8e78 Mon Sep 17 00:00:00 2001 From: Olga Patrakova Date: Thu, 25 Jun 2020 14:43:51 -0700 Subject: [PATCH 5/5] Added test for movie create action --- test/controllers/movies_controller_test.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 9172cf6e..02505900 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -75,4 +75,27 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest end end + + describe "create" do + it "can create a movie with valid info" do + movie_hash = + { + title: "Test", + overview: "Test overview", + inventory: 1, + external_id: 12345 + } + + expect { + post movies_path, params: movie_hash + }.must_differ "Movie.count", 1 + + new_movie = Movie.last + expect(new_movie.title).must_equal movie_hash[:title] + expect(new_movie.overview).must_equal movie_hash[:overview] + expect(new_movie.inventory).must_equal movie_hash[:inventory] + expect(new_movie.external_id).must_equal movie_hash[:external_id] + must_respond_with :success + end + end end