-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathActivity.ts
67 lines (57 loc) · 1.84 KB
/
Activity.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
import { Object as LCObject, Query } from 'leanengine';
import updateEvents, { Event } from '@fcc-cdc/it-events';
import { JsonController, QueryParam, Get } from 'routing-controllers';
class Activity extends LCObject {}
interface EventData extends Event {
objectId: string;
createdAt: string;
updatedAt: string;
}
export async function update(interval?: number) {
const list = (
await new Query(Activity)
.greaterThanOrEqualTo('start', new Date())
.limit(1000)
.find()
).map(item => item.toJSON() as EventData);
for await (const {
objectId,
createdAt,
updatedAt,
link,
banner,
...rest
} of updateEvents(list, interval)) {
const data = objectId
? LCObject.createWithoutData('Activity', objectId)
: new Activity();
await data.save({ link: link + '', banner: banner + '', ...rest });
}
}
@JsonController('/activity')
export class ActivityController {
@Get()
async search(
@QueryParam('keywords') keywords: string,
@QueryParam('page') page = 1,
@QueryParam('rows') rows = 10,
@QueryParam('from') from: string,
@QueryParam('to') to: string
) {
const query = keywords
? Query.or(
new Query(Activity).contains('title', keywords),
new Query(Activity).contains('address', keywords)
)
: new Query(Activity);
if (from) query.greaterThanOrEqualTo('start', new Date(from));
if (to) query.lessThanOrEqualTo('start', new Date(to));
const list = await query
.addDescending('start')
.addDescending('end')
.limit(rows)
.skip((page - 1) * rows)
.find();
return list.map(item => item.toJSON());
}
}