diff --git a/.elasticbeanstalk/config.yml b/.elasticbeanstalk/config.yml new file mode 100644 index 00000000..4102d904 --- /dev/null +++ b/.elasticbeanstalk/config.yml @@ -0,0 +1,2 @@ +global: + profile: eb-cli diff --git a/.gitignore b/.gitignore index 4a494a75..614c3613 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ # Ignore master key for decrypting credentials and more. /config/master.key + +.env diff --git a/Gemfile b/Gemfile index 2d53ba9c..fa19b64a 100644 --- a/Gemfile +++ b/Gemfile @@ -52,7 +52,6 @@ end gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] group :development, :test do - gem 'pry-rails' gem 'dotenv-rails' end diff --git a/Gemfile.lock b/Gemfile.lock index a0a07df1..543ce8d7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,7 +56,6 @@ GEM byebug (10.0.2) case_transform (0.2) activesupport - coderay (1.1.2) concurrent-ruby (1.1.4) crass (1.0.4) dotenv (2.5.0) @@ -110,11 +109,6 @@ GEM nokogiri (1.8.5) mini_portile2 (~> 2.3.0) pg (1.1.3) - pry (0.12.2) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - pry-rails (0.3.8) - pry (>= 0.10.4) puma (3.12.0) rack (2.0.6) rack-cors (1.0.2) @@ -186,7 +180,6 @@ DEPENDENCIES minitest-rails minitest-reporters pg (>= 0.18, < 2.0) - pry-rails puma (~> 3.11) rack-cors rails (~> 5.2.0) @@ -199,4 +192,4 @@ RUBY VERSION ruby 2.5.1p57 BUNDLED WITH - 1.16.6 + 1.17.2 diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..7c6d043e 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -10,7 +10,7 @@ def index data = Customer.all end - data = data.paginate(page: params[:p], per_page: params[:n]) + # data = data.paginate(page: params[:p], per_page: params[:n]) render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit], @@ -18,7 +18,29 @@ def index ) end -private + + def create + + customer = Customer.new(customer_params) + + if customer.save + render( + status: :ok, json: customer.as_json( only: [:name, :id] ) + ) + else + #errors + render( + status: :bad_request, json: {errors: {customer: customer.errors.messages }} + ) + end + end + + private + + def customer_params + params.permit(:name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit) + end + def parse_query_args errors = {} @sort = params[:sort] diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..bde2d437 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,8 +17,29 @@ def show json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) + end + + def create + movie_exists = Movie.find_by(external_id: params[:id]) + if movie_exists + render status: :bad_request, json: {errors: {movie: "Movie already exists in library"}} + else + movie = MovieWrapper.get_movie(params[:id]) + + if movie + movie.save + render( + status: :ok, json: movie.as_json( only: [:title] ) + ) + else + #errors + render( + status: :bad_request, json: {errors: {id: 'must be present'}} + ) + end + end end private diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..6f284716 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,6 +1,7 @@ class Customer < ApplicationRecord has_many :rentals has_many :movies, through: :rentals + validates :name, presence: true def movies_checked_out_count self.rentals.where(returned: false).length diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..f9f47207 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,6 +1,7 @@ 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 diff --git a/config/routes.rb b/config/routes.rb index f4c99688..2962870e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,11 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :customers, only: [:index] + resources :customers, only: [:index, :create] + # resources :customers, only: [:create] resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:create], param: :id 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/lib/movie_wrapper.rb b/lib/movie_wrapper.rb index c51d05ee..3ca024e3 100644 --- a/lib/movie_wrapper.rb +++ b/lib/movie_wrapper.rb @@ -1,4 +1,3 @@ - class MovieWrapper BASE_URL = "https://api.themoviedb.org/3/" KEY = ENV["MOVIEDB_KEY"] @@ -8,13 +7,16 @@ class MovieWrapper DEFAULT_IMG_URL = "http://lorempixel.com/185/278/" def self.search(query) + url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query + puts url # puts url response = HTTParty.get(url) if response["total_results"] == 0 return [] else + puts response movies = response["results"].map do |result| self.construct_movie(result) @@ -23,6 +25,18 @@ def self.search(query) end end + def self.get_movie(id) + + url = "#{BASE_URL}movie/#{id}?api_key=#{KEY}" + response = HTTParty.get(url) + + if response["id"] + return self.construct_movie(response) + else + return nil + end + end + private def self.construct_movie(api_result) @@ -32,10 +46,10 @@ def self.construct_movie(api_result) release_date: api_result["release_date"], image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), external_id: api_result["id"]) - end + end - def self.construct_image_url(img_name) - return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name - end + def self.construct_image_url(img_name) + return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name + end -end + end