diff --git a/Gemfile.lock b/Gemfile.lock index 7d037cf4..525918af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,7 +24,7 @@ GEM erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - active_model_serializers (0.10.8) + active_model_serializers (0.10.9) actionpack (>= 4.1, < 6) activemodel (>= 4.1, < 6) case_transform (>= 0.2) @@ -59,22 +59,22 @@ GEM coderay (1.1.2) concurrent-ruby (1.1.5) crass (1.0.4) - dotenv (2.7.2) - dotenv-rails (2.7.2) - dotenv (= 2.7.2) + dotenv (2.7.4) + dotenv-rails (2.7.4) + dotenv (= 2.7.4) railties (>= 3.2, < 6.1) erubi (1.8.0) ffi (1.11.1) globalid (0.4.2) activesupport (>= 4.2.0) - httparty (0.16.3) + httparty (0.17.0) mime-types (~> 3.0) multi_xml (>= 0.5.2) i18n (1.6.0) concurrent-ruby (~> 1.0) jbuilder (2.9.1) activesupport (>= 4.2.0) - jsonapi-renderer (0.2.0) + jsonapi-renderer (0.2.2) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -89,20 +89,20 @@ GEM method_source (0.9.2) mime-types (3.2.2) mime-types-data (~> 3.2015) - mime-types-data (3.2018.0812) + mime-types-data (3.2019.0331) mimemagic (0.3.3) mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) - minitest-rails (3.0.0) - minitest (~> 5.8) - railties (~> 5.0) + minitest-rails (5.2.0) + minitest (~> 5.10) + railties (~> 5.2.0) minitest-reporters (1.3.6) ansi builder minitest (>= 5.0) ruby-progressbar - msgpack (1.2.10) + msgpack (1.3.0) multi_xml (0.6.0) nio4r (2.3.1) nokogiri (1.10.3) @@ -115,7 +115,7 @@ GEM pry (>= 0.10.4) puma (3.12.1) rack (2.0.7) - rack-cors (1.0.2) + rack-cors (1.0.3) rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.2.3) @@ -196,4 +196,4 @@ RUBY VERSION ruby 2.5.1p57 BUNDLED WITH - 1.17.3 + 2.0.1 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..581795fb 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,29 @@ def show ) end + def create + if Movie.does_movie_exist(movie_params[:external_id]) + render json: {errors: ["#{movie_params[:title]} already exists in library. Qty 1 added to the inventory."], + status: :ok} + return + end + # Rails has set up that everything that comes in from a request as query params... gets into the params object + @movie = Movie.new(movie_params) + # puts "we're in the create function ****************************************" + # puts params#["apples"] + # How do we make sure that this new instance of movie actually has the values coming from the request ... Hm...(strong params!) + # Traditionally, we have put this functionality of reading from the request and also some cool rails security things using Strong Params + + + if @movie.save + render json: {movie: {id: @movie.id, title: @movie.title}}, status: :ok + else + render json: {errors: @movie.errors.messages}, + status: :bad_request + end + + end + private def require_movie @@ -29,4 +52,9 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + +def movie_params + return params.require(:movie).permit(:title, :overview, :release_date, :inventory, :external_id, :image_url) +end + end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..7ba15441 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,10 +7,12 @@ def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) if rental.save - render status: :ok, json: {} + render status: :ok, json: {title: params[:title], customer_id: params[:customer_id], due_date: params[:due_date]} else render status: :bad_request, json: { errors: rental.errors.messages } end + + # decrease inventory quantity? end def check_in @@ -24,7 +26,7 @@ def check_in end rental.returned = true if rental.save - render status: :ok, json: {} + render status: :ok, json: {movie: @movie.title, customer: @customer.name, due: params[:due_date]} else render status: :bad_request, json: { errors: rental.errors.messages } end diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..240efe7b 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -6,6 +6,19 @@ def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end + def self.does_movie_exist(external_id) + existingMovie = Movie.find_by(external_id: external_id) + + if existingMovie + existingMovie.inventory += 1 + existingMovie.save + return true + end + + return false + + end + def image_url orig_value = read_attribute :image_url if !orig_value 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" 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 diff --git a/db/seeds.rb b/db/seeds.rb index 6abb1e9f..ef99aa67 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,6 +7,8 @@ JSON.parse(File.read('db/seeds/movies.json')).each do |movie_data| movies = MovieWrapper.search(movie_data["title"]) ap "#{movie_data['title']} Added to the library!" - movies.first.inventory = movie_data['inventory'] - movies.first.save unless movies.empty? -end + if movies + movies.first.inventory = movie_data['inventory'] + movies.first.save unless movies.empty? + end +end \ No newline at end of file diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 0152c40e..3c51bc53 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -12,7 +12,7 @@ def self.search(query) response = HTTParty.get(url) if response["total_results"] == 0 return [] - else + elsif response["results"] movies = response["results"].map do |result| self.construct_movie(result) end