forked from WebDevSimplified/notion-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (42 loc) · 1.09 KB
/
server.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
require("dotenv").config()
const express = require("express")
const {
getTags,
createSuggestion,
getSuggestions,
upVoteSuggestion,
} = require("./notion")
const app = express()
app.set("views", "./views")
app.set("view engine", "ejs")
app.use(express.static("public"))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
let tags = []
getTags().then(data => {
tags = data
})
setInterval(async () => {
tags = await getTags()
}, 1000 * 60 * 60)
app.get("/", async (req, res) => {
const suggestions = await getSuggestions()
res.render("index", { tags, suggestions })
})
app.post("/create-suggestion", async (req, res) => {
const { title, description, isProject, tagIds = [] } = req.body
await createSuggestion({
title,
description,
isProject: isProject != null,
tags: (Array.isArray(tagIds) ? tagIds : [tagIds]).map(tagId => {
return { id: tagId }
}),
})
res.redirect("/")
})
app.post("/up-vote-suggestion", async (req, res) => {
const votes = await upVoteSuggestion(req.body.suggestionId)
res.json({ votes })
})
app.listen(process.env.PORT)