-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostService.js
36 lines (35 loc) · 1020 Bytes
/
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
import PostModel from "./PostModel.js";
import FileService from "./FileService.js";
class PostService{
async create(post, picture) {
const fileName = FileService.saveFile(picture)
const createPost = await PostModel.create({...post, picture: fileName})
return createPost;
}
async getOne(id) {
if (!id) {
throw new Error('not specified Id')
}
const post = await PostModel.findById(id)
return post;
}
async getAll() {
const post = await PostModel.find()
return post;
}
async update(post) {
if (!post._id) {
throw new Error('not specified Id')
}
const newPost = await PostModel.findByIdAndUpdate(post._id, post, {new:true})
return newPost;
}
async delete(id) {
if (!id) {
throw new Error('not specified Id')
}
const post = await PostModel.findByIdAndDelete(id)
return post;
}
}
export default new PostService()