Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add API_cluster #1

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
62 changes: 35 additions & 27 deletions server/controller/cluster.js
Original file line number Diff line number Diff line change
@@ -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,
}
6 changes: 3 additions & 3 deletions server/routes/clusters.js
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down