-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathposts.js
58 lines (50 loc) · 1.05 KB
/
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
52
53
54
55
56
57
58
const { Posts, Users, Comments } = require('../db/models')
async function createNewPost(userId, title, body) {
const post = await Posts.create({
title,
body,
userId,
})
return post
}
/**
* showAllPosts({username: ''})
* showAllPosts({title: ''})
*/
async function findAllPosts(query) {
let where = {}
if (query.userId) { where.userId = query.userId }
const posts = await Posts.findAll({
include: [Users, Comments],
where
})
return posts
}
module.exports = {
createNewPost,
findAllPosts
}
/* Test Code */
/*
async function task() {
// console.log(
// await createNewPost(
// 1,
// 'This is a sample post',
// 'Body of the post goes here'
// )
// ),
// console.log(
// await createNewPost(
// 2,
// 'Another sample post',
// 'Some body example here as well'
// )
// )
const posts = await showAllPosts()
for (let p of posts) {
console.log(`${p.title}\nauthor: ${p.user.username}\n${p.body}\n==========\n`)
}
}
task()
*/