From 31ae3b99bb35d03c4ae44bcd652e550982cc817d Mon Sep 17 00:00:00 2001 From: geun9716 Date: Mon, 14 Jun 2021 21:36:32 +0900 Subject: [PATCH] add API_cluster --- server/app.js | 2 +- server/controller/cluster.js | 62 ++++++++++++++++++++---------------- server/routes/clusters.js | 6 ++-- server/routes/index.js | 4 ++- 4 files changed, 42 insertions(+), 32 deletions(-) diff --git a/server/app.js b/server/app.js index 231426f..6efa01f 100644 --- a/server/app.js +++ b/server/app.js @@ -5,7 +5,7 @@ var cookieParser = require('cookie-parser'); var logger = require('morgan'); var cors = require('cors'); -var indexRouter = require('./routes/index'); +import indexRouter from './routes/index'; var app = express(); diff --git a/server/controller/cluster.js b/server/controller/cluster.js index bd812fd..f3b1bcc 100644 --- a/server/controller/cluster.js +++ b/server/controller/cluster.js @@ -1,59 +1,67 @@ import db from '../middleware/db' import httpStatus from 'http-status-codes' import security from '../middleware/security' -//Date to yyyy-mm-dd -function getFormatDate(date){ - var year = date.getFullYear(); //yyyy - var month = (1 + date.getMonth()); //M - month = month >= 10 ? month : '0' + month; //month 두자리로 저장 - var day = date.getDate(); //d - day = day >= 10 ? day : '0' + day; //day 두자리로 저장 - return year + '-' + month + '-' + day; //'-' 추가하여 yyyy-mm-dd 형태 생성 가능 -} - // 게시글 조회 -async function getAll (req, res) { - console.log(req.query); - +async function getClusterAll (req, res) { try { - let SQLresult = await db.query('select * from News limit 20;'); - - if(newsInfo.length > 0){ + let result = await db.query('select * from Clusters;'); + const clusterInfo = []; + + + if(result.length > 0){ + result.map((val)=>{ + clusterInfo.push({ + cId : val.cId, + count : val.count, + Topic : val.Topic.replace(/\[|\]|\'| /g, "").split(',').slice(0,5), + summary : val.summary, + img : val.img, + }) + }); const returnObj = { - SQLresult : SQLresult + clusterInfo : clusterInfo, } res.status(httpStatus.OK).send(returnObj) } else{ res.status(httpStatus.NOT_FOUND).send() } } catch(error) { - console.error(error, "posts api error") + console.error(error, "clusters api error") res.status(httpStatus.INTERNAL_SERVER_ERROR).send([]) } } -async function getCluster (req, res) { - let id = req.params.id; +async function getClusterId (req, res) { + let cId = req.params.cId; try { - let newsInfo = await db.query('select * from News where postID = ?', [id]); + let result = await db.query('select * from Clusters where cId = ?', [cId]); + const clusterInfo = []; - if(postInfo.length > 0){ + if(result.length > 0){ + result.map((val)=>{ + clusterInfo.push({ + cId : val.cId, + count : val.count, + Topic : val.Topic.replace(/\[|\]|\'| /g, "").split(','), + summary : val.summary, + img : val.img, + }) + }); const returnObj = { - newsInfo : newsInfo + clusterInfo : clusterInfo, } res.status(httpStatus.OK).send(returnObj) } else{ res.status(httpStatus.NOT_FOUND).send() } } catch(error) { - console.error(error, "posts api error") + console.error(error, "clusters api error") res.status(httpStatus.INTERNAL_SERVER_ERROR).send([]) } } - export default { - getAll, - getCluster, + getClusterAll, + getClusterId, } \ No newline at end of file diff --git a/server/routes/clusters.js b/server/routes/clusters.js index c2ef049..aefe68b 100644 --- a/server/routes/clusters.js +++ b/server/routes/clusters.js @@ -1,12 +1,12 @@ +import cluster from '../controller/cluster.js'; var express = require('express'); -import cluster from '../controller/cluster' const router = express.Router(); // 클러스터와 클러스터 단어 5개씩 있는 것을 전송 -router.get('/cluster/', cluster.getAll) +router.get('/', cluster.getClusterAll); // 클러스터 id와 클러스터 WordCloud, 클러스터 개수, 20개씩 전달 -router.get('/cluster/:clusterId', cluster.get); +router.get('/:cId', cluster.getClusterId); export default router \ No newline at end of file diff --git a/server/routes/index.js b/server/routes/index.js index de8df6d..1f5d74e 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -3,10 +3,12 @@ var router = express.Router(); import postRouter from './posts.js'; - +import clusterRouter from './clusters.js'; // router.use('/user', users); router.use('/post', postRouter); +router.use('/cluster', clusterRouter); + /* GET home page. */