최동원

Merge branch 'master' into 'share,favorite'

# Conflicts:
#   backend/app.js
This diff is collapsed. Click to expand it.
...@@ -11,15 +11,18 @@ ...@@ -11,15 +11,18 @@
11 "crypto-js": "^4.0.0", 11 "crypto-js": "^4.0.0",
12 "debug": "~2.6.9", 12 "debug": "~2.6.9",
13 "ejs": "^3.1.3", 13 "ejs": "^3.1.3",
14 + "excel4node": "^1.7.2",
14 "express": "~4.16.1", 15 "express": "~4.16.1",
15 "express-session": "^1.17.1", 16 "express-session": "^1.17.1",
16 "formidable": "^1.2.2", 17 "formidable": "^1.2.2",
17 "http-errors": "~1.6.3", 18 "http-errors": "~1.6.3",
18 "moment": "^2.26.0", 19 "moment": "^2.26.0",
19 "morgan": "~1.9.1", 20 "morgan": "~1.9.1",
21 + "multer": "^1.4.2",
20 "mysql": "^2.18.1", 22 "mysql": "^2.18.1",
21 "passport": "^0.4.1", 23 "passport": "^0.4.1",
22 "pug": "^3.0.0", 24 "pug": "^3.0.0",
23 - "request": "^2.88.2" 25 + "request": "^2.88.2",
26 + "xlsx": "^0.16.2"
24 } 27 }
25 } 28 }
......
1 +var express = require('express');
2 +var router = express.Router();
3 +const XLSX = require('xlsx');
4 +var multer = require('multer');
5 +var fs = require('fs');
6 +var xl = require('excel4node');
7 +// var pool = require('./../routes/modules/database');
8 +
9 +//파일 저장위치와 파일이름 설정
10 +var storage = multer.diskStorage({
11 + destination: function (req, file, cb) {
12 + cb(null, 'public/upload');
13 + },
14 + //파일이름 설정
15 + filename: function (req, file, cb) {
16 + cb(null, file.originalname);
17 + }
18 +})
19 +
20 +//파일 업로드 모듈
21 +var upload = multer({ storage: storage })
22 +
23 +// 연락처 백업, 삭제, 검색
24 +/*
25 +contact_upload : 연락처 파일 업로드 모듈
26 +input : 사용자가 연락처를 저장해놓은 csv 혹은 xlsx 파일
27 +output : 연락처 저장 성공 여부
28 +*/
29 +router.post('/contact_upload', upload.single('file'), function(req, res, next) {
30 + // console.log(req);
31 + var user_id = req.query.id;
32 + var file = req.file;
33 + var workbook = XLSX.readFile('public/upload/contact.xlsx');
34 + var firstWSheetName = workbook.SheetNames[0];
35 + var firstWSheet = workbook.Sheets[firstWSheetName];
36 + var name = [];
37 + var phone = [];
38 + var email = [];
39 + var added_date = [];
40 + var length = firstWSheet['!ref'][4];
41 + var temp ="";
42 +
43 + var sqlquery = "insert into contact(user_id,name,email,phone,added_date) values";
44 + for(var i=1;i<length;i++)
45 + {
46 + for(var j=0;j<4;j++)
47 + {
48 + if(j==0){
49 + temp = "A" + (i+1);
50 + name.push(firstWSheet[temp].v);
51 + sqlquery += ' ("' + user_id + '","'+name[i-1];
52 + }
53 + else if(j==1){
54 + temp = "B" + (i+1);
55 + phone.push(firstWSheet[temp].v);
56 + sqlquery += '","' + phone[i-1];
57 + }
58 + else if(j==2){
59 + temp = "C" + (i+1);
60 + email.push(firstWSheet[temp].v);
61 + sqlquery += '","' + email[i-1];
62 + }
63 + else if(j==3){
64 + temp = "D" + (i+1);
65 + added_date.push(firstWSheet[temp].v);
66 + sqlquery += '",STR_TO_DATE("' + added_date[i-1] + '",' + "'%Y.%m.%d')),";
67 + }
68 +
69 + }
70 + }
71 + sqlquery = sqlquery.substring(0,sqlquery.length-1);
72 + connection.query(sqlquery, function (err, rows) {
73 + if (err) {
74 + console.log("upload contact failed");
75 + throw err;
76 + } else {
77 + console.log(rows);
78 + var filename = 'public/upload/contact.xlsx';
79 + fs.unlink(filename, function (err) {
80 + if (err) throw err;
81 + console.log('file deleted');
82 + })
83 + res.status(200).send('upload');
84 + }
85 + });
86 +});
87 +
88 +/*
89 +contact_download : 연락처 파일 다운로드 모듈
90 +input : 사용자의 연락처 다운로드 요청
91 +output : 연락처 다운로드 성공 여부 및 csv, xlsx 파일
92 +*/
93 +router.post('/contact_download', function(req, res, next) {
94 +
95 + var filename = 'public/upload/contactdownload.xlsx';
96 + fs.exists(filename, function (exists) {
97 + if(exists == true)
98 + {
99 + fs.unlink(filename, function (err) {
100 + if (err) throw err;
101 + });
102 + }
103 + });
104 + // Create a new instance of a Workbook class
105 + var wb = new xl.Workbook();
106 +
107 + // Add Worksheets to the workbook
108 + var ws = wb.addWorksheet('Sheet 1');
109 +
110 + ws.cell(1, 1)
111 + .string('name');
112 + ws.cell(1, 2)
113 + .string('phone');
114 + ws.cell(1, 3)
115 + .string('email');
116 + ws.cell(1, 4)
117 + .string('added_date');
118 +
119 + var user_id = req.query.id;
120 + var sqlquery = "SELECT * FROM contact WHERE user_id = ?";
121 + connection.query(sqlquery, [user_id], function (err, rows) {
122 + if (err) {
123 + console.log("download contact failed");
124 + throw err;
125 + } else {
126 + if(rows.length != 0)
127 + {
128 + for(var i=0;i<rows.length;i++)
129 + {
130 + ws.cell(i+2, 1).string(rows[i].name);
131 + ws.cell(i+2, 2).string(rows[i].phone);
132 + ws.cell(i+2, 3).string(rows[i].email);
133 + ws.cell(i+2, 4).string(rows[i].added_date);
134 + }
135 + }
136 + wb.write('public/upload/contactdownload.xlsx', (err, stats)=> {
137 + if (err){
138 + throw err;
139 + }
140 + else {
141 + res.sendFile(path.join(__dirname, '../public/upload/contactdownload.xlsx'));
142 + }
143 + });
144 + }
145 +
146 + });
147 +});
148 +
149 +/*
150 +contact_create : 연락처 생성 모듈
151 +input : 사용자의 연락처 생성 요청
152 +output : 연락처 생성 성공 여부
153 +사용자한테 연락처 정보 받아오기
154 +*/
155 +router.get('/contact_create', function(req, res, next) {
156 + //insert into contact(name) value ("daso")
157 +});
158 +
159 +/*
160 +contact_delete : 연락처 삭제 모듈
161 +input : 사용자의 연락처 삭제 요청
162 +output : 연락처 삭제 성공 여부
163 +*/
164 +router.post('/contact_delete', function(req, res, next) {
165 + // console.log("req.query",req.query);
166 + var user_id = req.query.id;
167 + var sqlquery = "DELETE FROM contact WHERE user_id = ?";
168 + connection.query(sqlquery, [user_id], function (err, rows) {
169 + if (err) {
170 + console.log("delete contact failed");
171 + throw err;
172 + } else {
173 + // console.log(rows);s
174 + res.status(200).send("delete");
175 + }
176 +
177 + });
178 +});
179 +
180 +/*
181 +contact_search : 연락처 검색 모듈
182 +input : 사용자가 이름, 전화번호, 이메일 중 하나를 검색
183 +output : 검색을 요청한 데이터에 부합하는 연락처를 찾았다면 출력
184 +*/
185 +router.post('/contact_search', function(req, res, next) {
186 + console.log("req.query",req.query);
187 + var user_id = req.query.id;
188 + var type = req.query.type;
189 + var info = "";
190 + if(type == "name")
191 + {
192 + info = req.query.name;
193 + var sqlquery = "SELECT * FROM contact WHERE user_id = ? and name = ?";
194 + }
195 + else if(type == "phone")
196 + {
197 + info = req.query.phone;
198 + var sqlquery = "SELECT * FROM contact WHERE user_id = ? and phone = ?";
199 + }
200 + else if(type == "email")
201 + {
202 + info = req.query.email;
203 + var sqlquery = "SELECT * FROM contact WHERE user_id = ? and email = ?";
204 + }
205 + console.log(sqlquery);
206 + connection.query(sqlquery, [user_id,info], function (err, rows) {
207 + if (err) {
208 + console.log("search contact failed");
209 + throw err;
210 + } else {
211 + console.log(rows);
212 + res.status(200).send({contact_list : rows});
213 + }
214 +
215 + });
216 +});
217 +
218 +/*
219 +contact_list : 연락처 출력 모듈
220 +input : 사용자의 연락처 출력 요청
221 +output : 저장된 연락처 목록 출력
222 +*/
223 +router.post('/contact_list', function(req, res, next) {
224 + console.log("req.query",req.query);
225 + var user_id = req.query.id;
226 + var sqlquery = "SELECT * FROM contact WHERE user_id = ?";
227 + connection.query(sqlquery, [user_id], function (err, rows) {
228 + if (err) {
229 + console.log("select contact failed");
230 + throw err;
231 + } else {
232 + console.log(rows);
233 + res.status(200).send({contact_list : rows});
234 + }
235 +
236 + });
237 +});
238 +
239 +
240 +module.exports = router;
...@@ -7,4 +7,4 @@ connection = mysql.createConnection({ ...@@ -7,4 +7,4 @@ connection = mysql.createConnection({
7 password: 'password1234', 7 password: 'password1234',
8 database: 'khuloud', 8 database: 'khuloud',
9 connectionLimit: 30 9 connectionLimit: 30
10 - });
...\ No newline at end of file ...\ No newline at end of file
10 + });
......