-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostService.js
43 lines (38 loc) · 1.3 KB
/
postService.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
var postStorage = (function () {
function Post(title, image) {
this.title = title;
this.image = image;
this.description = "";
this.points = 0;
this.comments = 0;
this.category = "";
this.hashTags = [];
this.owner = null;
}
function PostStorage() {
if (localStorage.getItem('posts') != null) {
this.posts = JSON.parse(localStorage.getItem('posts'));
} else {
this.posts = [];
}
}
PostStorage.prototype.addPost = function (title, image) {
if (!(this.posts.find((post) => post.title == title))) {
var newPost = new Post(title, image);
this.posts.push(newPost);
localStorage.setItem('posts', JSON.stringify(this.posts));
return newPost;
}
}
PostStorage.prototype.removePost = function () {
var index = this.posts.findIndex((post) => post.title == title);
if (index < 0) {
throw new Error('No such object with title ' + title);
} else {
this.posts.splice(index, 1);
localStorage.setItem('posts', JSON.stringify(this.posts));
return this.posts[index].title;
}
}
return new PostStorage();
})();