Skip to content

Commit 23854d0

Browse files
authored
Merge branch 'main' into 1130_store-and-return-scratch-data
2 parents 8283e49 + fb74973 commit 23854d0

19 files changed

Lines changed: 132 additions & 81 deletions

File tree

.clabot

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"jamiebenstead",
2020
"jrmhaig",
2121
"KristinaDudnyk",
22+
"kylec-rpf",
2223
"loiswells97",
2324
"lpmi-13",
2425
"m-bowley",
@@ -35,4 +36,4 @@
3536
"zetter-rpf"
3637
],
3738
"message": "We require contributors to sign our Contributor License Agreement, and we don't have you on file. In order for us to review and merge your code, please complete [this form](https://form.raspberrypi.org/4873530) and we'll get you added and review your contribution as soon as possible."
38-
}
39+
}

.erdconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
filename: EditorAPI-ERD
2+
title: 'Editor API Entity-Relationship Diagram (ERD)'

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gem 'image_processing'
2828
gem 'importmap-rails'
2929
gem 'jbuilder'
3030
gem 'kaminari'
31-
gem 'omniauth-rails_csrf_protection', '~> 1.0.1'
31+
gem 'omniauth-rails_csrf_protection', '~> 2.0.1'
3232
gem 'omniauth-rpi',
3333
github: 'RaspberryPiFoundation/omniauth-rpi',
3434
tag: 'v1.3.1'
@@ -80,4 +80,4 @@ group :test do
8080
gem 'webmock'
8181
end
8282

83-
gem 'flipper-ui', '~> 1.3'
83+
gem 'flipper-ui', '~> 1.4'

Gemfile.lock

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ GEM
178178
factory_bot_rails (6.5.1)
179179
factory_bot (~> 6.5)
180180
railties (>= 6.1.0)
181-
faker (3.5.2)
181+
faker (3.6.0)
182182
i18n (>= 1.8.11, < 2)
183183
faraday (2.14.1)
184184
faraday-net_http (>= 2.0, < 3.5)
@@ -190,14 +190,14 @@ GEM
190190
ffi (1.17.2-arm64-darwin)
191191
ffi (1.17.2-x86_64-linux-gnu)
192192
fiber-storage (1.0.1)
193-
flipper (1.3.6)
193+
flipper (1.4.0)
194194
concurrent-ruby (< 2)
195-
flipper-active_record (1.3.6)
195+
flipper-active_record (1.4.0)
196196
activerecord (>= 4.2, < 9)
197-
flipper (~> 1.3.6)
198-
flipper-ui (1.3.6)
197+
flipper (~> 1.4.0)
198+
flipper-ui (1.4.0)
199199
erubi (>= 1.0.0, < 2.0.0)
200-
flipper (~> 1.3.6)
200+
flipper (~> 1.4.0)
201201
rack (>= 1.4, < 4)
202202
rack-protection (>= 1.5.3, < 5.0.0)
203203
rack-session (>= 1.0.2, < 3.0.0)
@@ -228,7 +228,8 @@ GEM
228228
activesupport (>= 3.0)
229229
graphql (>= 1.13.0)
230230
hashdiff (1.2.0)
231-
hashie (5.0.0)
231+
hashie (5.1.0)
232+
logger
232233
i18n (1.14.8)
233234
concurrent-ruby (~> 1.0)
234235
image_processing (1.14.0)
@@ -315,14 +316,15 @@ GEM
315316
rack (>= 1.2, < 4)
316317
snaky_hash (~> 2.0, >= 2.0.3)
317318
version_gem (>= 1.1.8, < 3)
318-
omniauth (2.1.3)
319+
omniauth (2.1.4)
319320
hashie (>= 3.4.6)
321+
logger
320322
rack (>= 2.2.3)
321323
rack-protection
322324
omniauth-oauth2 (1.8.0)
323325
oauth2 (>= 1.4, < 3)
324326
omniauth (~> 2.0)
325-
omniauth-rails_csrf_protection (1.0.2)
327+
omniauth-rails_csrf_protection (2.0.1)
326328
actionpack (>= 4.2)
327329
omniauth (~> 2.0)
328330
open-uri (0.5.0)
@@ -366,7 +368,7 @@ GEM
366368
rack-cors (3.0.0)
367369
logger
368370
rack (>= 3.0.14)
369-
rack-protection (4.1.1)
371+
rack-protection (4.2.1)
370372
base64 (>= 0.1.0)
371373
logger (>= 1.6.0)
372374
rack (>= 3.0.0, < 4)
@@ -468,7 +470,7 @@ GEM
468470
rubocop-capybara (2.22.1)
469471
lint_roller (~> 1.1)
470472
rubocop (~> 1.72, >= 1.72.1)
471-
rubocop-factory_bot (2.27.1)
473+
rubocop-factory_bot (2.28.0)
472474
lint_roller (~> 1.1)
473475
rubocop (~> 1.72, >= 1.72.1)
474476
rubocop-graphql (1.5.6)
@@ -609,7 +611,7 @@ DEPENDENCIES
609611
faraday
610612
flipper (~> 1.3)
611613
flipper-active_record (~> 1.3)
612-
flipper-ui (~> 1.3)
614+
flipper-ui (~> 1.4)
613615
github_webhook (~> 1.4)
614616
globalid
615617
good_job (~> 4.3)
@@ -621,7 +623,7 @@ DEPENDENCIES
621623
importmap-rails
622624
jbuilder
623625
kaminari
624-
omniauth-rails_csrf_protection (~> 1.0.1)
626+
omniauth-rails_csrf_protection (~> 2.0.1)
625627
omniauth-rpi!
626628
open-uri
627629
paper_trail

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ Handled in `config/initializers/cors.rb`.
151151

152152
This API receives push event data from the [Raspberry Pi Learning](https://github.com/raspberrypilearning) organisation via webhooks. This data is used to create or update code projects related to the [Code Club Projects Site](https://projects.raspberrypi.org), and is processed using the `github_webhooks` gem in the `github_webhooks_controller`. For development purposes, these webhooks are mediated locally through `smee`, which runs in a Docker container.
153153

154+
### Database Structure
155+
156+
Once you have completed the initial setup and have everything running in a container, you can generate an Entity-Relationship Diagram (ERD) to help you to understand the database structure. In order to do this, start things up as usual in one terminal:
157+
158+
`docker-compose up`
159+
160+
then in a second terminal, create the ERD using:
161+
162+
`docker-compose exec api bundle exec erd`
163+
164+
then copy the created ERD out of the container with:
165+
166+
`docker cp "$(docker-compose ps -q api)":/app/EditorAPI-ERD.pdf .`
167+
154168
## Usage
155169

156170
### Projects

app/controllers/api/projects/remixes_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class RemixesController < ApiController
99

1010
def index
1111
projects = Project.where(remixed_from_id: project.id).accessible_by(current_ability)
12-
@projects_with_users = projects.includes(:school_project).with_users(current_user)
13-
render index: @projects_with_users, formats: [:json]
12+
@projects_with_students = projects.includes(:school_project).with_students(project.school, current_user)
13+
render index: @projects_with_students, formats: [:json]
1414
end
1515

1616
def show

app/controllers/api/projects_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def index
2121

2222
def show
2323
if !@project.school_id.nil? && @project.lesson_id.nil?
24-
project_with_user = @project.with_user(current_user)
24+
project_with_user = @project.with_student(current_user)
2525
@user = project_with_user[1]
2626
end
2727

app/controllers/api_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,12 @@ def authorize_user
2323
render json: { error: 'Unauthorized' }, status: :unauthorized unless current_user
2424
end
2525

26-
def denied(exception)
27-
render_error_as_json(exception, :forbidden)
26+
def denied(_exception)
27+
if current_user
28+
render json: { error: 'Forbidden' }, status: :forbidden
29+
else
30+
render json: { error: 'Unauthorized' }, status: :unauthorized
31+
end
2832
end
2933

3034
def not_found(exception)

app/models/project.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,19 @@ module Types
4545
}
4646
)
4747

48-
def self.users(current_user)
49-
school = School.find_by(id: pluck(:school_id))
48+
def self.students(school, current_user)
5049
SchoolStudent::List.call(school:, token: current_user.token, student_ids: pluck(:user_id).uniq)[:school_students] || []
5150
end
5251

53-
def self.with_users(current_user)
54-
by_id = users(current_user).index_by(&:id)
52+
def self.with_students(school, current_user)
53+
by_id = students(school, current_user).index_by(&:id)
5554
all.map { |instance| [instance, by_id[instance.user_id]] }
5655
end
5756

58-
def with_user(current_user)
57+
def with_student(current_user)
5958
# students cannot call the Profile API so do not even try
6059
return [self, nil] if current_user&.student?
6160

62-
school = School.find_by(id: school_id)
6361
students = SchoolStudent::List.call(school:, token: current_user.token,
6462
student_ids: [user_id])[:school_students] || []
6563
[self, students.first]

app/views/api/projects/remixes/index.json.jbuilder

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
json.array!(@projects_with_users) do |project, user|
3+
json.array!(@projects_with_students) do |project, student|
44
json.call(
55
project,
66
:identifier,
@@ -11,7 +11,7 @@ json.array!(@projects_with_users) do |project, user|
1111
:last_edited_at
1212
)
1313

14-
json.user_name(user&.name)
14+
json.user_name(student&.name)
1515
json.finished(project.school_project&.finished)
1616
json.status(project.school_project&.status)
1717
end

0 commit comments

Comments
 (0)