Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ports - Shamira, Ngoc, Elle #20

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -196,4 +196,4 @@ RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.17.3
2.0.1
28 changes: 28 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 4 additions & 2 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
59 changes: 31 additions & 28 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down