|
1 | 1 | class SubscriptionsController < ApiController
|
| 2 | + |
| 3 | + # GET /subscriptions/new |
| 4 | + def new |
| 5 | + @subscription = Subscription.new |
| 6 | + end |
| 7 | + |
| 8 | + # POST /subscriptions |
2 | 9 | def create
|
3 |
| - authed_session = HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139') |
4 |
| - new_sub_request = HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139/validate') |
5 |
| - new_sub_response = JSON.parse(new_sub_request) |
6 |
| - |
7 |
| - if new_sub_response == response_ok |
8 |
| - subscription = Subscription.new(params) |
9 |
| - return 'subscription created and payment successful' |
10 |
| - elsif new_sub_response == response_fail |
11 |
| - return 'subscription not created due to insufficient funds' |
12 |
| - elsif new_sub_response == response_timeout |
13 |
| - sleep 15 |
14 |
| - # do that stuff again |
15 |
| - elsif new_sub_response == response_503 |
16 |
| - return 'Service Unavailable' |
| 10 | + if authed_session |
| 11 | + new_sub_request = HTTParty.get('https://gist.github.com/freezepl/2a75c29c881982645156f5ccf8d1b139/validate') |
| 12 | + new_sub_response = JSON.parse(new_sub_request) |
| 13 | + |
| 14 | + if new_sub_response == response_ok |
| 15 | + subscription = Subscription.new(subscription_params) |
| 16 | + return 'subscription created and payment successful' |
| 17 | + elsif new_sub_response == response_fail |
| 18 | + return 'subscription not created due to insufficient funds' |
| 19 | + elsif new_sub_response == response_timeout |
| 20 | + sleep 15 |
| 21 | + # do that stuff again |
| 22 | + elsif new_sub_response == response_503 |
| 23 | + return 'Service Unavailable' |
| 24 | + end |
| 25 | + else |
| 26 | + return 'Invalid session' |
17 | 27 | end
|
18 | 28 | end
|
19 | 29 |
|
| 30 | + #GET /subscriptions |
20 | 31 | def index
|
21 | 32 | subscriptions = Subscription.all
|
22 | 33 | render json: { status: 'SUCCESS', message: 'Loaded all subscriptions', data: subscriptions }, status: :ok
|
23 | 34 | end
|
24 | 35 |
|
| 36 | + # GET /subscriptions/1 |
25 | 37 | def show
|
26 | 38 | render json: { status: 'SUCCESS', message: 'Got your sub, bro', data: @subscription }, status: :ok
|
27 | 39 | end
|
28 | 40 |
|
| 41 | + # GET /subscriptions/user_id?{@user} |
29 | 42 | def show_all_for_user
|
30 | 43 | render json: { status: 'SUCCESS', message: 'All subs for user', data: @user_subscriptions }, status: :ok
|
31 | 44 | end
|
32 | 45 |
|
33 | 46 | private
|
34 | 47 |
|
35 | 48 | def subscription_params
|
36 |
| - params[:subscription].permit(:user_id, :paid, :billing_date, :cc_number, :cc_expiration, :cc_code) |
| 49 | + params[:subscription].permit(:user_id, :paid, :billing_date, :amount, :cc_number, :cc_expiration, :cc_code) |
37 | 50 | end
|
38 | 51 |
|
39 | 52 | def find_subscription
|
|
0 commit comments