Skip to content

Commit

Permalink
Merge pull request #1 from KKennedyCodes/ktm-api
Browse files Browse the repository at this point in the history
Ktm api
  • Loading branch information
north108 authored Dec 18, 2019
2 parents 506640d + 7ee268b commit 0beefc8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
33 changes: 31 additions & 2 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@ class RentalsController < ApplicationController
before_action :require_customer, only: [:check_out, :check_in]

# TODO: make sure that wave 2 works all the way
def index


# data = data.paginate(page: params[:p], per_page: params[:n])

# render json: data.as_json(
# only: [:id, :customer_id, :movie_id, :checkout_date, :due_date, :returned],
# )

data = Rental.all
rentals = data.map do |rental|
{
id: rental.id,
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
}
end
render status: :ok, json: rentals
end

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: {rental: rental}
else
render status: :bad_request, json: { errors: rental.errors.messages }
end
Expand Down Expand Up @@ -41,7 +65,12 @@ def overdue
due_date: rental.due_date
}
end
render status: :ok, json: rentals

if rentals.empty?
render status: :bad_request, json: { errors: "no overdue rentals"}
else
render status: :ok, json: rentals
end
end

private
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out"
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

get "/rentals/", to: "rentals#index", as: "rentals"


root 'movies#index'

end

0 comments on commit 0beefc8

Please sign in to comment.