-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathposts.js
51 lines (42 loc) · 984 Bytes
/
posts.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
45
46
47
48
49
50
51
class Post {
constructor(title, createdAt) {
this._title = title;
this._createdAt = createdAt;
}
getTitle() {
return this._title;
}
getCreatedAt() {
return this._createdAt;
}
}
class Posts {
constructor(mongo) {
this.db = mongo.db("app");
this.postsCollection = this.db.collection("posts");
}
async all(search) {
const filter = {};
if (search) {
// There's a vulnerability here, which can be abused for demo purposes
filter.title = search;
}
const posts = await this.postsCollection.find(filter).toArray();
return posts.map((post) => new Post(post.title, post.createdAt));
}
async persist(post) {
await this.postsCollection.insertOne({
title: post.getTitle(),
createdAt: post.getCreatedAt(),
});
}
async where(title) {
return await this.postsCollection
.find({ $where: `this.title === '${title}'` })
.toArray();
}
}
module.exports = {
Posts,
Post,
};