Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Add: 이모티콘 업로드 기능 테스트
Browse files Browse the repository at this point in the history
  • Loading branch information
leehj050211 committed Jan 29, 2022
1 parent 49f1398 commit 1b1a695
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 8 deletions.
51 changes: 46 additions & 5 deletions src/controllers/api/emoticon.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const jwt = require('../../jwt')
const model = require('../../models/emoticon')
const multer = require('multer')

let result, dbResult;
const getemoticon = async (req, res) => {
const jwtValue = jwt.check(req.cookies.token);
if(!jwtValue.isLogin){res.send(JSON.stringify(jwtValue.msg));return;}
dbResult = await model.getemoticon(req.params.id)
result = {
status:1,
Expand All @@ -14,8 +13,6 @@ const getemoticon = async (req, res) => {
res.send(JSON.stringify(result))
}
const getemoticons = async (req, res) => {
const jwtValue = jwt.check(req.cookies.token);
if(!jwtValue.isLogin){res.send(JSON.stringify(jwtValue.msg));return;}
dbResult = await model.getemoticons()
result = {
status:1,
Expand All @@ -24,8 +21,52 @@ const getemoticons = async (req, res) => {
}
res.send(JSON.stringify(result))
}
const uploadProcessing = multer({
storage:multer.diskStorage({
destination:(req, file, cb) => {
cb(null, 'public/resource/board/emoticon/temp')
},
filename:(req, file, cb) => {
const name = file.originalname.split('.')[0];
const ext = file.originalname.split('.')[file.originalname.split('.').length-1];
file.name = name;
file.ext = ext;
cb(null, `${name}.${ext}`)
}
})
})
const uploadCheck = async (req, res) => {
const jwtValue = jwt.check(req.cookies.token);
if(!jwtValue.isLogin){res.send(JSON.stringify(jwtValue.msg));return;}

// 업로드 데이터 체크
if(!req.body.name || !req.body.description || !req.files.file || !req.files.files || !req.body.emoticons){res.send(JSON.stringify({status:3,subStatus:0}));return;}
if(req.body.name.length<2){res.send(JSON.stringify({status:3,subStatus:0}));return;}
if(req.body.description.length<2){res.send(JSON.stringify({status:3,subStatus:0}));return;}
if(req.files.length<2){res.send(JSON.stringify({status:3,subStatus:0}));return;}
req.body.emoticons = JSON.parse(req.body.emoticons)
if(req.files.files.length != (Object.keys(req.body.emoticons).length)){res.send(JSON.stringify({status:3,subStatus:0}));return;}
for(let i=0;i<req.files.files.length;i++){
const e = req.files.files[i];
if(!req.body.emoticons[e.name]){res.send(JSON.stringify({status:3,subStatus:0}));return;}
if(e.ext != req.body.emoticons[e.name].type){
res.send(JSON.stringify({status:3,subStatus:0}));
return;
}
}
res.send(JSON.stringify({status:1,subStatus:0}));return;
// dbResult = await model.uploadEmoticonInfo(req.body.name, req.body.description, jwtValue.memberCode)
// if(!dbResult){
// res.send(JSON.stringify({status:2,subStatus:0}));return;
// }else{
// req.body.id=dbResult;
// next();
// }
}

module.exports = {
getemoticon:getemoticon,
getemoticons:getemoticons
getemoticons:getemoticons,
uploadProcessing:uploadProcessing,
uploadCheck:uploadCheck
}
37 changes: 36 additions & 1 deletion src/models/emoticon.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,42 @@ const getemoticons = async () => {
}
return result
}

const uploadEmoticonInfo = async (name, description, memberCode) => {
let rows
const getIndexQuery = `
SELECT AUTO_INCREMENT
FROM information_schema.tables
WHERE table_name = 'emoticon'
AND table_schema = DATABASE()`
try{
[rows] = await pool.query(getIndexQuery)
}catch(err){
console.error(err)
return null;
}
const insertEmoticonsQuery = "INSERT INTO emoticon values(?, ?, ?, now(), ?)"
try{
pool.query(insertEmoticonsQuery, [rows[0].AUTO_INCREMENT], name, description, memberCode)
}catch(err){
console.error(err)
return null;
}
return rows[0].AUTO_INCREMENT
}
const uploadEmoticon = (id, idx, type) => {
const insertEmoticonsQuery = "INSERT INTO emoticons values(?, ?, ?)"
try{
pool.query(insertEmoticonsQuery, id, idx, type)
}catch(err){
console.error(err)
return null;
}
return true
}
module.exports = {
getemoticon:getemoticon,
getemoticons:getemoticons
getemoticons:getemoticons,
uploadEmoticonInfo:uploadEmoticonInfo,
uploadEmoticon:uploadEmoticon
}
5 changes: 3 additions & 2 deletions src/routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ router.post('/like/:boardType/:postNo', likeController.like)

router.post('/imageUpload', loginCheck, imageUpload.single('file'), imageUploadController.upload)

router.get('/emoticon/:id', emoticonController.getemoticon)
router.get('/emoticon', emoticonController.getemoticons)
router.get('/emoticon/:id', loginCheck, emoticonController.getemoticon)
router.get('/emoticon', loginCheck, emoticonController.getemoticons)
router.post('/emoticon', loginCheck, emoticonController.uploadProcessing.fields([{name:'file'},{name:'files'}]), emoticonController.uploadCheck)

module.exports = router
15 changes: 15 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,20 @@ router.get('/pwReset', (req ,res) => {
}
})
})
router.get('/emoticon', (req ,res) => {
const jwtValue = jwt.check(req.cookies.token);
res.render('emoticon', {
member:{
isLogin:jwtValue.isLogin,
code:jwtValue.memberCode,
id:jwtValue.memberId,
nickname:jwtValue.memberNickname,
level:jwtValue.memberLevel,
grade:jwtValue.grade,
classNo:jwtValue.classNo,
studentNo:jwtValue.studentNo,
}
})
})

module.exports = router;

0 comments on commit 1b1a695

Please sign in to comment.