Skip to content

Commit 56cf273

Browse files
committed
Add athlete results and volunteering JSON endpoint
1 parent 0b2c74e commit 56cf273

3 files changed

Lines changed: 89 additions & 1 deletion

File tree

app/controllers/athletes_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ def index
1717

1818
def show
1919
@athlete = Athlete.find params.expect(:id)
20-
return redirect_unregistered_athlete if !@athlete.user_id && @athlete.fiveverst_code
20+
21+
if !@athlete.user_id && @athlete.fiveverst_code
22+
return head :not_found if request.format.json?
23+
24+
return redirect_unregistered_athlete
25+
end
2126

2227
load_athlete_results
2328
load_athlete_volunteering
29+
30+
return if request.format.json?
31+
2432
@total_events_count = total_events_count
2533
@total_trophies = @athlete.trophies.size
2634
@barcode = BarcodeService.call("A#{@athlete.code}", module_size: 8)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
json.athlete do
2+
json.extract! @athlete, :id, :name, :gender, :parkrun_code
3+
json.code @athlete.code
4+
json.home_event @athlete.event&.name
5+
json.club @athlete.club&.name
6+
end
7+
8+
json.total_results @results.size
9+
json.total_volunteering @volunteering.size
10+
11+
json.results @results do |result|
12+
json.date result.date.iso8601
13+
json.total_time result.time_string
14+
json.position result.position
15+
json.personal_best result.personal_best
16+
json.first_run result.first_run
17+
json.event result.activity.event, :code_name, :name, :town
18+
end
19+
20+
json.volunteering @volunteering do |volunteer|
21+
json.date volunteer.date.iso8601
22+
json.role volunteer.role
23+
json.event volunteer.activity.event, :code_name, :name, :town
24+
end

spec/requests/athletes_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,62 @@
6363
end
6464
end
6565

66+
describe 'GET /athletes/1.json' do
67+
let(:athlete) { create(:athlete, fiveverst_code: nil) }
68+
let!(:result) { create(:result, athlete:) }
69+
let!(:volunteer) { create(:volunteer, athlete:) }
70+
71+
let(:expected_body) do
72+
{
73+
'athlete' => {
74+
'id' => athlete.id,
75+
'name' => athlete.name,
76+
'gender' => 'male',
77+
'parkrun_code' => athlete.parkrun_code,
78+
'code' => athlete.code,
79+
'home_event' => athlete.event&.name,
80+
'club' => athlete.club&.name,
81+
},
82+
'total_results' => 1,
83+
'total_volunteering' => 1,
84+
'results' => [
85+
{
86+
'date' => result.date.iso8601,
87+
'total_time' => result.time_string,
88+
'position' => result.position,
89+
'personal_best' => result.personal_best,
90+
'first_run' => result.first_run,
91+
'event' => result.activity.event.as_json(only: %i[code_name name town]),
92+
},
93+
],
94+
'volunteering' => [
95+
{
96+
'date' => volunteer.date.iso8601,
97+
'role' => volunteer.role,
98+
'event' => volunteer.activity.event.as_json(only: %i[code_name name town]),
99+
},
100+
],
101+
}
102+
end
103+
104+
it 'renders athlete with results and volunteering in JSON' do
105+
get athlete_url(athlete, format: :json)
106+
107+
expect(response).to be_successful
108+
expect(response.parsed_body).to eq(expected_body)
109+
end
110+
111+
context 'when athlete is unregistered and has fiveverst_code' do
112+
let(:hidden_athlete) { create(:athlete, parkrun_code: nil) }
113+
114+
it 'returns not found' do
115+
get athlete_url(hidden_athlete, format: :json)
116+
117+
expect(response).to have_http_status(:not_found)
118+
end
119+
end
120+
end
121+
66122
describe 'GET /athletes/:code/best_result' do
67123
let(:athlete) { create(:athlete) }
68124

0 commit comments

Comments
 (0)