Skip to content

Commit

Permalink
wrote methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MonalisaC committed May 9, 2018
1 parent dc73a4f commit 16226a4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
8 changes: 4 additions & 4 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def create
end
end

private
private

def movies_params
return params.permit(:title,:overview,:release_date,:inventory)
end
def movies_params
return params.permit(:title, :overview, :release_date, :inventory)
end
end
66 changes: 45 additions & 21 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
class RentalsController < ApplicationController

# def check_out
#
# rental = Rental.new(rental_params)
# if rental.save
#
# if @rental
#
# render json: @rental.as_json(rental.customer_id,rental.video_id),status: :ok
#
# else
# render json: {ok: false,errors:"movie not found"}, status: :not_found
# end
#
# end
#
# def check_in
# end
#
# def rentals_params
# return params.permit(:customer_id,:movie_id,:due_date,:checkout_date)
# end
def check_out
customer = Customer.find_by(id: params[:customer_id])
if customer.nil?
render json: { ok: false, errors: "invalid customer id"}, status: :not_found
return
end
movie = Movie.find_by(id: params[:movie_id])
if movie.nil?
render json: { ok: false, errors: "invalid movid id"}, status: :not_found
return
end
if movie.available_inventory < 1
render json: { ok: false, errors: "movie not in inventory"}, status: :not_found
return
end
rental = Rental.new(rental_params)
rental.checkout_date = Date.today
rental.due_date = Date.today + 7
movie.inventory -= 1
if rental.save && movie.save
render json: rental.as_json(only: [:customer_id, :movie_id, :due_date]), status: :ok
else
render json: {ok: false, errors: "could not rent"}, status: :not_found
end
end

def check_in
rental = Rental.where(customer_id: params[:customer_id], movie_id: params[:movie_id])[0]
if rental.nil?
render json: {errors: "rental not found" }, status: :not_found
else
rental.movie.inventory += 1
unless rental.customer.save && rental.movie.save
render json: {errors: {customer: customer.errors.messages, movie: movie.errors.messages} }, status: :bad_request
end
render json: rental.as_json(only: [:customer_id, :movie_id])
rental.destroy
end
end

private

def rental_params
params.permit(:customer_id, :movie_id)
end
end

0 comments on commit 16226a4

Please sign in to comment.