Skip to content

Commit 14b836d

Browse files
committed
[optimize] simplify DB Store accessing
1 parent abe8076 commit 14b836d

File tree

15 files changed

+85
-204
lines changed

15 files changed

+85
-204
lines changed

source/controller/Announcement.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Get,
77
HttpCode,
88
JsonController,
9-
NotFoundError,
109
OnNull,
1110
OnUndefined,
1211
Param,
@@ -16,19 +15,10 @@ import {
1615
} from 'routing-controllers';
1716
import { ResponseSchema } from 'routing-controllers-openapi';
1817

19-
import {
20-
Announcement,
21-
AnnouncementListChunk,
22-
BaseFilter,
23-
dataSource,
24-
Hackathon,
25-
User
26-
} from '../model';
18+
import { Announcement, AnnouncementListChunk, BaseFilter, User } from '../model';
2719
import { hackathonService, UserServiceWithLog } from '../service';
2820
import { searchConditionOf } from '../utility';
2921

30-
const hackathonStore = dataSource.getRepository(Hackathon);
31-
3222
@JsonController('/hackathon/:name/announcement')
3323
export class AnnouncementController {
3424
service = new UserServiceWithLog(Announcement, ['title', 'content']);
@@ -42,11 +32,7 @@ export class AnnouncementController {
4232
@Param('name') name: string,
4333
@Body() announcement: Announcement
4434
) {
45-
const hackathon = await hackathonStore.findOneBy({ name });
46-
47-
if (!hackathon) throw new NotFoundError();
48-
49-
await hackathonService.ensureAdmin(createdBy.id, name);
35+
const hackathon = await hackathonService.ensureAdmin(createdBy.id, name);
5036

5137
return this.service.createOne({ ...announcement, hackathon }, createdBy);
5238
}

source/controller/Award.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,10 @@ import {
2222
AwardAssignmentListChunk,
2323
AwardListChunk,
2424
BaseFilter,
25-
dataSource,
26-
Hackathon,
2725
User
2826
} from '../model';
2927
import { awardAssignmentService, awardService, hackathonService } from '../service';
3028

31-
const hackathonStore = dataSource.getRepository(Hackathon);
32-
3329
@JsonController('/hackathon/:name/award')
3430
export class AwardController {
3531
service = awardService;
@@ -43,11 +39,7 @@ export class AwardController {
4339
@Param('name') name: string,
4440
@Body() award: Award
4541
) {
46-
const hackathon = await hackathonStore.findOneBy({ name });
47-
48-
if (!hackathon) throw new NotFoundError(`Hackathon ${name} is not found`);
49-
50-
await hackathonService.ensureAdmin(createdBy.id, name);
42+
const hackathon = await hackathonService.ensureAdmin(createdBy.id, name);
5143

5244
return this.service.createOne({ ...award, hackathon }, createdBy);
5345
}
@@ -100,7 +92,6 @@ export class AwardController {
10092
@JsonController('/hackathon/:name/award/:aid/assignment')
10193
export class AwardAssignmentController {
10294
service = awardAssignmentService;
103-
awardService = awardService;
10495

10596
@Post()
10697
@Authorized()
@@ -112,7 +103,8 @@ export class AwardAssignmentController {
112103
@Param('aid') aid: number,
113104
@Body() assignment: AwardAssignment
114105
) {
115-
const award = await this.awardService.getOne(aid, ['hackathon']);
106+
const award = await awardService.getOne(aid, ['hackathon']);
107+
116108
if (!award) throw new NotFoundError(`Award "${aid}" is not found`);
117109

118110
await hackathonService.ensureAdmin(currentUser.id, name);
@@ -140,7 +132,7 @@ export class AwardAssignmentController {
140132
@Get()
141133
@ResponseSchema(AwardAssignmentListChunk)
142134
getList(@Param('aid') aid: number, @QueryParams() { pageSize, pageIndex }: BaseFilter) {
143-
return awardAssignmentService.getListByDimension('award', aid, pageSize, pageIndex);
135+
return this.service.getListByDimension('award', aid, pageSize, pageIndex);
144136
}
145137
}
146138

source/controller/Enrollment.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,15 @@ import {
1515
import { ResponseSchema } from 'routing-controllers-openapi';
1616

1717
import {
18-
dataSource,
1918
Enrollment,
2019
EnrollmentFilter,
2120
EnrollmentListChunk,
2221
EnrollmentStatus,
23-
Hackathon,
2422
User
2523
} from '../model';
2624
import { enrollmentService, hackathonService } from '../service';
2725
import { searchConditionOf } from '../utility';
2826

29-
const hackathonStore = dataSource.getRepository(Hackathon);
30-
3127
@JsonController('/hackathon/:name/enrollment')
3228
export class EnrollmentController {
3329
service = enrollmentService;
@@ -64,7 +60,7 @@ export class EnrollmentController {
6460
@Param('name') name: string,
6561
@Body() { form }: Enrollment
6662
) {
67-
const hackathon = await hackathonStore.findOneBy({ name }),
63+
const hackathon = await hackathonService.store.findOneBy({ name }),
6864
now = Date.now();
6965

7066
if (!hackathon) throw new NotFoundError();

source/controller/Evaluation.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,10 @@ import {
1414
import { ResponseSchema } from 'routing-controllers-openapi';
1515
import { groupBy, sum } from 'web-utility';
1616

17-
import {
18-
BaseFilter,
19-
dataSource,
20-
Evaluation,
21-
EvaluationListChunk,
22-
Score,
23-
Team,
24-
User
25-
} from '../model';
26-
import { UserServiceWithLog } from '../service';
17+
import { BaseFilter, Evaluation, EvaluationListChunk, Score, User } from '../model';
18+
import { teamService, UserServiceWithLog } from '../service';
2719
import { searchConditionOf } from '../utility';
2820

29-
const teamStore = dataSource.getRepository(Team);
30-
3121
@JsonController('/hackathon/:name/team/:tid/evaluation')
3222
export class EvaluationController {
3323
service = new UserServiceWithLog(Evaluation, ['scores', 'comment']);
@@ -38,11 +28,10 @@ export class EvaluationController {
3828
@ResponseSchema(Evaluation)
3929
async createOne(
4030
@CurrentUser() createdBy: User,
41-
@Param('name') name: string,
4231
@Param('tid') tid: number,
4332
@Body() evaluation: Evaluation
4433
) {
45-
const team = await teamStore.findOne({
34+
const team = await teamService.store.findOne({
4635
where: { id: tid },
4736
relations: ['hackathon']
4837
});
@@ -57,7 +46,6 @@ export class EvaluationController {
5746
{ ...evaluation, team, hackathon: team.hackathon },
5847
createdBy
5948
);
60-
6149
const allScores = (await this.service.store.findBy({ team: { id: tid } }))
6250
.map(({ scores }) => scores)
6351
.flat();
@@ -71,7 +59,7 @@ export class EvaluationController {
7159
);
7260
const score = sum(...scores.map(({ score }) => score));
7361

74-
await teamStore.save({ ...team, scores, score });
62+
await teamService.store.save({ ...team, scores, score });
7563

7664
return saved;
7765
}

source/controller/GitTemplate.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { RepositoryModel } from 'mobx-github';
21
import {
32
Authorized,
43
Body,
@@ -7,7 +6,6 @@ import {
76
Get,
87
HttpCode,
98
JsonController,
10-
NotFoundError,
119
OnNull,
1210
OnUndefined,
1311
Param,
@@ -16,19 +14,10 @@ import {
1614
} from 'routing-controllers';
1715
import { ResponseSchema } from 'routing-controllers-openapi';
1816

19-
import {
20-
BaseFilter,
21-
dataSource,
22-
GitTemplate,
23-
GitTemplateListChunk,
24-
Hackathon,
25-
User
26-
} from '../model';
17+
import { BaseFilter, GitTemplate, GitTemplateListChunk, User } from '../model';
2718
import { gitTemplateService, hackathonService } from '../service';
2819
import { searchConditionOf } from '../utility';
2920

30-
const hackathonStore = dataSource.getRepository(Hackathon);
31-
3221
@JsonController('/hackathon/:name/git-template')
3322
export class GitTemplateController {
3423
service = gitTemplateService;
@@ -42,11 +31,7 @@ export class GitTemplateController {
4231
@Param('name') name: string,
4332
@Body() { html_url }: GitTemplate
4433
) {
45-
const hackathon = await hackathonStore.findOneBy({ name });
46-
47-
if (!hackathon) throw new NotFoundError();
48-
49-
await hackathonService.ensureAdmin(createdBy.id, name);
34+
const hackathon = await hackathonService.ensureAdmin(createdBy.id, name);
5035

5136
const repository = await gitTemplateService.getRepository(html_url);
5237

source/controller/Organizer.ts

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
Get,
77
HttpCode,
88
JsonController,
9-
NotFoundError,
109
OnUndefined,
1110
Param,
1211
Post,
@@ -15,21 +14,13 @@ import {
1514
} from 'routing-controllers';
1615
import { ResponseSchema } from 'routing-controllers-openapi';
1716

18-
import {
19-
dataSource,
20-
Hackathon,
21-
Organizer,
22-
OrganizerFilter,
23-
OrganizerListChunk,
24-
User
25-
} from '../model';
17+
import { Organizer, OrganizerFilter, OrganizerListChunk, User } from '../model';
2618
import { hackathonService, UserServiceWithLog } from '../service';
2719
import { searchConditionOf } from '../utility';
2820

2921
@JsonController('/hackathon/:name/organizer')
3022
export class OrganizerController {
3123
service = new UserServiceWithLog(Organizer, ['name', 'description', 'url']);
32-
hackathonStore = dataSource.getRepository(Hackathon);
3324

3425
@Post()
3526
@Authorized()
@@ -40,13 +31,7 @@ export class OrganizerController {
4031
@Param('name') name: string,
4132
@Body() organizer: Organizer
4233
) {
43-
const hackathon = await this.hackathonStore.findOne({
44-
where: { name },
45-
relations: ['createdBy']
46-
});
47-
if (!hackathon) throw new NotFoundError();
48-
49-
await hackathonService.ensureAdmin(createdBy.id, name);
34+
const hackathon = await hackathonService.ensureAdmin(createdBy.id, name);
5035

5136
return this.service.createOne({ ...organizer, hackathon }, createdBy);
5237
}

source/controller/PlatformAdmin.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,14 @@ import {
1414
} from 'routing-controllers';
1515
import { ResponseSchema } from 'routing-controllers-openapi';
1616

17-
import {
18-
BaseFilter,
19-
dataSource,
20-
PlatformAdmin,
21-
PlatformAdminListChunk,
22-
Role,
23-
User
24-
} from '../model';
25-
import { platformAdminService } from '../service';
17+
import { BaseFilter, PlatformAdmin, PlatformAdminListChunk, Role, User } from '../model';
18+
import { platformAdminService, sessionService } from '../service';
2619
import { searchConditionOf } from '../utility';
2720

28-
const userStore = dataSource.getRepository(User);
29-
3021
@JsonController('/platform/admin')
3122
export class PlatformAdminController {
3223
service = platformAdminService;
24+
userStore = sessionService.userStore;
3325

3426
@Put('/:uid')
3527
@Authorized(Role.Administrator)
@@ -40,7 +32,7 @@ export class PlatformAdminController {
4032
@Param('uid') uid: number,
4133
@Body() { description }: PlatformAdmin
4234
) {
43-
const user = await userStore.findOneBy({ id: uid });
35+
const user = await this.userStore.findOneBy({ id: uid });
4436

4537
if (!user) throw new NotFoundError();
4638

@@ -50,7 +42,7 @@ export class PlatformAdminController {
5042

5143
user.roles.push(Role.Administrator);
5244

53-
await userStore.save(user);
45+
await this.userStore.save(user);
5446

5547
return this.service.createOne({ user, description }, createdBy);
5648
}
@@ -59,7 +51,7 @@ export class PlatformAdminController {
5951
@Authorized(Role.Administrator)
6052
@OnUndefined(204)
6153
async deleteOne(@CurrentUser() deletedBy: User, @Param('uid') uid: number) {
62-
const user = await userStore.findOneBy({ id: uid });
54+
const user = await this.userStore.findOneBy({ id: uid });
6355

6456
if (!user) throw new NotFoundError();
6557

@@ -69,7 +61,7 @@ export class PlatformAdminController {
6961

7062
user.roles = user.roles.filter(role => role !== Role.Administrator);
7163

72-
await userStore.save(user);
64+
await this.userStore.save(user);
7365

7466
await this.service.deleteOne(admin.id, deletedBy);
7567
}

source/controller/Staff.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,16 @@ import {
1515
} from 'routing-controllers';
1616
import { ResponseSchema } from 'routing-controllers-openapi';
1717

18-
import {
19-
dataSource,
20-
Hackathon,
21-
Staff,
22-
StaffFilter,
23-
StaffListChunk,
24-
StaffType,
25-
User
26-
} from '../model';
27-
import { hackathonService,staffService } from '../service';
18+
import { Staff, StaffFilter, StaffListChunk, StaffType, User } from '../model';
19+
import { hackathonService, sessionService, staffService } from '../service';
2820
import { searchConditionOf } from '../utility';
2921

30-
const userStore = dataSource.getRepository(User),
31-
hackathonStore = dataSource.getRepository(Hackathon);
3222
const StaffTypeRegExp = Object.values(StaffType).join('|');
3323

3424
@JsonController(`/hackathon/:name/:type(${StaffTypeRegExp})`)
3525
export class StaffController {
3626
service = staffService;
27+
userStore = sessionService.userStore;
3728

3829
@Put('/:uid')
3930
@HttpCode(201)
@@ -47,8 +38,8 @@ export class StaffController {
4738
@Body() staff: Staff
4839
) {
4940
const [user, hackathon] = await Promise.all([
50-
userStore.findOneBy({ id: uid }),
51-
hackathonStore.findOneBy({ name })
41+
this.userStore.findOneBy({ id: uid }),
42+
hackathonService.store.findOneBy({ name })
5243
]);
5344
if (!user || !hackathon || !StaffType[type]) throw new NotFoundError();
5445

0 commit comments

Comments
 (0)