Skip to content

Commit

Permalink
get basic api working
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewroth committed Dec 13, 2024
1 parent 9c55aa9 commit 2726c67
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
20 changes: 20 additions & 0 deletions app/admin/api_keys.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ActiveAdmin.register ApiKey do

# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# Uncomment all parameters which should be permitted for assignment
#
#
permit_params :access_token, :user

#
# or
#
# permit_params do
# permitted = [:access_token, :user]
# permitted << :other if params[:action] == 'create' && current_user.admin?
# permitted
# end

end
1 change: 1 addition & 0 deletions app/controllers/api/v1/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Api::V1::ApplicationController < ApplicationController

respond_to :json

skip_forgery_protection
#skip_before_action :require_login, raise: false
#skip_before_action :check_url

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/v1/gift_cards_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
class Api::V1::GiftCardsController < Api::V1::ApplicationController
def show
gift_card = GiftCard.find(params[:id])
gift_card = GiftCard.find_by(certificate: params[:id])

render json: gift_card
end

# an update automatically means to use up one registration
def update
gift_card = GiftCard.find(params[:id])
gift_card = GiftCard.find_by(certificate: params[:id])

if gift_card.registrations_available.to_i == 0
if gift_card.registrations_available.to_i <= 0
render json: {
error: "No registrations available",
status: 403
}, status: 403
return
end

gift_card.update(registrations_available: registrations_available - 1)
gift_card.update(registrations_available: gift_card.registrations_available - 1)
render json: gift_card
end
end
Expand Down
17 changes: 17 additions & 0 deletions app/models/api_key.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
class ApiKey < ApplicationRecord
before_create :generate_access_token
after_initialize :generate_access_token

def self.ransackable_attributes(auth_object = nil)
["access_token", "created_at", "id", "id_value", "updated_at", "user"]
end

private

def generate_access_token
return if !new_record? || access_token.present?

loop do
self.access_token ||= SecureRandom.hex
break unless self.class.exists?(access_token: access_token)
end
end
end
12 changes: 7 additions & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
ActiveAdmin.routes(self)
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Render dynamic PWA files from app/views/pwa/*
get "service-worker" => "rails/pwa#service_worker", as: :pwa_service_worker
get "manifest" => "rails/pwa#manifest", as: :pwa_manifest

# Reveal health status that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "monitors/lb" => "monitors#lb"

namespace :api do
namespace :v1 do
resources :gift_cards, only: [:show, :update]
end
end

# Defines the root path route ("/")
# root "posts#index"
get "/", to: redirect("/admin")
end

0 comments on commit 2726c67

Please sign in to comment.