-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostsController.js
57 lines (57 loc) · 1.3 KB
/
postsController.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
52
53
54
55
56
57
import errorMessages from './errorMessages.js'
import dbQuerys from './dbQuerys.js'
import { pool as db } from './db.js'
export default {
async getPosts() {
try {
let posts = (await db.query(dbQuerys.getPostsQuery)).rows
return posts
} catch {
throw new Error(errorMessages.getPostsError)
}
},
async getPost(postId) {
try {
const posts = await this.getPosts()
const post = posts.find((post) => post.id === String(postId))
return post
} catch {
throw new Error(errorMessages.getPostError)
}
},
async createPost(post) {
try {
db.query(dbQuerys.createPostQuery, [
post.title,
post.text,
post.tags === '' ? [] : post.tags.split(','),
'/images/' + post.image,
])
} catch {
throw new Error(errorMessages.getCreateError)
}
},
async existPostWithId(postId) {
return Boolean(await this.getPost(postId))
},
async removePost(postId) {
try {
db.query(dbQuerys.removePostQuery, [postId])
} catch {
throw new Error(errorMessages.removePostError)
}
},
async editPost(newPostData) {
try {
db.query(dbQuerys.editPostQuery, [
newPostData.title,
newPostData.text,
newPostData.tags === '' ? [] : newPostData.tags.split(','),
newPostData.image,
newPostData.id,
])
} catch {
throw new Error(errorMessages.editPostError)
}
},
}