-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
95 lines (85 loc) · 2.52 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
const cors = require('cors')
const mysql = require('mysql')
const express = require('express')
const body_parser = require('body-parser')
const { mysql_secrets } = require('./secrets')
const server = express()
const router = express.Router()
const db = mysql.createConnection(mysql_secrets)
server.use(cors())
server.use(body_parser.json())
server.use('/api', router)
router.get('/bookmarks', (req, res) => {
const { user_id } = req.query
const query = `
select * from bookmarks where user = ?;
select * from tags where user = ?;
`
db.query(query, [user_id, user_id], (err, results) => {
if (err) return res.status(500).send(err)
return res.send(results)
})
})
router.get('/bookmarks/:uid', (req, res) => {
const { uid } = req.params
const query = `
select * from bookmarks where uid = ?;
select * from tags where uid = ?;
`
db.query(query, [uid, uid], (err, result) => {
if (err) return res.status(500).send(err)
return res.send(result)
})
})
router.get('/tags', (req, res) => {
const { user_id } = req.query
let query = 'select distinct tag from tags where user = ?'
db.query(query, [user_id], (err, result) => {
if (err) return res.status(500).send(err)
return res.send(result)
})
})
router.post('/bookmarks', (req, res) => {
let { uid, user, title, url, tags } = req.body
let query = `
insert into bookmarks (uid, user, title, url) values (?, ?, ?, ?);
`
if (tags.length) {
query += 'insert into tags (uid, user, tag) values'
tags.forEach(tag => (query += `("${uid}", "${user}", "${tag}"),`))
query = query.replace(/.$/, ';')
}
db.query(query, [uid, user, title, url], (err, results) => {
if (err) return res.status(500).send(err)
return res.send(results)
})
})
router.put('/bookmarks/:uid', (req, res) => {
let { url, tags, title, user } = req.body
let { uid } = req.params
let query = `
update bookmarks set title = ?, url = ? where uid = ?;
delete from tags where uid = ?;
`
if (tags.length) {
query += 'insert into tags (uid, user, tag) values'
tags.forEach(tag => (query += `("${uid}", "${user}", "${tag}"),`))
query = query.replace(/.$/, ';')
}
db.query(query, [title, url, uid, uid], (err, result) => {
if (err) return res.status(500).send(err)
return res.send(result)
})
})
router.delete('/bookmarks/:uid', (req, res) => {
const { uid } = req.params
const sql = `
delete from bookmarks where uid = ?;
delete from tags where uid = ?;
`
db.query(sql, [uid, uid], (err, result) => {
if (err) return res.status(500).send(err)
return res.send(result)
})
})
server.listen(7779)