forked from AdaGold/video-store-api
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrentals_controller.rb
34 lines (27 loc) · 997 Bytes
/
rentals_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'date'
class RentalsController < ApplicationController
def check_in
rental = Rental.find_by(customer_id: rental_params[:customer_id], movie_id: rental_params[:movie_id], check_in_status: false)
if rental
rental.update(check_in_status: true)
render json: { status: 'Success', rental: rental }, status: :ok
else
render json: { errors: 'Rental does not exist' }, status: :bad_request
end
end
def check_out
rental = Rental.new(rental_params)
rental.checked_out = DateTime.now.to_date
rental.due_date = rental.checked_out + 7
rental.check_in_status = false
if rental.save
render json: { id: rental.id, movie_id: rental.movie.id, customer_id: rental.customer.id, checked_out_date: rental.checked_out, due_date: rental.due_date }, status: :ok
else
render json: { errors: rental.errors }, status: :bad_request
end
end
private
def rental_params
return params.permit(:movie_id, :customer_id)
end
end