Skip to content

Commit

Permalink
adds users api controller
Browse files Browse the repository at this point in the history
  • Loading branch information
citin committed Jul 12, 2019
1 parent 2ebc09c commit 91fdd9d
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
class Api::V1::UsersController < ApplicationController

rescue_from User::AlreadyPurchasedError, with: :render_method_not_allowed
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response

def render_not_found_response(exception)
render json: { error: exception.message }, status: :not_found
end

def render_method_not_allowed(exception)
render json: { error: exception.message }, status: 405
end

# POST /api/v1/users/:id/purchase
# {
# "content_id": 1,
# "content_type": "Movie",
# "purchase_option":
# {
# "price": 2.99,
# "video_quality": "HD"
# }
# }
# Params: { "purchase_option_id": 1 }
def purchase
# build content = params[:content_tipe].classify.find(params[:content_id])
# check if content is not in user's library
# build purchase w/options
# po = PurchaseOption.create(params[:purchase_option])
# todo: before_action
purchase_option = PurchaseOption.find(params[:purchase_option_id])
User.find(params[:id]).purchase(purchase_option)
render status: 200
end

# GET /api/v1/users/:id/purchases
def purchases
render json: User.find(params[:id]).library.sort_by(&:remaining_time)
render json: User.find(params[:id]).library
end
end

0 comments on commit 91fdd9d

Please sign in to comment.