From 875812e010873b3b58e6beab7d2bf718d01b206e Mon Sep 17 00:00:00 2001 From: snghx Date: Wed, 18 Aug 2021 00:44:41 +0900 Subject: [PATCH] =?UTF-8?q?=EB=9D=BC=EC=9A=B0=ED=84=B0-=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20=EB=B6=84=EB=A6=AC=20(1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/auth.ctrl.js | 0 controllers/dailynote.ctrl.js | 133 ++++++++++++++++ controllers/dday.ctrl.js | 105 +++++++++++++ controllers/{manage.js => manage.ctrl.js} | 0 controllers/mypage.ctrl.js | 179 ++++++++++++++++++++++ controllers/page.ctrl.js | 33 ++++ controllers/timerDay.ctrl.js | 60 ++++++++ controllers/timerWeek.ctrl.js | 28 ++++ controllers/todo.ctrl.js | 0 routes/dailynote.js | 133 +--------------- routes/dday.js | 109 +------------ routes/manage.js | 2 +- routes/mypage.js | 175 +-------------------- routes/page.js | 49 +----- routes/timerDay.js | 75 ++------- routes/timerWeek.js | 24 +-- 16 files changed, 579 insertions(+), 526 deletions(-) create mode 100644 controllers/auth.ctrl.js create mode 100644 controllers/dailynote.ctrl.js create mode 100644 controllers/dday.ctrl.js rename controllers/{manage.js => manage.ctrl.js} (100%) create mode 100644 controllers/mypage.ctrl.js create mode 100644 controllers/page.ctrl.js create mode 100644 controllers/timerDay.ctrl.js create mode 100644 controllers/timerWeek.ctrl.js create mode 100644 controllers/todo.ctrl.js diff --git a/controllers/auth.ctrl.js b/controllers/auth.ctrl.js new file mode 100644 index 0000000..e69de29 diff --git a/controllers/dailynote.ctrl.js b/controllers/dailynote.ctrl.js new file mode 100644 index 0000000..39210aa --- /dev/null +++ b/controllers/dailynote.ctrl.js @@ -0,0 +1,133 @@ +const express = require('express'); +const solux_db = require('../models/db')(); +const connection = solux_db.init(); +solux_db.test_open(connection); + +const list = (req,res)=>{ + const dailyid = req.decoded.id; + try{ + console.log("dailyid: "+dailyid); + //console.log("req.cookies: "+req.cookies); + const searchList = "SELECT * from daily WHERE dailyid = ?"; + connection.query(searchList, dailyid, (err,result)=>{ + console.log(result); + if (result.length==0){ + return res.json({success : true, message : '아직 dailynote 없음'}); + } + return res.send(result); + }) + }catch(err){ + console.error(err); + return err; + } +}; + +const change = (req,res)=>{ + const index =req.body.index; + const content = req.body.content; + try{ + const sqlCheck = "UPDATE daily SET content = ? WHERE `index` = ?"; + connection.query(sqlCheck, [content, index] , (err, result)=>{ + console.log(result); + if(err){ + console.log(err); + return res.json({success : false, message : 'daily change 오류'}); + }else{ + console.log('daily 완료'); + return res.redirect('/dailynote/list'); + } + }) + }catch(err){ + console.log(err); + } +}; + +const check = (req,res)=>{ + const index =req.body.index; + try{ + const sqlSearch = "SELECT `check` from daily WHERE `index`=?"; + connection.query(sqlSearch, index, (err,result)=>{ + console.log(result[0].check); + const ck = result[0].check; + console.log(ck); + + const sqlCheck = "UPDATE daily SET `check` = ? WHERE `index` = ?"; + if (ck==1){ + connection.query(sqlCheck, [0, index] , (err, result2)=>{ + console.log(result2); + if(err){ + console.log(err); + return res.json({success : false, message : 'daily check to 0 오류'}); + }else{ + console.log('update to 0 완료'); + return res.redirect('/dailynote/list'); + } + }) + }else{ + connection.query(sqlCheck, [1, index ], (err, result2)=>{ + console.log(result2); + if(err){ + console.log(err); + return res.json({success : false, message : 'daily check to 1 오류'}); + }else{ + console.log('update to 1 완료'); + return res.redirect('/dailynote/list'); + } + }) + } + }) + }catch(err){ + console.log(err); + } +}; + +const del = (req, res)=>{ + console.log('delete 실행 시작'); + const index = req.body.index; + try{ + const sqlDelete = "DELETE FROM daily WHERE `index` = ?"; + connection.query(sqlDelete, index, (err, result)=>{ + console.log(result); + if(err){ + console.log(err); + return res.json({success : false, message : 'daily delete 오류'}); + } + else{ + console.log("Number of records deleted: " + result.affectedRows); + return res.redirect('/dailynote/list'); + } + }); + }catch(err){ + console.log(err); + } +}; + +const insert = (req,res)=>{ + const dailyid = req.decoded.id; + const { date, content, subject } = req.body; + try{ + console.log(dailyid); + console.log(content); + + const insertTodo = 'INSERT INTO daily (dailyid, content, subject, date, `check`) VALUES(?,?,?,?,?)'; + connection.query(insertTodo, [dailyid, content, subject, date, 0 ] , async(err,result)=>{ + if(err) console.log(err); + else{ + console.log('daily 추가 성공'); + return res.redirect('/dailynote/list'); + //return res.send(content); + } + }); + }catch(err){ + console.log(err); + return res.json({success : false, message : 'daily 추가 오류'}); + } +}; + +module.exports = { + list, + insert, + change, + del, + check +} \ No newline at end of file diff --git a/controllers/dday.ctrl.js b/controllers/dday.ctrl.js new file mode 100644 index 0000000..795b362 --- /dev/null +++ b/controllers/dday.ctrl.js @@ -0,0 +1,105 @@ +const express = require('express'); +const solux_db = require('../models/db')(); +const connection = solux_db.init(); +solux_db.test_open(connection); + +const list = (req,res)=>{ + const id = req.decoded.id; + try{ + console.log('id : '+id); + const sqlSearch = "SELECT * from dday WHERE ddayid = ?"; + connection.query(sqlSearch, id, (err,result)=>{ + if(err) console.log(err); + else{ + if(result.length==0){ + return res.json({success : true, message : '아직 dday 없음'}); + } + return res.send(result); + } + }); + }catch(err){ + console.log(err); + } +}; + +const insert = (req,res)=>{ + const { id, date, content } = req.body; + try{ + console.log('id : '+id); + const sqlInsert = "INSERT INTO dday (ddayid, date, content) VALUES (?,?,?)"; + connection.query(sqlInsert, [id,date,content], (err, result)=>{ + if(err){ + console.log(err); + return res.json({success : false, message : 'dday insert 오류'}); + } + else{ + return res.redirect('/dday/list'); + } + }); + }catch(err){ + console.log(err); + } +}; + +const del = (req,res)=>{ + console.log('delete 실행 시작'); + const index = req.body.index; + try{ + const sqlDelete = "DELETE FROM dday WHERE `index` = ?"; + connection.query(sqlDelete, index, (err, result)=>{ + console.log(result); + if(err){ + console.log(err); + return res.json({success : false, message : 'dday delete 오류'}); + } + else{ + console.log("Number of records deleted: " + result.affectedRows); + return res.redirect('/dday/list'); + } + }); + }catch(err){ + console.log(err); + } +}; + +const changeDate = (req,res)=>{ + const { idx, date } = req.body; + try{ + const sqlChangeDate = "UPDATE dday SET date = ? WHERE `index`= ?"; + connection.query(sqlChangeDate, [date, idx], (err,result)=>{ + if(err) console.log(err); + else{ + console.log('수정 성공'); + res.redirect('/dday/list'); + } + }) + }catch(err){ + console.log(err); + return res.json({success : false, message : 'dday 날짜 수정 오류'}); + } +}; + +const changeCont = (req,res)=>{ + const { idx, content } = req.body; + try{ + const sqlChangeDate = "UPDATE dday SET content = ? WHERE `index`= ?"; + connection.query(sqlChangeDate, [content, idx], (err,result)=>{ + if(err) console.log(err); + else{ + console.log('수정 성공'); + res.redirect('/dday/list'); + } + }) + }catch(err){ + console.log(err); + return res.json({success : false, message : 'dday 내용 수정 오류'}); + } +}; + +module.exports = { + list, + insert, + del, + changeCont, + changeDate +} \ No newline at end of file diff --git a/controllers/manage.js b/controllers/manage.ctrl.js similarity index 100% rename from controllers/manage.js rename to controllers/manage.ctrl.js diff --git a/controllers/mypage.ctrl.js b/controllers/mypage.ctrl.js new file mode 100644 index 0000000..3aa2aab --- /dev/null +++ b/controllers/mypage.ctrl.js @@ -0,0 +1,179 @@ +const express = require('express'); +//const jsonwebtoken = require('jsonwebtoken'); +const bcrypt = require('bcrypt'); +//const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares'); +const db = require('../models/db')(); +const connection = db.init(); +db.test_open(connection); + +const info = (req,res)=>{ + const id = req.decoded.id; + try{ + console.log(id); + const sqlInfo = "SELECT * from user WHERE id = ?"; + connection.query(sqlInfo, id, (err,result)=>{ + if(result.length!=0){ + console.log(result); + return res.json({ success : true, data : result}); + } + else{ + console.log('잘못된 ID'); + return res.json({ success : false, message : '잘못된 ID'}); + } + }) + }catch(err){ + console.log(err); + } + } + +const changePw = (req,res)=>{ + const id = req.decoded.id; + const { name, newPw } = req.body; + try{ + console.log(id+","+name); + const sqlSearch = "SELECT * from user WHERE id = ? AND name = ?"; + connection.query(sqlSearch, [id, name], async(err,result)=>{ + if(err) { + console.log(err); + return res.json({success : false, message : '비밀번호 변경 오류입니다.'}); + + } + else{ + if(result.length ==0 ){ + console.log('존재하지 않는 회원'); + return res.json({success : false, message : '존재하지 않는 회원입니다.'}); + } + else{ + const pwUpdate = "UPDATE user SET pw = ? WHERE id=? AND name = ?"; + const hash = await bcrypt.hash(newPw,12); + connection.query(pwUpdate, [hash, id, name] , async(err,result)=>{ + if(err){ + console.log(err); + return res.json({success : false, message : '비밀번호 변경 오류입니다.'}); + } + else{ + console.log('pw변경 성공'); + return res.json({success : true, message : '비밀번호 변경 완료'}); + } + })} + }}); + }catch(err){ + console.log(err); + } + } + +const changeEmail = (req,res)=>{ + const id = req.decoded.id; + const { newEmail } = req.body; + try{ + console.log(id+","+newEmail); + const sqlSearch = "SELECT * from user WHERE id=?"; + connection.query(sqlSearch, id, (err,result)=>{ + if(err){ + console.log(err); + return res.json({success : false, message : 'email 변경 오류'}); + } + else{ + if(result.length==0){ + console.log('존재하지 않는 회원'); + return res.json({success : false, message : '존재하지 않는 회원입니다.'}); + } + else{ + const emailUpdate = "UPDATE user SET email = ? WHERE id = ?"; + connection.query(emailUpdate, [newEmail, id], (err,result)=>{ + if(err) { + console.log(err); + return res.json({success : false, message : 'email 변경 오류'}); + } + else{ + console.log('email 변경 성공'); + return res.json({success: true, message : 'email 변경 완료'}); + } + }) + } + } + }) + }catch(err){ + console.log(err); + } +} + +const changeMajor = (req,res)=>{ + const id = req.decoded.id; + const { newMajor } = req.body; + try{ + console.log(id+","+newMajor); + const sqlSearch = "SELECT * from user WHERE id = ?"; + connection.query(sqlSearch, id, (err,result)=>{ + if(err) { + console.log(err); + return res.json({success : false, message : 'major 변경 오류'}); + } + else{ + if(result.length==0){ + console.log('존재하지 않는 회원'); + return res.json({success : false, message : '존재하지 않는 회원입니다.'}); + } + else{ + const majorUpdate = "UPDATE user SET major = ? WHERE id = ?"; + connection.query(majorUpdate, [newMajor, id], (err,result)=>{ + if(err){ + console.log(err); + return res.json({success : false, message : 'major 변경 오류'}); + } + else{ + console.log('major 변경 성공'); + return res.json({success : true, message : 'major 변경 완료'}); + } + }) + } + } + }) + }catch(err){ + console.log(err); + } +} + +const changeTel = (req,res)=>{ + const id = req.decoded.id; + const { newTel } = req.body; + try{ + console.log(id+","+newTel); + const sqlSearch = "SELECT * from user WHERE id = ?"; + connection.query(sqlSearch, id, (err,result)=>{ + if(err) { + console.log(err); + return res.json({success : false, message : 'tel 변경 오류'}); + } + else{ + if(result.length==0){ + console.log('존재하지 않는 회원'); + return res.json({success : false, message : '존재하지 않는 회원입니다.'}); + } + else{ + const telUpdate = "UPDATE user SET tel = ? WHERE id = ?"; + connection.query(telUpdate, [newTel, id], (err,result)=>{ + if(err){ + console.log(err); + return res.json({success : false, message : 'tel 변경 오류'}); + } + else{ + console.log('tel 변경 성공'); + return res.json({success : true, message : 'tel 변경 완료'}); + } + }) + } + } + }) + }catch(err){ + console.log(err); + } +} + +module.exports = { + info, + changePw, + changeEmail, + changeMajor, + changeTel +}; \ No newline at end of file diff --git a/controllers/page.ctrl.js b/controllers/page.ctrl.js new file mode 100644 index 0000000..802bded --- /dev/null +++ b/controllers/page.ctrl.js @@ -0,0 +1,33 @@ +const express = require('express'); +const solux_db = require('../models/db')(); +const connection = solux_db.init(); +solux_db.test_open(connection); + +const join = (req, res) => { + console.log('회원가입으로 이동'); + res.redirect('/auth/join'); +}; + +const login = (req,res)=>{ + console.log('로그인으로 이동'); + res.redirect('/auth/login'); +} + +const main = (req,res,next)=>{ + //console.log(req.cookies.user_id); + res.sendFile('index.html'); +}; + +/* +router.get('/', (req,res,next)=>{ + //res.render('todo.html'); + console.log(req.cookies.user_id); + res.sendFile('index.html'); + }); + */ + +module.exports = { + main, + login, + join +}; \ No newline at end of file diff --git a/controllers/timerDay.ctrl.js b/controllers/timerDay.ctrl.js new file mode 100644 index 0000000..f9d0e0d --- /dev/null +++ b/controllers/timerDay.ctrl.js @@ -0,0 +1,60 @@ +const express = require('express'); +const solux_db = require('../models/db')(); +const connection = solux_db.init(); +solux_db.test_open(connection); + +const list = (req, res) => { + const id = req.decoded.id; + try { + console.log('id : ' + id); + const sqlSearch = "SELECT * from timerDay WHERE timerDayid = ?"; + connection.query(sqlSearch, id, (err, result) => { + if (err) console.log(err); + else { + if (result.length == 0) { + return res.json({ success: true, message: '아직 기록된 timer record 없음' }); + } + return res.send(result); + } + }); + } catch (err) { + console.log(err); + return res.json({ success: false, message: 'timer list 오류' }); + } +}; + +const update = (req, res) => { + const recordDay = req.body.recordDay; + const timerDayid = req.decoded.id; + try { + const sqlSelect = "SELECT recordDay from timerDay WHERE timerDayid =?"; + connection.query(sqlSelect, timerDayid, (err, result) => { + if (err) { + console.log('select 오류 ' + err); + return res.send({ success: false, message: "select 오류" }); + } + else { + const newrecord = parseInt(recordDay) + parseInt(result[0].recordDay); + console.log(newrecord); + const sqlChangeDate = "UPDATE timerDay SET recordDay = ? WHERE timerDayid = ?"; + connection.query(sqlChangeDate, [newrecord, timerDayid], (err, result) => { + if (err) { + console.log("update 오류 " + err); + return res.send({ success: false, message: "일일 타이머 기록 갱신 오류" }); + } + else { + console.log('timerDay record 수정 완료'); + res.redirect('/timerDay/list'); + } + }) + } + }) + } catch (err) { + console.log(err); + } +}; + +module.exports = { + list, + update +}; \ No newline at end of file diff --git a/controllers/timerWeek.ctrl.js b/controllers/timerWeek.ctrl.js new file mode 100644 index 0000000..527e53c --- /dev/null +++ b/controllers/timerWeek.ctrl.js @@ -0,0 +1,28 @@ +const express = require('express'); +const db = require('../models/db')(); +const connection = db.init(); +db.test_open(connection); + +const rank = (req,res)=>{ + try{ + const rankSelect = "SELECT * FROM timerWeek ORDER BY recordWeek DESC Limit 5"; + connection.query(rankSelect, (err,result)=>{ + if(err){ + console.log(err); + } + else{ + console.log(result); + return res.json({ + success : true, + data : result + }) + } + } ) + }catch(err){ + console.log(err); + } +} + +module.exports = { + rank +}; \ No newline at end of file diff --git a/controllers/todo.ctrl.js b/controllers/todo.ctrl.js new file mode 100644 index 0000000..e69de29 diff --git a/routes/dailynote.js b/routes/dailynote.js index 86c6e2f..99f8544 100644 --- a/routes/dailynote.js +++ b/routes/dailynote.js @@ -1,133 +1,12 @@ const express = require('express'); -const mysql = require('mysql'); -const moment = require('moment'); const router = express.Router(); -const path = require('path'); const { verifyToken } = require('./middlewares'); -const solux_db = require('../models/db')(); -const connection = solux_db.init(); -solux_db.test_open(connection); +const ctrl = require("../controllers/dailynote.ctrl"); -router.get('/list', verifyToken, (req,res)=>{ - const dailyid = req.decoded.id; - try{ - console.log("dailyid: "+dailyid); - //console.log("req.cookies: "+req.cookies); - const searchList = "SELECT * from daily WHERE dailyid = ?"; - connection.query(searchList, dailyid, (err,result)=>{ - console.log(result); - if (result.length==0){ - return res.json({success : true, message : '아직 dailynote 없음'}); - } - return res.send(result); - }) - }catch(err){ - console.error(err); - return err; - } -}); - -router.post('/change', (req,res)=>{ - const index =req.body.index; - const content = req.body.content; - try{ - const sqlCheck = "UPDATE daily SET content = ? WHERE `index` = ?"; - connection.query(sqlCheck, [content, index] , (err, result)=>{ - console.log(result); - if(err){ - console.log(err); - return res.json({success : false, message : 'daily change 오류'}); - }else{ - console.log('daily 완료'); - return res.redirect('/dailynote/list'); - } - }) - }catch(err){ - console.log(err); - } -}); - -router.post('/check', (req,res)=>{ - const index =req.body.index; - try{ - const sqlSearch = "SELECT `check` from daily WHERE `index`=?"; - connection.query(sqlSearch, index, (err,result)=>{ - console.log(result[0].check); - const ck = result[0].check; - console.log(ck); - - const sqlCheck = "UPDATE daily SET `check` = ? WHERE `index` = ?"; - if (ck==1){ - connection.query(sqlCheck, [0, index] , (err, result2)=>{ - console.log(result2); - if(err){ - console.log(err); - return res.json({success : false, message : 'daily check to 0 오류'}); - }else{ - console.log('update to 0 완료'); - return res.redirect('/dailynote/list'); - } - }) - }else{ - connection.query(sqlCheck, [1, index ], (err, result2)=>{ - console.log(result2); - if(err){ - console.log(err); - return res.json({success : false, message : 'daily check to 1 오류'}); - }else{ - console.log('update to 1 완료'); - return res.redirect('/dailynote/list'); - } - }) - } - }) - }catch(err){ - console.log(err); - } -}); - -router.post('/delete', (req, res)=>{ - console.log('delete 실행 시작'); - const index = req.body.index; - try{ - const sqlDelete = "DELETE FROM daily WHERE `index` = ?"; - connection.query(sqlDelete, index, (err, result)=>{ - console.log(result); - if(err){ - console.log(err); - return res.json({success : false, message : 'daily delete 오류'}); - } - else{ - console.log("Number of records deleted: " + result.affectedRows); - return res.redirect('/dailynote/list'); - } - }); - }catch(err){ - console.log(err); - } -} -); - -router.post('/insert', verifyToken, (req,res)=>{ - const dailyid = req.decoded.id; - const { date, content, subject } = req.body; - try{ - console.log(dailyid); - console.log(content); - - const insertTodo = 'INSERT INTO daily (dailyid, content, subject, date, `check`) VALUES(?,?,?,?,?)'; - connection.query(insertTodo, [dailyid, content, subject, date, 0 ] , async(err,result)=>{ - if(err) console.log(err); - else{ - console.log('daily 추가 성공'); - return res.redirect('/dailynote/list'); - //return res.send(content); - } - }); - }catch(err){ - console.log(err); - return res.json({success : false, message : 'daily 추가 오류'}); - } -}); +router.get('/list', verifyToken, ctrl.list); +router.post('/insert', verifyToken, ctrl.insert); +router.post('/del', ctrl.del); +router.post('/change', ctrl.change); +router.post('/check', ctrl.check); module.exports = router; \ No newline at end of file diff --git a/routes/dday.js b/routes/dday.js index 13229e5..5b3fe02 100644 --- a/routes/dday.js +++ b/routes/dday.js @@ -1,109 +1,12 @@ const express = require('express'); -const mysql = require('mysql'); - const router = express.Router(); - -const path = require('path'); const { verifyToken } = require('./middlewares'); +const ctrl = require("../controllers/dday.ctrl"); -const solux_db = require('../models/db')(); -const connection = solux_db.init(); - -solux_db.test_open(connection); - -router.get('/list', verifyToken, (req,res)=>{ - const id = req.decoded.id; - try{ - console.log('id : '+id); - const sqlSearch = "SELECT * from dday WHERE ddayid = ?"; - connection.query(sqlSearch, id, (err,result)=>{ - if(err) console.log(err); - else{ - if(result.length==0){ //존재하지 않는 회원 - return res.json({success : true, message : '아직 dday 없음'}); - } - return res.send(result); - } - }); - }catch(err){ - console.log(err); - } -}); - -router.post('/insert', verifyToken, (req,res)=>{ - const { id, date, content } = req.body; - try{ - console.log('id : '+id); - const sqlInsert = "INSERT INTO dday (ddayid, date, content) VALUES (?,?,?)"; - connection.query(sqlInsert, [id,date,content], (err, result)=>{ - if(err){ - console.log(err); - return res.json({success : false, message : 'dday insert 오류'}); - } - else{ - return res.redirect('/dday/list'); - } - }); - }catch(err){ - console.log(err); - } - - -}) - -router.post('/delete', (req,res)=>{ - console.log('delete 실행 시작'); - const index = req.body.index; - try{ - const sqlDelete = "DELETE FROM dday WHERE `index` = ?"; - connection.query(sqlDelete, index, (err, result)=>{ - console.log(result); - if(err){ - console.log(err); - return res.json({success : false, message : 'dday delete 오류'}); - } - else{ - console.log("Number of records deleted: " + result.affectedRows); - return res.redirect('/dday/list'); - } - }); - }catch(err){ - console.log(err); - } -}); -router.post('/changeDate', (req,res)=>{ - const { idx, date } = req.body; - try{ - const sqlChangeDate = "UPDATE dday SET date = ? WHERE `index`= ?"; - connection.query(sqlChangeDate, [date, idx], (err,result)=>{ - if(err) console.log(err); - else{ - console.log('수정 성공'); - res.redirect('/dday/list'); - } - }) - }catch(err){ - console.log(err); - return res.json({success : false, message : 'dday 날짜 수정 오류'}); - } - -}) - -router.post('/changeCont', (req,res)=>{ - const { idx, content } = req.body; - try{ - const sqlChangeDate = "UPDATE dday SET content = ? WHERE `index`= ?"; - connection.query(sqlChangeDate, [content, idx], (err,result)=>{ - if(err) console.log(err); - else{ - console.log('수정 성공'); - res.redirect('/dday/list'); - } - }) - }catch(err){ - console.log(err); - return res.json({success : false, message : 'dday 내용 수정 오류'}); - } -}) +router.get('/list', verifyToken, ctrl.list); +router.post('/insert', verifyToken, ctrl.insert); +router.post('/del', ctrl.del); +router.post('/changeCont', ctrl.changeCont); +router.post('/changeDate', ctrl.changeDate); module.exports = router; \ No newline at end of file diff --git a/routes/manage.js b/routes/manage.js index 0316c5e..f881ea9 100644 --- a/routes/manage.js +++ b/routes/manage.js @@ -1,7 +1,7 @@ const express = require('express'); const router = express.Router(); const { verifyToken } = require('./middlewares'); -const ctrl = require("../controllers/manage"); +const ctrl = require("../controllers/manage.ctrl"); router.get('/list', verifyToken, ctrl.list); router.post('/insert', verifyToken, ctrl.insert); diff --git a/routes/mypage.js b/routes/mypage.js index c4e4f24..9943b12 100644 --- a/routes/mypage.js +++ b/routes/mypage.js @@ -3,176 +3,15 @@ const jsonwebtoken = require('jsonwebtoken'); const bcrypt = require('bcrypt'); const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares'); const router = express.Router(); - const db = require('../models/db')(); const connection = db.init(); - db.test_open(connection); +const ctrl = require("../controllers/mypage.ctrl"); -router.get('/info', verifyToken, (req,res)=>{ - const id = req.decoded.id; - try{ - console.log(id); - const sqlInfo = "SELECT * from user WHERE id = ?"; - connection.query(sqlInfo, id, (err,result)=>{ - if(result.length!=0){ - console.log(result); - return res.json({ success : true, data : result}); - } - else{ - console.log('잘못된 ID'); - return res.json({ success : false, message : '잘못된 ID'}); - } - }) - }catch(err){ - console.log(err); - } - }) - -router.post('/changePw', verifyToken, (req,res)=>{ - const id = req.decoded.id; - const { name, newPw } = req.body; - try{ - console.log(id+","+name); - const sqlSearch = "SELECT * from user WHERE id = ? AND name = ?"; - connection.query(sqlSearch, [id, name], async(err,result)=>{ - if(err) { - console.log(err); - return res.json({success : false, message : '비밀번호 변경 오류입니다.'}); - - } - else{ - if(result.length ==0 ){ - console.log('존재하지 않는 회원'); - return res.json({success : false, message : '존재하지 않는 회원입니다.'}); - } - else{ - const pwUpdate = "UPDATE user SET pw = ? WHERE id=? AND name = ?"; - const hash = await bcrypt.hash(newPw,12); - connection.query(pwUpdate, [hash, id, name] , async(err,result)=>{ - if(err){ - console.log(err); - return res.json({success : false, message : '비밀번호 변경 오류입니다.'}); - } - else{ - console.log('pw변경 성공'); - return res.json({success : true, message : '비밀번호 변경 완료'}); - } - })} - }}); - }catch(err){ - console.log(err); - } - }) - -router.post('/changeEmail', verifyToken, (req,res)=>{ - const id = req.decoded.id; - const { newEmail } = req.body; - try{ - console.log(id+","+newEmail); - const sqlSearch = "SELECT * from user WHERE id=?"; - connection.query(sqlSearch, id, (err,result)=>{ - if(err){ - console.log(err); - return res.json({success : false, message : 'email 변경 오류'}); - } - else{ - if(result.length==0){ - console.log('존재하지 않는 회원'); - return res.json({success : false, message : '존재하지 않는 회원입니다.'}); - } - else{ - const emailUpdate = "UPDATE user SET email = ? WHERE id = ?"; - connection.query(emailUpdate, [newEmail, id], (err,result)=>{ - if(err) { - console.log(err); - return res.json({success : false, message : 'email 변경 오류'}); - } - else{ - console.log('email 변경 성공'); - return res.json({success: true, message : 'email 변경 완료'}); - } - }) - } - } - }) - }catch(err){ - console.log(err); - } -}) - -router.post('/changeMajor', verifyToken, (req,res)=>{ - const id = req.decoded.id; - const { newMajor } = req.body; - try{ - console.log(id+","+newMajor); - const sqlSearch = "SELECT * from user WHERE id = ?"; - connection.query(sqlSearch, id, (err,result)=>{ - if(err) { - console.log(err); - return res.json({success : false, message : 'major 변경 오류'}); - } - else{ - if(result.length==0){ - console.log('존재하지 않는 회원'); - return res.json({success : false, message : '존재하지 않는 회원입니다.'}); - } - else{ - const majorUpdate = "UPDATE user SET major = ? WHERE id = ?"; - connection.query(majorUpdate, [newMajor, id], (err,result)=>{ - if(err){ - console.log(err); - return res.json({success : false, message : 'major 변경 오류'}); - } - else{ - console.log('major 변경 성공'); - return res.json({success : true, message : 'major 변경 완료'}); - } - }) - } - } - }) - }catch(err){ - console.log(err); - } -}) - -router.post('/changeTel', verifyToken, (req,res)=>{ - const id = req.decoded.id; - const { newTel } = req.body; - try{ - console.log(id+","+newTel); - const sqlSearch = "SELECT * from user WHERE id = ?"; - connection.query(sqlSearch, id, (err,result)=>{ - if(err) { - console.log(err); - return res.json({success : false, message : 'tel 변경 오류'}); - } - else{ - if(result.length==0){ - console.log('존재하지 않는 회원'); - return res.json({success : false, message : '존재하지 않는 회원입니다.'}); - } - else{ - const telUpdate = "UPDATE user SET tel = ? WHERE id = ?"; - connection.query(telUpdate, [newTel, id], (err,result)=>{ - if(err){ - console.log(err); - return res.json({success : false, message : 'tel 변경 오류'}); - } - else{ - console.log('tel 변경 성공'); - return res.json({success : true, message : 'tel 변경 완료'}); - } - }) - } - } - }) - }catch(err){ - console.log(err); - } -}) - - +router.get('/info', verifyToken, ctrl.info); +router.post('/changePw', verifyToken, ctrl.changePw); +router.post('/changeEmail', verifyToken, ctrl.changeEmail); +router.post('/changeMajor', verifyToken, ctrl.changeMajor); +router.post('/changeTel', verifyToken, ctrl.changeTel); - module.exports = router; \ No newline at end of file +module.exports = router; \ No newline at end of file diff --git a/routes/page.js b/routes/page.js index 39cf274..16ebabf 100644 --- a/routes/page.js +++ b/routes/page.js @@ -1,51 +1,10 @@ const express = require('express'); -const mysql = require('mysql'); const { isLoggedIn, isNotLoggedIn, verifyToken } = require('./middlewares'); - const router = express.Router(); +const ctrl = require("../controllers/page.ctrl"); -const solux_db = require('../models/db')(); -const connection = solux_db.init(); - -solux_db.test_open(connection); - -router.get('/join', isNotLoggedIn, (req,res)=>{ - console.log('회원가입으로 이동'); - res.redirect('/auth/join'); -}); - -router.get('/login', isNotLoggedIn, (req,res)=>{ - console.log('로그인으로 이동'); - res.redirect('/auth/login'); -}) - -/* -router.get('/todo',verifyToken, (req,res)=>{ - console.log('todo로 이동'); - res.redirect('/todo/list'); -}) -*/ - -router.get('/', (req,res,next)=>{ - //res.render('todo.html'); - console.log(req.cookies.user_id); - res.sendFile('index.html'); - /* - const sqlConnect = "SELECT * FROM user"; - connection.query(sqlConnect, (err,rows, fileds)=>{ - if(!err){ - for(var i=0;i { - const id = req.decoded.id; - try { - console.log('id : ' + id); - const sqlSearch = "SELECT * from timerDay WHERE timerDayid = ?"; - connection.query(sqlSearch, id, (err, result) => { - if (err) console.log(err); - else { - if (result.length == 0) { - return res.json({ success: true, message: '아직 기록된 timer record 없음' }); - } - return res.send(result); - } - }); - } catch (err) { - console.log(err); - return res.json({ success: false, message: 'timer list 오류' }); - } -}); +router.get('/list', verifyToken, ctrl.list); +router.post('/update', verifyToken, ctrl.update); -router.post('/update', verifyToken, (req, res) => { - const recordDay = req.body.recordDay; - const timerDayid = req.decoded.id; - try { - const sqlSelect = "SELECT recordDay from timerDay WHERE timerDayid =?"; - connection.query(sqlSelect, timerDayid, (err, result) => { - if (err) { - console.log('select 오류 ' + err); - return res.send({success: false, message : "select 오류"}); - } - else { - const newrecord = parseInt(recordDay) + parseInt(result[0].recordDay); - console.log(newrecord); - const sqlChangeDate = "UPDATE timerDay SET recordDay = ? WHERE timerDayid = ?"; - connection.query(sqlChangeDate, [newrecord, timerDayid], (err, result) => { - if (err) { - console.log("update 오류 " + err); - return res.send({success: false, message : "일일 타이머 기록 갱신 오류"}); - } - else { - console.log('timerDay record 수정 완료'); - res.redirect('/timerDay/list'); - } - }) - } - }) - } catch (err) { - console.log(err); - } -}) - - -schedule.scheduleJob('0 0 0 * * *', async()=>{ - const timerDayid = req.decoded.id; +schedule.scheduleJob('0 0 0 * * *', async () => { try { + //const timerDayid = req.decoded.id; //현재 로그인한 사용자 뿐만 아니라 전체 사용자의 기록 갱신 (수정) const sqlSelectWeek = "SELECT recordWeek from timerWeek WHERE timerWeekid = ?"; connection.query(sqlSelectWeek, timerDayid, (err, result1) => { if (err) console.log('select 오류' + err); @@ -79,7 +26,7 @@ schedule.scheduleJob('0 0 0 * * *', async()=>{ const sqlDelete = "UPDATE timerDay SET recordDay = 0 "; connection.query(sqlDelete, (err, result4) => { if (err) console.log(err); - else console.log('timer day 초기화 완료'); + else console.log('timer day 초기화 완료 (login)'); }) } }) @@ -89,7 +36,15 @@ schedule.scheduleJob('0 0 0 * * *', async()=>{ }) } catch (err) { console.log(err); - return res.json({ success: false, message: "자동 실행 실패" }); + const sqlDelete = "UPDATE timerDay SET recordDay = 2 "; + connection.query(sqlDelete, (err, result4) => { + if (err) { + console.log(err); + } + else { + console.log('timer day 초기화 완료'); + } + }) } }) diff --git a/routes/timerWeek.js b/routes/timerWeek.js index 8140ebf..fb23122 100644 --- a/routes/timerWeek.js +++ b/routes/timerWeek.js @@ -1,32 +1,12 @@ const express = require('express'); -const mysql = require('mysql'); const schedule = require('node-schedule'); -const {verifyToken} = require('./middlewares') const db = require('../models/db')(); const connection = db.init(); const router = express.Router(); - db.test_open(connection); +const ctrl = require("../controllers/timerWeek.ctrl"); -router.get('/rank', (req,res)=>{ - try{ - const rankSelect = "SELECT * FROM timerWeek ORDER BY recordWeek DESC Limit 5"; - connection.query(rankSelect, (err,result)=>{ - if(err){ - console.log(err); - } - else{ - console.log(result); - return res.json({ - success : true, - data : result - }) - } - } ) - }catch(err){ - console.log(err); - } -}) +router.get('/rank', ctrl.rank); schedule.scheduleJob('0 0 0 * * 1', async()=>{ const sqlReset = "UPDATE timerWeek SET recordWeek=0";