Skip to content

Commit

Permalink
Merge pull request #15 from AustinMusiku/add-league-types
Browse files Browse the repository at this point in the history
Add league types
  • Loading branch information
AustinMusiku authored Sep 25, 2023
2 parents 733b922 + be0834e commit 10ab00d
Show file tree
Hide file tree
Showing 11 changed files with 633 additions and 3 deletions.
15 changes: 14 additions & 1 deletion codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ const config: CodegenConfig = {
PastGameweek: '@/models/managerHistory#PastGameweek',
MangerHistory: '@/models/managerHistory#ManagerHistory',
GwChip: '@/models/managerHistory#GwChip',
PastSeason: '@/models/managerHistory#PastSeason'
PastSeason: '@/models/managerHistory#PastSeason',
ClassicLeague: '@/models/classicLeague#ClassicLeague',
ClassicLeagueMeta:
'@/models/classicLeague#ClassicLeagueMeta',
ClassicLeagueStandings:
'@/models/classicLeague#ClassicLeagueStandings',
ClassicLeagueStanding:
'@/models/classicLeague#ClassicLeagueStanding',
H2hLeague: '@/models/h2hLeague#H2hLeague',
H2hLeagueMeta: '@/models/h2hLeague#H2hLeagueMeta',
H2hLeagueStandings: '@/models/h2hLeague#H2hLeagueStandings',
H2hLeagueStanding: '@/models/h2hLeague#H2hLeagueStanding',
NewEntries: '@/models/classicLeague#NewEntries',
NewEntry: '@/models/classicLeague#NewEntry'
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/fetchControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ class FetchController {
}
}

async getClassicLeague(id: number) {
try {
const url = urls.classicLeague + id + '/standings/'
return await this.fetchFromCache(`cl${id}`, url)
} catch (err) {
console.log(`Error fetching classic-league ${id}: ${err}`)
}
}

async getH2hLeague(id: number) {
try {
const url = urls.h2hLeague + id + '/standings/'
return await this.fetchFromCache(`hl${id}`, url)
} catch (err) {
console.log(`Error fetching h2h-league ${id}: ${err}`)
}
}

// -------------------------------------------------------------------
async getAllPlayers() {
try {
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ export const urls = {
playerById: 'https://fantasy.premierleague.com/api/element-summary/',
gameweekLive:
'https://fantasy.premierleague.com/api/https://fantasy.premierleague.com/api/event/{event_id}/live/',
manager: 'https://fantasy.premierleague.com/api/entry/'
manager: 'https://fantasy.premierleague.com/api/entry/',
classicLeague: 'https://fantasy.premierleague.com/api/leagues-classic/',
h2hLeague: 'https://fantasy.premierleague.com/api/leagues-h2h/'
}
65 changes: 65 additions & 0 deletions src/models/classicLeague.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Classic League
export type ClassicLeague = {
last_updated_data: string | null
league: ClassicLeagueMeta
new_entries: NewEntries
standings: ClassicLeagueStandings
}

export type ClassicLeagueMeta = LeagueMeta & {
rank: number | null
}

export type ClassicLeagueStandings = {
has_next: boolean
page: number
results: ClassicLeagueStanding[]
}

export type ClassicLeagueStanding = Standing & {
event_total: number
}

// ------------------------------------------
// NewEntries
export type NewEntries = {
has_next: boolean
page: number
results: NewEntry[]
}

export type NewEntry = {
entry: number
entry_name: string
joined_time: string
player_first_name: string
player_last_name: string
}

// LeagueMeta
export type LeagueMeta = {
id: number
name: string
created: string
closed: boolean
max_entries: number | null
league_type: string
scoring: string
admin_entry: number
start_event: number
code_privacy: string
has_cup: boolean
cup_league: null
}

// Standing
export type Standing = {
id: number
player_name: string
rank: number
last_rank: number
rank_sort: number
total: number
entry: number
entry_name: string
}
28 changes: 28 additions & 0 deletions src/models/h2hLeague.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { LeagueMeta, NewEntries, Standing } from './classicLeague'

// H2H League
export type H2hLeague = {
last_updated_data: string | null
league: H2hLeagueMeta
new_entries: NewEntries
standings: H2hLeagueStandings
}

export type H2hLeagueMeta = LeagueMeta & {
ko_rounds: number | null
}

export type H2hLeagueStandings = {
has_next: boolean
page: number
results: H2hLeagueStanding[]
}

export type H2hLeagueStanding = Standing & {
division: number
matches_played: number
matches_won: number
matches_drawn: number
matches_lost: number
points_for: number
}
63 changes: 63 additions & 0 deletions src/resolvers/classicLeague.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
ClassicLeagueResolvers,
ClassicLeagueMetaResolvers,
ClassicLeagueStandingsResolvers,
ClassicLeagueStandingResolvers,
NewEntriesResolvers,
NewEntryResolvers
} from '@/types/schema'

export const ClassicLeague: ClassicLeagueResolvers = {
last_updated_data: ({ last_updated_data }) => last_updated_data,
league: ({ league }) => league,
new_entries: ({ new_entries }) => new_entries,
standings: ({ standings }) => standings
}

export const ClassicLeagueMeta: ClassicLeagueMetaResolvers = {
admin_entry: ({ admin_entry }) => admin_entry,
closed: ({ closed }) => closed,
code_privacy: ({ code_privacy }) => code_privacy,
created: ({ created }) => created,
cup_league: ({ cup_league }) => cup_league,
has_cup: ({ has_cup }) => has_cup,
id: ({ id }) => id,
league_type: ({ league_type }) => league_type,
max_entries: ({ max_entries }) => max_entries,
name: ({ name }) => name,
rank: ({ rank }) => rank,
scoring: ({ scoring }) => scoring,
start_event: ({ start_event }) => start_event
}

export const ClassicLeagueStandings: ClassicLeagueStandingsResolvers = {
has_next: ({ has_next }) => has_next,
page: ({ page }) => page,
results: ({ results }) => results
}

export const ClassicLeagueStanding: ClassicLeagueStandingResolvers = {
entry: ({ entry }) => entry,
entry_name: ({ entry_name }) => entry_name,
event_total: ({ event_total }) => event_total,
id: ({ id }) => id,
last_rank: ({ last_rank }) => last_rank,
player_name: ({ player_name }) => player_name,
rank: ({ rank }) => rank,
rank_sort: ({ rank_sort }) => rank_sort,
total: ({ total }) => total
}

export const NewEntries: NewEntriesResolvers = {
has_next: ({ has_next }) => has_next,
page: ({ page }) => page,
results: ({ results }) => results
}

export const NewEntry: NewEntryResolvers = {
entry: ({ entry }) => entry,
entry_name: ({ entry_name }) => entry_name,
joined_time: ({ joined_time }) => joined_time,
player_first_name: ({ player_first_name }) => player_first_name,
player_last_name: ({ player_last_name }) => player_last_name
}
52 changes: 52 additions & 0 deletions src/resolvers/h2hLeague.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import {
H2hLeagueResolvers,
H2hLeagueMetaResolvers,
H2hLeagueStandingsResolvers,
H2hLeagueStandingResolvers
} from '@/types/schema'

export const H2hLeague: H2hLeagueResolvers = {
last_updated_data: ({ last_updated_data }) => last_updated_data,
league: ({ league }) => league,
new_entries: ({ new_entries }) => new_entries,
standings: ({ standings }) => standings
}

export const H2hLeagueMeta: H2hLeagueMetaResolvers = {
admin_entry: ({ admin_entry }) => admin_entry,
closed: ({ closed }) => closed,
code_privacy: ({ code_privacy }) => code_privacy,
created: ({ created }) => created,
cup_league: ({ cup_league }) => cup_league,
has_cup: ({ has_cup }) => has_cup,
id: ({ id }) => id,
league_type: ({ league_type }) => league_type,
ko_rounds: ({ ko_rounds }) => ko_rounds,
max_entries: ({ max_entries }) => max_entries,
name: ({ name }) => name,
scoring: ({ scoring }) => scoring,
start_event: ({ start_event }) => start_event
}

export const H2hLeagueStandings: H2hLeagueStandingsResolvers = {
has_next: ({ has_next }) => has_next,
page: ({ page }) => page,
results: ({ results }) => results
}

export const H2hLeagueStanding: H2hLeagueStandingResolvers = {
division: ({ division }) => division,
entry: ({ entry }) => entry,
entry_name: ({ entry_name }) => entry_name,
id: ({ id }) => id,
last_rank: ({ last_rank }) => last_rank,
matches_drawn: ({ matches_drawn }) => matches_drawn,
matches_lost: ({ matches_lost }) => matches_lost,
matches_played: ({ matches_played }) => matches_played,
matches_won: ({ matches_won }) => matches_won,
player_name: ({ player_name }) => player_name,
points_for: ({ points_for }) => points_for,
rank: ({ rank }) => rank,
rank_sort: ({ rank_sort }) => rank_sort,
total: ({ total }) => total
}
6 changes: 5 additions & 1 deletion src/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { PastGameweek } from './pastGameweek'
import { ManagerHistory } from './managerHistory'
import { ManagerHistoryChips } from './managerHistoryChips'
import { ManagerHistoryPastSeason } from './managerHistoryPastSeason'
import * as ClassicLeague from './classicLeague'
import * as H2hLeague from './h2hLeague'

export default {
Query,
Expand All @@ -35,5 +37,7 @@ export default {
PastGameweek,
ManagerHistory,
ManagerHistoryChips,
ManagerHistoryPastSeason
ManagerHistoryPastSeason,
...ClassicLeague,
...H2hLeague
}
8 changes: 8 additions & 0 deletions src/resolvers/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,13 @@ export const Query: QueryResolvers = {
manager: async (_: unknown, { id }: { id: number }) => {
const manager = await fetchController.getManager(id)
return manager
},
classic_league: async (_: unknown, { id }: { id: number }) => {
const league = await fetchController.getClassicLeague(id)
return league
},
h2h_league: async (_: unknown, { id }: { id: number }) => {
const league = await fetchController.getH2hLeague(id)
return league
}
}
Loading

0 comments on commit 10ab00d

Please sign in to comment.