-
Notifications
You must be signed in to change notification settings - Fork 556
/
Copy pathcomment-service.ts
25 lines (21 loc) · 896 Bytes
/
comment-service.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
import { debug } from 'debug';
import { CacheSingleton } from '../database/cache';
import * as HNDB from '../database/hn-data-api';
import { CommentModel } from '../../src/data/models';
const logger = debug('app:Comment');
logger.log = console.log.bind(console);
export abstract class CommentService {
static getComment(id: number): CommentModel | Promise<CommentModel | void> {
return (
CacheSingleton.getComment(id) ||
HNDB.fetchComment(id).catch((reason) => logger('Rejected comment:', reason))
);
}
static getComments(ids: number[]): Promise<Array<CommentModel> | void> {
return Promise.all(ids.map((commentId) => CommentService.getComment(commentId)))
.then((comments): CommentModel[] =>
comments.filter((comment): comment is CommentModel => comment !== undefined)
)
.catch((reason) => logger('Rejected comments:', reason));
}
}