-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostService.js
40 lines (35 loc) · 1.03 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
import Post from './Post.js'
import fileService from './FileService.js'
class PostService {
async create(post, picture) {
const fileName = fileService.saveFile(picture)
const createdPost = await Post.create({...post, fileName})
return createdPost
}
async getAll() {
const posts = Post.find()
return posts
}
async getOne(id) {
if(!id) {
throw new Error('ID не вказан')
}
const post = await Post.findById(id)
return post
}
async update(post) {
if(!post._id) {
throw new Error('ID не вказан')
}
const updatedPost = await Post.findByIdAndUpdate(post._id, post, {new: true})
return updatedPost
}
async delete(id) {
if(!id) {
throw new Error('ID не вказан')
}
const deletedPost = await Post.findByIdAndDelete(id)
return deletedPost
}
}
export default new PostService()