generated from idea2app/REST-Node-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Announcement.ts
133 lines (115 loc) · 3.31 KB
/
Announcement.ts
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
import {
Authorized,
Body,
CurrentUser,
Delete,
Get,
HttpCode,
JsonController,
NotFoundError,
OnNull,
OnUndefined,
Param,
Patch,
Post,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';
import {
Announcement,
AnnouncementListChunk,
BaseFilter,
dataSource,
Hackathon,
User
} from '../model';
import { searchConditionOf } from '../utility';
import { ActivityLogController } from './ActivityLog';
import { HackathonController } from './Hackathon';
const store = dataSource.getRepository(Announcement),
hackathonStore = dataSource.getRepository(Hackathon);
@JsonController('/hackathon/:name/announcement')
export class AnnouncementController {
@Post()
@Authorized()
@HttpCode(201)
@ResponseSchema(Announcement)
async createOne(
@CurrentUser() createdBy: User,
@Param('name') name: string,
@Body() announcement: Announcement
) {
const hackathon = await hackathonStore.findOneBy({ name });
if (!hackathon) throw new NotFoundError();
await HackathonController.ensureAdmin(createdBy.id, name);
const saved = await store.save({
...announcement,
hackathon,
createdBy
});
await ActivityLogController.logCreate(
createdBy,
'Announcement',
saved.id
);
return saved;
}
@Patch('/:id')
@Authorized()
@ResponseSchema(Announcement)
async updateOne(
@CurrentUser() updatedBy: User,
@Param('name') name: string,
@Param('id') id: number,
@Body() newData: Announcement
) {
const old = await store.findOneBy({ id });
if (!old) throw new NotFoundError();
await HackathonController.ensureAdmin(updatedBy.id, name);
const saved = await store.save({ ...old, ...newData, updatedBy });
await ActivityLogController.logUpdate(updatedBy, 'Announcement', id);
return saved;
}
@Delete('/:id')
@Authorized()
@OnUndefined(204)
async deleteOne(
@CurrentUser() deletedBy: User,
@Param('name') name: string,
@Param('id') id: number
) {
const old = await store.findOne({
where: { id },
relations: ['hackathon']
});
if (!old) throw new NotFoundError();
await HackathonController.ensureAdmin(deletedBy.id, name);
await store.save({ ...old, deletedBy });
await store.softDelete(id);
await ActivityLogController.logDelete(deletedBy, 'Announcement', id);
}
@Get('/:id')
@OnNull(404)
@ResponseSchema(Announcement)
getOne(@Param('id') id: number) {
return store.findOneBy({ id });
}
@Get()
@ResponseSchema(AnnouncementListChunk)
async getList(
@Param('name') name: string,
@QueryParams() { keywords, pageSize, pageIndex }: BaseFilter
) {
const where = searchConditionOf<Announcement>(
['title', 'content'],
keywords,
{ hackathon: { name } }
);
const [list, count] = await store.findAndCount({
where,
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}