-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.bal
306 lines (258 loc) · 8.64 KB
/
types.bal
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import movie_rating_system.datasource;
import ballerina/constraint;
import ballerina/graphql;
import ballerina/graphql.dataloader;
const string DIRECTOR_LOADER = "directorLoader";
const string MOVIE_LOADER = "movieLoader";
# Represents a Movie in the movie database.
@display {
label: "Movie",
id: "movie"
}
isolated service class Movie {
private final string id;
private final string title;
private final int year;
private final string description;
private final int score;
private final int reviewers;
private final string directorId;
isolated function init(MovieRecord movieRecord) {
self.id = movieRecord.id;
self.title = movieRecord.title;
self.year = movieRecord.year;
self.description = movieRecord.description;
self.score = movieRecord.score;
self.reviewers = movieRecord.reviewers;
self.directorId = movieRecord.directorId;
}
# The ID of the movie.
# + return - ID of the movie.
isolated resource function get id() returns @graphql:ID string => self.id;
# The title of the movie.
# + return - Title of the movie.
isolated resource function get title() returns string => self.title;
# The year the movie was released.
# + return - Year the movie was released.
isolated resource function get year() returns int => self.year;
# The description of the movie.
# + return - Description of the movie.
isolated resource function get description() returns string => self.description;
# The average rating of the movie.
# + return - Average rating of the movie.
isolated resource function get rating() returns float? {
if self.score == 0 || self.reviewers == 0 {
return 0.0;
}
return (<float>self.score / <float>self.reviewers).round(2);
}
isolated function preDirector(graphql:Context context) {
dataloader:DataLoader directorLoader = context.getDataLoader(DIRECTOR_LOADER);
directorLoader.add(self.directorId);
}
# The director of the movie.
# + return - Director of the movie.
isolated resource function get director(graphql:Context context) returns Director|error? {
dataloader:DataLoader directorLoader = context.getDataLoader(DIRECTOR_LOADER);
DirectorRecord directorRecord = check directorLoader.get(self.directorId);
return new (directorRecord);
}
}
# Represents a Director in the movie database.
@display {
label: "Director",
id: "director"
}
public isolated service class Director {
private final string id;
private final string name;
private final string bio;
isolated function init(DirectorRecord directorRecord) {
self.id = directorRecord.id;
self.name = directorRecord.name;
self.bio = directorRecord.bio;
}
# The ID of the director.
# + return - ID of the director.
isolated resource function get id() returns @graphql:ID string => self.id;
# The name of the director.
# + return - Name of the director.
isolated resource function get name() returns string => self.name;
# The biography of the director.
# + return - Biography of the director.
isolated resource function get bio() returns string => self.bio;
isolated function preMovies(graphql:Context context) {
dataloader:DataLoader movieLoader = context.getDataLoader(MOVIE_LOADER);
movieLoader.add(self.id);
}
# The movies directed by the director.
# + return - Movies directed by the director.
isolated resource function get movies(graphql:Context context) returns Movie[]|error? {
dataloader:DataLoader movieLoader = context.getDataLoader(MOVIE_LOADER);
MovieRecord[] movieRecords = check movieLoader.get(self.id);
return from MovieRecord movieRecord in movieRecords
select new (movieRecord);
}
}
# Represents a User in the movie database.
@display {
label: "User",
id: "user"
}
public isolated service class User {
private final string id;
private final string name;
private final string email;
isolated function init(UserRecord userRecord) {
self.id = userRecord.id;
self.name = userRecord.name;
self.email = userRecord.email;
}
# The ID of the user.
# + return - ID of the user.
isolated resource function get id() returns @graphql:ID string => self.id;
# The name of the user.
# + return - Name of the user.
isolated resource function get name() returns string? => self.name;
# The email of the user.
# + return - Email of the user.
@graphql:ResourceConfig {
cacheConfig: {
enabled: false
},
interceptors: new AdminAuthInterceptor()
}
isolated resource function get email(graphql:Context context) returns string? => self.email;
}
# Represents a Review in the movie database.
@display {
label: "Review",
id: "review"
}
public isolated service class Review {
private final string id;
private final string userId;
private final string movieId;
private final int score;
private final string description;
isolated function init(ReviewRecord reviewRecord) {
self.id = reviewRecord.id;
self.userId = reviewRecord.userId;
self.movieId = reviewRecord.movieId;
self.score = reviewRecord.score;
self.description = reviewRecord.description;
}
# The ID of the review.
# + return - ID of the review.
isolated resource function get id() returns @graphql:ID string => self.id;
# The user who wrote the review.
# + return - User who wrote the review.
isolated resource function get user(graphql:Context context) returns User|error? {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
UserRecord userRecord = check datasource->getUser(self.userId);
return new (userRecord);
}
# The movie that was reviewed.
# + return - Movie that was reviewed.
isolated resource function get movie(graphql:Context context) returns Movie|error? {
datasource:Datasource datasource = check context.get(DATASOURCE).ensureType();
MovieRecord movieRecord = check datasource->getMovieById(self.movieId);
return new (movieRecord);
}
# The score given by the user.
# + return - Score given by the user.
isolated resource function get score() returns int => self.score;
# The description of the review.
# + return - Description of the review.
isolated resource function get description() returns string => self.description;
}
type MovieRecord record {|
readonly string id;
string title;
int year;
string description;
int score = 0; // Total review points
int reviewers = 0;
readonly string directorId;
|};
type DirectorRecord record {|
readonly string id;
string name;
string bio;
|};
type UserRecord record {|
readonly string id;
string name;
string email;
string[] roles;
|};
type ReviewRecord record {|
readonly string id;
readonly string userId;
readonly string movieId;
int score;
string description;
|};
# Input type for adding a Movie Review.
@display {
label: "Review Input",
id: "review-input"
}
public type ReviewInput record {|
# ID of the movie
@graphql:ID
string movieId;
# Score given by the user. Should be between 1 and 5.
@constraint:Int {
minValue: 1,
maxValue: 5
}
int score;
# Description of the review.
string description;
|};
# Input type for adding a Movie.
@display {
label: "Movie Input",
id: "movie-input"
}
public type MovieInput record {|
# Title of the movie
string title;
# Year the movie was released
int year;
# Description of the movie
string description;
# ID of the director
@graphql:ID
string directorId;
|};
# Input type for adding a Director.
@display {
label: "Director Input",
id: "director-input"
}
public type DirectorInput record {|
# Name of the director
string name;
# Biography of the director
string bio;
|};
type MoviesOfDirector record {|
string _id;
MovieRecord[] movies;
|};
readonly service class AdminAuthInterceptor {
*graphql:Interceptor;
isolated remote function execute(graphql:Context context, graphql:Field 'field) returns anydata|error {
check validateUserRole(context, "admin");
return context.resolve('field);
}
}
readonly service class UserAuthInterceptor {
*graphql:Interceptor;
isolated remote function execute(graphql:Context context, graphql:Field 'field) returns anydata|error {
check validateUserRole(context, "user");
return context.resolve('field);
}
}