Skip to content

Commit

Permalink
add mobile api
Browse files Browse the repository at this point in the history
  • Loading branch information
vol1ura committed Jan 10, 2025
1 parent 47898db commit 39509ba
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/controllers/api/mobile/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

module API
module Mobile
class ApplicationController < ActionController::API
AUTHORIZATION_HEADER = "Bearer #{ENV.fetch('MOBILE_API_KEY')}".freeze

respond_to :json
before_action :authorize_request
around_action :switch_locale

rescue_from(ActiveRecord::RecordNotFound) { head :not_found }

private

def switch_locale(&)
locale = params[:locale]&.to_sym || I18n.default_locale

I18n.with_locale(locale, &)
end

def authorize_request
return if request.headers['Authorization'] == AUTHORIZATION_HEADER

head :unauthorized
end
end
end
end
24 changes: 24 additions & 0 deletions app/controllers/api/mobile/athletes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module API
module Mobile
class AthletesController < ApplicationController
before_action :find_athlete

def info; end

private

def find_athlete
code = params[:code].to_i
raise ActiveRecord::RecordNotFound unless code.positive?

@athlete = Athlete.find_by!(**Athlete::PersonalCode.new(code).to_params)
end

def athlete_params
params.require(:athlete).permit(:club_id, :event_id)
end
end
end
end
14 changes: 14 additions & 0 deletions app/views/api/mobile/athletes/info.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
json.call(@athlete, :name, :male)
json.home_event @athlete.event&.name
json.volunteering do
json.stats @athlete.stats['volunteers']
json.scheduled(
@athlete
.volunteering
.includes(activity: :event)
.where(activity: { date: Date.current.. })
.rewhere(activity: { published: false })
.reorder(:date)
.map { |v| { event_name: v.activity.event_name, date: v.date.to_s, role: v.role } },
)
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
resources :athletes, only: :update
resources :activities, only: :create
end
namespace :mobile do
get 'athletes/:code/info', to: 'athletes#info'
end
end

authenticate :user, ->(user) { user.admin? } do
Expand Down
2 changes: 2 additions & 0 deletions deploy/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ VK_GROUP_ID=
BOT_TOKEN=
DEV_TELEGRAM_ID=

MOBILE_API_KEY=

ROLLBAR_ACCESS_TOKEN=

BULLET_ENABLED=true
Expand Down

0 comments on commit 39509ba

Please sign in to comment.