-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolvers.js
190 lines (186 loc) · 4.76 KB
/
resolvers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import LeagueModel from './src/models/League.js'
import TeamModel from './src/models/Team.js'
import PlayerModel from './src/models/Player.js'
import CoachModel from './src/models/Coach.js'
import fetchLeague from './src/controllers/fetchLeague.js'
import fetchTeams from './src/controllers/fetchTeams.js'
import fetchPlayers from './src/controllers/fetchPlayers.js'
import { UserInputError } from 'apollo-server-errors'
const resolvers = {
Query: {
getAllLeagues: async () => {
const leagues = await LeagueModel.find()
return leagues
},
getAllTeams: async () => {
const teams = await TeamModel.find()
return teams
},
getAllPlayers: async () => {
const players = await PlayerModel.find()
return players
},
getAllCoaches: async () => {
const coaches = await CoachModel.find()
return coaches
},
getLeague: async (_, args) => {
const league = await LeagueModel.findById(args.id)
return league
},
getTeam: async (_, args) => {
const team = await TeamModel.findById(args.id)
return team
},
getPlayer: async (_, args) => {
const player = await PlayerModel.findById(args.id)
return player
},
getCoach: async (_, args) => {
const coach = await CoachModel.findById(args.id)
return coach
}
},
Mutation: {
async createLeague (_, args) {
const { leagueCode, leagueName, leagueAreaName } = args.league
const newLeague = new LeagueModel({
leagueCode,
leagueName,
leagueAreaName
})
await newLeague.save()
return newLeague
},
createTeam: async (_, args) => {
const {
teamTla,
teamShortName,
teamAreaName,
teamAddress,
teamLeague,
teamSquad,
coach
} = args.team
const newTeam = new TeamModel({
teamTla,
teamShortName,
teamAreaName,
teamAddress,
teamLeague,
teamSquad,
coach
})
await newTeam.save()
return newTeam
},
createPlayer: async (_, args) => {
const {
playerName,
playerPosition,
playerDateOfBirth,
playerNationality,
playerTeam
} = args.player
const newPlayer = new PlayerModel({
playerName,
playerPosition,
playerDateOfBirth,
playerNationality,
playerTeam
})
await newPlayer.save()
return newPlayer
},
createCoach: async (_, args) => {
const {
coachName,
coachDateOfBirth,
coachNationality,
coachTeam
} = args.coach
const newCoach = new CoachModel({
coachName,
coachDateOfBirth,
coachNationality,
coachTeam
})
await newCoach.save()
return newCoach
},
async deleteLeague (_, { id }) {
await LeagueModel.findByIdAndDelete(id)
return 'League deleted'
},
async deleteTeam (_, { id }) {
await TeamModel.findByIdAndDelete(id)
return 'Team deleted'
},
async deletePlayer (_, { id }) {
await PlayerModel.findByIdAndDelete(id)
return 'Player deleted'
},
async deleteCoach (_, { id }) {
await CoachModel.findByIdAndDelete(id)
return 'Coach deleted'
},
async updateLeague (_, { league, id }) {
const updatedLeague = await LeagueModel.findByIdAndUpdate(
id,
{
$set: league
},
{ new: true }
)
return updatedLeague
},
async updateTeam (_, { team, id }) {
const updatedTeam = await TeamModel.findByIdAndUpdate(
id,
{
$set: team
},
{ new: true }
)
return updatedTeam
},
async updatePlayer (_, { player, id }) {
const updatedPlayer = await PlayerModel.findByIdAndUpdate(
id,
{
$set: player
},
{ new: true }
)
return updatedPlayer
},
async updateCoach (_, { coach, id }) {
const updatedCoach = await CoachModel.findByIdAndUpdate(
id,
{
$set: coach
},
{ new: true }
)
return updatedCoach
},
async importLeague (_, args) {
const { leagueCode } = args.league
const leagueAvailable = await LeagueModel.find({ leagueCode })
if (leagueAvailable.length === 0) {
fetchLeague(leagueCode)
console.log('Fetch League')
await fetchTeams(leagueCode)
console.log('Fetch Teams')
await fetchPlayers(leagueCode)
console.log('Fetch Players & Coaches')
const newLeague = new LeagueModel({ leagueCode })
return newLeague
}
throw new UserInputError(
'Sorry, that league already exists in our records.'
)
}
}
}
export default resolvers