-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.db.js
44 lines (35 loc) · 1.17 KB
/
post.db.js
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
/* eslint-disable no-underscore-dangle */
const { getSearchQuery } = require('./postQueryBuilder');
const { errorFactory, errors } = require('../utils/errorManager');
module.exports = class PostDb {
constructor(elasticApi) {
this.elasticApi = elasticApi;
}
async getPostById(postId) {
const result = await this.elasticApi.getById(postId);
return result;
}
async getPosts(postFilter) {
const result = await this.elasticApi.search(getSearchQuery(postFilter));
return result.hits.hits.map((hit) => hit._source);
}
async createPost(post) {
const postWithJoinField = { ...post, type: 'post', postCommentJoin: 'post' };
await this.elasticApi.index(
postWithJoinField.postId,
postWithJoinField,
);
}
async addPostLike(postId, userId) {
const succeeded = await this.elasticApi.addLike(postId, userId);
if (!succeeded) {
throw errorFactory(errors.alreadyLiked, 'Post already liked by the user');
}
}
async deletePostLike(postId, userId) {
const succeeded = await this.elasticApi.removeLike(postId, userId);
if (!succeeded) {
throw errorFactory(errors.notLiked, 'Post not liked by the user');
}
}
};