-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_mariadb.js
128 lines (113 loc) · 3.37 KB
/
app_mariadb.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const Client = require('mariasql');
const c = new Client({
host: '127.0.0.1',
user: 'root',
password: 'dlekdms!@#$',
db: 'o2'
});
const app = express();
app.locals.pretty = true;
//pug (jade)
app.set('view engine', 'pug');
app.set('views', './views_mariadb');
//bodyParser (post method)
app.use(bodyParser.urlencoded({extended:false}));
app.get('/topic/add', function (req, res) {
c.query('SELECT id, title FROM topic', function(err, topics) {
if(err)
showError(err, res);
res.render('add', {topics:topics, isEmpty: true});
});
});
app.post('/topic/add', function (req, res) {
var title = req.body.title;
var description = req.body.description;
var author = req.body.author;
var params = [title, description, author];
c.query('INSERT INTO topic (title, description, author) VALUES (?, ?, ?)', params, function(err, result) {
if(err)
showError(err, res);
res.redirect('/topic/' + result.info.insertId);
});
});
app.get(['/topic', '/topic/:id'], function (req, res) {
c.query('SELECT id, title FROM topic', function(err, topics) {
var id = req.params.id;
if (err) {
showError(err, res);
} else if(id) {
c.query('SELECT * FROM topic WHERE id = ' + id ,
function(err, topic) {
if (err)
showError(err, res);
res.render('view', {topics:topics, topic:topic[0], isEmpty:false});
});
} else {
console.log('empty id');
res.render('view', {topics:topics, isEmpty: true});
}
});
});
app.get('/topic/:id/edit', function(req, res) {
var sql = 'SELECT id, title FROM topic';
c.query(sql, function(err, topics) {
var id = req.params.id;
if(err) {
showError(err, res);
} else if(id){
c.query('SELECT * FROM topic WHERE id = ' + id, function(err, topic) {
if(err)
showError(err, res);
res.render('edit', {topics:topics, topic:topic[0], isEmpty:false});
});
} else {
showError('There is no id.', res);
}
});
});
app.post('/topic/:id/edit', function(req, res) {
var title = req.body.title;
var description = req.body.description;
var author = req.body.author;
var id = req.params.id;
var sql = 'UPDATE topic SET title=?, description=?, author=? WHERE id=?';
c.query(sql, [title, description, author, id], function(err, topic) {
if(err)
showError(err, res);
res.redirect('/topic/'+id);
});
});
app.get('/topic/:id/delete', function(req, res) {
var sql = 'SELECT id, title FROM topic';
c.query(sql, function(err, topics) {
var id = req.params.id;
if(err) {
showError(err, res);
} else if(id){
c.query('SELECT * FROM topic WHERE id = ' + id, function(err, topic) {
if(err)
showError(err, res);
res.render('delete', {topics:topics, topic:topic[0], isEmpty:false});
});
} else {
showError('There is no id.', res);
}
});
});
app.post('/topic/:id/delete', function(req, res) {
var id = req.params.id;
var sql = 'DELETE FROM topic WHERE id=?';
c.query(sql, [id], function(err, result) {
res.redirect('/topic/');
});
});
app.listen(3000, function() {
console.log('Connected 3000 port!');
});
function showError(err, res) {
console.log(err);
res.status(500).send('Internal Server Error');
}