forked from Ada-C9/VideoStoreAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |