Skip to content

Commit

Permalink
Merge pull request #23 from SoluxProject/feature/#21
Browse files Browse the repository at this point in the history
라우터-컨트롤러 분리 (1)
  • Loading branch information
snghx authored Aug 18, 2021
2 parents 0b85b1b + 875812e commit baa355f
Show file tree
Hide file tree
Showing 16 changed files with 579 additions and 526 deletions.
Empty file added controllers/auth.ctrl.js
Empty file.
133 changes: 133 additions & 0 deletions controllers/dailynote.ctrl.js
Original file line number Diff line number Diff line change
@@ -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
}
105 changes: 105 additions & 0 deletions controllers/dday.ctrl.js
Original file line number Diff line number Diff line change
@@ -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
}
File renamed without changes.
Loading

0 comments on commit baa355f

Please sign in to comment.