-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.js
171 lines (157 loc) · 4.17 KB
/
main2.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const http = require('http');
const fs = require('fs');
const url = require('url');
const express = require('express');
const db = require('./lib/db.js');
const { v4: uid } = require('uuid');
const djte_ob = new Date();
const app = express();
app.use(express.urlencoded());
app.get('/', function(req, res){
db.query(`SELECT * FROM clients`, function(err, clients){
var title = `who's diary?`;
var name_list = `<tr>`;
var i;
for(i = 0; i < clients.length; i++)
name_list += `
<td>${clients[i]._id}</td>
<td>${clients[i].name}</td>
<td>${clients[i].uuid}</td>
<td>
<form action="/delete_process" method="post">
<input type="hidden" name="id" value="${clients[i]._id}">
<input type="submit" value="delete">
</form>
</td>
<td>
<form action="/${clients[i].name}/daily" method="post">
<input type="hidden" name="uuid" value="${clients[i].uuid}">
<input type="submit" value="일기 쓰기">
</form>
</td>
</tr><tr>`;
name_list += `</tr>`;
var template = `
<!doctype html>
<html>
<head>
<title>5y diary</title>
<meta charset="utf-8">
</head>
<body>
<h1>5year diary</h1>
<h2>${title}</h2>
<br>
<p>log-in</p>
<p><input type="text" placeholder="ID"></p>
<p><input type="text" placeholder="PW"></p>
<p>
<a href="/create">create</a>
</p>
<table>
<tr>
<td>순번</td>
<td>이름</td>
</tr>
${name_list}
</table>
</body>
</html>
<style>
td {
border : solid; 1px;
}
</style>
`;
res.send(template);
});
});
app.get('/create', function(req, res){
var template = `
<!doctype html>
<html>
<head>
<title>5y diary</title>
<meta charset="utf-8">
</head>
<body>
<h1>5year diary</h1>
<h2>계정추가</h2>
<form action="/create_process" method="post">
<p><input type="hidden" name="uuid" value="${uid()}"></p>
<p><input type="text" name="name" placeholder="name"></p>
<p><input type="submit"></p>
</form>
</html>
`;
res.send(template);
});
app.post('/create_process', function(req, res){
db.query(`INSERT INTO clients (name, uuid) VALUES(?, ?)`, [req.body.name, req.body.uuid], function(err){
if(err) throw err;
res.redirect('/');
});
});
app.post('/delete_process', function(req, res){
db.query(`DELETE FROM clients WHERE _id=?`, [req.body.id], function(err){
if(err) throw err;
res.redirect('/');
});
});
app.post('/:id/daily', function(req, res){
var uuid = req.body.uuid;
db.query(`SELECT * FROM clients`, function(err, clients){
db.query(`SELECT * FROM daily_diary WHERE uuid=?`, [uuid], function(err, daily_diary){
console.log(daily_diary);
console.log(daily_diary);
console.log(daily_diary);
console.log(daily_diary);
console.log(daily_diary);
var title = req.params.id;
var template = `
<!doctype html>
<html>
<head>
<title>${title}'s diary</title>
<meta charset="utf-8">
</head>
<body>
<p><a href="/">home page</a></p>
<h1>daily</h1>
<h2>who's : ${title}</h2>
<p>
${daily_diary.content}
</p>
<form action="/${title}/daily_upload" method="post">
<input type="hidden" name="uuid" value="${uuid}">
<input type="hidden" name="name" value="${title}">
<textarea name="content" placeholder="2문장 이하, 오늘의 일기"></textarea>
<input type="submit" value="저장">
</form>
</body>
</html>
<style>
textarea {
width: 500px;
height: 50px;
}
</style>
`;
res.send(template);
});
});
});
app.post('/:id/daily_upload', function(req, res){
var uuid = req.body.uuid;
var year = date_ob.getFullYear();
if(date_ob.getMonth()+1 < 10)
var date = `"${year} + "-" + ${(date_ob.getMonth()+1)} + "-0" + ${date_ob.getDate()}"`;
else
var date = `${year} + "-" + ${(date_ob.getMonth()+1)} + "-" + ${date_ob.getDate()}`;
var time = date_ob.getHours() + ":" + date_ob.getMinutes() + ":" + date_ob.getSeconds();
db.query(`INSERT INTO daily_diary (uuid, year, date, time, content) VALUES (?,?,?,?,?)`, [uuid, year, date, time, req.body.content], function(err){
if(err) throw err;
res.redirect(307, `/${req.params.id}/daily`);
});
});
app.listen(3005);