Skip to content

Commit

Permalink
Add sponsorships and sponsors endpoints to accounts API
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Nov 28, 2024
1 parent d0f8223 commit eb11bd6
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 4 deletions.
14 changes: 14 additions & 0 deletions app/controllers/api/v1/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,18 @@ def sponsors
@pagy, @accounts = pagy(scope)
render :index
end

def sponsorships
@account = Account.find_by_login(params[:account_id])
scope = @account.sponsorships_as_maintainer.order('created_at DESC').includes(:maintainer, :funder)
@pagy, @sponsorships = pagy(scope)
render 'api/v1/sponsorships/index'
end

def account_sponsors
@account = Account.find_by_login(params[:account_id])
scope = @account.sponsorships_as_funder.order('created_at DESC').includes(:maintainer, :funder)
@pagy, @sponsorships = pagy(scope)
render 'api/v1/sponsorships/index'
end
end
6 changes: 4 additions & 2 deletions app/views/api/v1/accounts/_account.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
json.extract! account, :id, :login, :data, :created_at, :updated_at, :last_synced_at, :sponsors_count, :sponsorships_count, :active_sponsorships_count, :sponsor_profile
json.extract! account, :id, :login, :has_sponsors_listing, :data, :created_at, :updated_at, :last_synced_at, :sponsors_count, :sponsorships_count, :active_sponsorships_count, :sponsor_profile

json.url account_url(account.login)
json.api_url api_v1_account_url(account.login)
json.html_url account.html_url
json.sponsors_url account.sponsors_url
json.sponsors_url account.sponsors_url
json.sponsors_api_url api_v1_account_sponsors_url(account.login)
json.sponsorships_api_url api_v1_account_sponsorships_url(account.login)
8 changes: 8 additions & 0 deletions app/views/api/v1/sponsorships/_sponsorship.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
json.extract! sponsorship, :id, :status, :created_at, :updated_at
json.funder do
json.partial! 'api/v1/accounts/account', account: sponsorship.funder
end

json.maintainer do
json.partial! 'api/v1/accounts/account', account: sponsorship.maintainer
end
1 change: 1 addition & 0 deletions app/views/api/v1/sponsorships/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @sponsorships, partial: 'api/v1/sponsorships/sponsorship', as: :sponsorship
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@

namespace :api, :defaults => {:format => :json} do
namespace :v1 do
resources :accounts, only: [:index, :show]
resources :accounts, only: [:index, :show] do
get 'sponsors', to: 'accounts#account_sponsors'
get 'sponsorships', to: 'accounts#sponsorships'
end
get 'sponsors', to: 'accounts#sponsors'
end
end
Expand Down
114 changes: 113 additions & 1 deletion openapi/api/v1/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,76 @@ paths:
$ref: "#/components/schemas/Account"
'404':
description: "Account not found."
/account/{login}/sponsors:
get:
summary: "List Account Sponsors"
description: "List all sponsors for an account."
operationId: listAccountSponsors
parameters:
- name: login
in: path
description: "The login name for the account."
required: true
schema:
type: string
- name: page
in: query
description: pagination page number
required: false
schema:
type: integer
- name: per_page
in: query
description: Number of records to return
required: false
schema:
type: integer
responses:
'200':
description: "A list of sponsors."
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Sponsorship"
'404':
description: "No sponsors found."
/account/{login}/sponsorships:
get:
summary: "List Account Sponsorships"
description: "List all sponsorships for an account."
operationId: listAccountSponsorships
parameters:
- name: login
in: path
description: "The login name for the account."
required: true
schema:
type: string
- name: page
in: query
description: pagination page number
required: false
schema:
type: integer
- name: per_page
in: query
description: Number of records to return
required: false
schema:
type: integer
responses:
'200':
description: "A list of sponsorships."
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/Sponsorship"
'404':
description: "No sponsorships found."
/sponsors:
get:
summary: "List Sponsors"
Expand Down Expand Up @@ -102,6 +172,7 @@ components:
required:
- id
- login
- has_sponsors_listing
- data
- created_at
- updated_at
Expand All @@ -114,6 +185,8 @@ components:
- api_url
- html_url
- sponsors_url
- sponsors_api_url
- sponsorships_api_url
type: object
properties:
id:
Expand All @@ -123,6 +196,9 @@ components:
login:
type: string
description: "The login name for the account."
has_sponsors_listing:
type: boolean
description: "Indicates if the account has a sponsors listing."
data:
type: object
description: "The raw data for the account."
Expand Down Expand Up @@ -164,4 +240,40 @@ components:
description: "The HTML URL for the account."
sponsors_url:
type: string
description: "The sponsors URL for the account."
description: "The sponsors URL for the account."
sponsors_api_url:
type: string
description: "The sponsors API URL for the account."
sponsorships_api_url:
type: string
description: "The sponsorships API URL for the account."
Sponsorship:
required:
- id
- status
- created_at
- updated_at
- funder
- maintainer
type: object
properties:
id:
type: integer
format: int64
description: "The unique identifier for the sponsorship."
status:
type: string
description: "The status of the sponsorship."
created_at:
type: string
format: date-time
description: "The date and time the sponsorship was created."
updated_at:
type: string
format: date-time
description: "The date and time the sponsorship was last updated."
funder:
$ref: "#/components/schemas/Account"
maintainer:
$ref: "#/components/schemas/Account"

0 comments on commit eb11bd6

Please sign in to comment.