diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..1b8e27f4 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,17 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Rails server", + "type": "Ruby", + "request": "launch", + "program": "${workspaceRoot}/bin/rails", + "args": [ + "server" + ] + } + ] +} \ No newline at end of file diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..28437ec4 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,14 +11,35 @@ def index render status: :ok, json: data end + def new + @movie = Movie.new + end + + def create + # @movie = Movie.find_by(title: params[:title]) + + # @movie_params = request.body.read + # puts @movie_params + @movie = Movie.new + @movie.title = params[:title] + @movie.overview = params[:overview] + @movie.release_date = params[:release_date] + @movie.image_url = params[:image_url] + @movie.inventory = 1 + + save_success = @movie.save + + puts save_success + end + def show render( status: :ok, json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) + methods: [:available_inventory], + ), + ) end private diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..c185cbe6 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,6 +7,17 @@ def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) if rental.save + # If we successfully rent, get the movie and decrement its inventory + movie = Movie.find_by(id: @movie.id) + + if (movie.inventory) + movie.inventory -= 1 + else + movie.inventory = 0 + end + puts "goodbye" + movie.save + puts "hello" render status: :ok, json: {} else render status: :bad_request, json: { errors: rental.errors.messages } @@ -17,13 +28,19 @@ def check_in rental = Rental.first_outstanding(@movie, @customer) unless rental return render status: :not_found, json: { - errors: { - rental: ["Customer #{@customer.id} does not have #{@movie.title} checked out"] - } - } + errors: { + rental: ["Customer #{@customer.id} does not have #{@movie.title} checked out"], + }, + } end rental.returned = true if rental.save + # If we successfully check in the movie, increment the inventory of the movie + movie = Movie.find_by(id: @movie.id) + + movie.inventory += 1 + + movie.save render status: :ok, json: {} else render status: :bad_request, json: { errors: rental.errors.messages } @@ -33,18 +50,19 @@ def check_in def overdue rentals = Rental.overdue.map do |rental| { - title: rental.movie.title, - customer_id: rental.customer_id, - name: rental.customer.name, - postal_code: rental.customer.postal_code, - checkout_date: rental.checkout_date, - due_date: rental.due_date + title: rental.movie.title, + customer_id: rental.customer_id, + name: rental.customer.name, + postal_code: rental.customer.postal_code, + checkout_date: rental.checkout_date, + due_date: rental.due_date, } end render status: :ok, json: rentals end -private + private + # TODO: make error payloads arrays def require_movie @movie = Movie.find_by title: params[:title] diff --git a/app/models/rental.rb b/app/models/rental.rb index 18654f04..6c5024d6 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -9,7 +9,6 @@ class Rental < ApplicationRecord after_initialize :set_checkout_date after_initialize :set_returned - def self.first_outstanding(movie, customer) self.where(movie: movie, customer: customer, returned: false).order(:due_date).first end @@ -18,7 +17,8 @@ def self.overdue self.where(returned: false).where("due_date < ?", Date.today) end -private + private + def due_date_in_future return unless self.due_date unless due_date > Date.today diff --git a/app/serializers/movie_serializer.rb b/app/serializers/movie_serializer.rb index 8a1cdc35..c1871302 100644 --- a/app/serializers/movie_serializer.rb +++ b/app/serializers/movie_serializer.rb @@ -1,5 +1,5 @@ class MovieSerializer < ActiveModel::Serializer attribute :id, if: -> { object.id != nil } - attributes :title, :overview, :release_date, :image_url, :external_id + attributes :title, :overview, :release_date, :image_url, :external_id, :inventory end diff --git a/config/routes.rb b/config/routes.rb index f4c99688..ff8e7f3e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,12 +3,12 @@ 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" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" + #post "/movies", to: "movies#create", as: "movies" - root 'movies#index' - + root "movies#index" end diff --git a/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index 1c808e8d..b384d8d2 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -6,20 +6,26 @@ class MovieWrapper DEFAULT_IMG_SIZE = "w185" DEFAULT_IMG_URL = "http://lorempixel.com/185/278/" - def self.search(query, retries_left=3) + def self.search(query, retries_left = 3) raise ArgumentError.new("Can't search without a MOVIEDB_KEY. Please check your .env file!") unless KEY url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query - response = HTTParty.get(url) + response = HTTParty.get(url) if response.success? if response["total_results"] == 0 return [] else movies = response["results"].map do |result| - self.construct_movie(result) + # look in table to see if the movie already exists, if so add it to the results + movie = Movie.find_by(title: result["title"]) + if (movie == nil) + self.construct_movie(result) + end end + + movies.reject { |item| item.nil? || item == "" } return movies end elsif retries_left > 0 @@ -39,7 +45,8 @@ def self.construct_movie(api_result) overview: api_result["overview"], release_date: api_result["release_date"], image_url: self.construct_image_url(api_result["poster_path"]), - external_id: api_result["id"] + external_id: api_result["id"], + inventory: api_result["inventory"], ) end