server.js 7.54 KB
const express = require('express');
const app = express();
const PORT = process.env.PORT || 4000;

const db = require('./config/db');


app.get('/api/host', (req, res) => {
    res.send({ host : 'Rekrow' });
})

app.get('/api/home_most_epic', (req, res) => {
    db.query("SELECT * FROM (SELECT ROW_NUMBER() over(order by temp.value desc)NUM, PICTURE,NAME, VALUE FROM (SELECT itemId, count(itemId) AS VALUE, date(droped_at) FROM item_drop_exception WHERE date(droped_at) = DATE(NOW()) GROUP BY itemid) temp join item_exception ie ON ie.itemId = temp.itemid) t WHERE t.NUM <=5",    (err, data) => {
        if(!err) {
           res.send(data);
        } else {
            console.log(err);
            res.send(err);
        }
    })
})
app.get('/api/home_most_channel', (req, res) => {
    db.query("SELECT * from(SELECT * , ROW_NUMBER() over(order by temp.value desc)NUM from(select channel_name, channel_number, count(itemId) AS VALUE FROM item_drop_exception WHERE date(droped_at) = DATE(NOW()) GROUP BY CHANNEL_Name, Channel_NUMBER) temp) t WHERE num<=5",
    (err, data) => {
        if(!err) {
           res.send(data);
        } else {
            console.log(err);
            res.send(err);
        }
    })
})

app.get('/api/most_channel', (req, res) => {
    db.query("SELECT channel_name, channel_number, count(itemId) AS VALUE, ROW_NUMBER() OVER (ORDER BY value desc) as NUM FROM item_drop_exception WHERE date(droped_at) = DATE(NOW()) GROUP BY CHANNEL_Name, Channel_NUMBER order BY VALUE desc",
    (err, data) => {
        if(!err) {
           res.send(data);
        } else {
            console.log(err);
            res.send(err);
        }
    })
})
app.get('/api/week_most_channel', (req, res) => {
    db.query("SELECT *, ROW_NUMBER() OVER (ORDER BY T.value desc) as NUM FROM(SELECT channel_name, channel_number,COUNT(id) AS VALUE FROM item_drop_exception WHERE droped_at BETWEEN DATE_ADD(NOW(),INTERVAL - 1 week) AND NOW() GROUP BY CHANNEL_NAME, CHANNEL_NUMBER) T",
    (err, data) => {        
        if(!err) {
           res.send(data);
        } else {
            console.log(err);
            res.send(err);
        }
    })
})

app.get('/api/most_epic', (req, res) => {
    db.query("SELECT NAME, PICTURE,VALUE, ROW_NUMBER() OVER (ORDER BY value desc) as NUM FROM (SELECT itemId, count(itemId) AS VALUE, date(droped_at) FROM item_drop_exception WHERE date(droped_at) = DATE(NOW()) GROUP BY itemid) temp join item_exception ie ON ie.itemId = temp.itemid",   
    (err, data) => {
        if(!err) {
           res.send(data);
        } else {
            console.log(err);
            res.send(err);
        }
    })
})

app.get('/api/week_most_epic', (req, res) => {
    db.query("SELECT NAME, PICTURE,VALUE, ROW_NUMBER() OVER (ORDER BY T.value desc) as NUM FROM(SELECT itemId, COUNT(itemId) AS VALUE FROM item_drop_exception WHERE droped_at BETWEEN DATE_ADD(NOW(),INTERVAL - 1 week) AND NOW() GROUP BY itemId) T JOIN item_exception i ON i.itemId = T.itemId",
    (err, data) => {      
        if(!err) {
           res.send(data);
        } else {
            console.log(err);
            res.send(err);
        }
    })
})

//search data
app.get('/api/search_data/:searchTag', (req, res) => {
   const searchTag = req.params.searchTag;
   //console.log(searchTag);
   res.json({searchTag});

})

app.get('/api/getImage/:searchName', (req, res) => {
    const searchName =req.params.searchName;
    let id;
    let valueSql = `SELECT itemId as keyValue, name, picture FROM item_exception WHERE NAME LIKE '${searchName}'`;
    db.query(valueSql,searchName,(err, data) => { 
        if(!err) {
            res.send(data);
         } else {
             console.log(err);
             res.send(err);
         }   
    })
})


app.get('/api/search_epic1/:searchName', (req, res) => {
    const searchName =req.params.searchName;
    let id;
    let valueSql = `SELECT itemId as keyValue FROM item_exception WHERE NAME LIKE '${searchName}'`;
    db.query(valueSql,searchName,(err, data) => {    
        if(!err) {
          id = data[0].keyValue;
           let resultSql =`SELECT T.channel_name, T.channel_number, T.VALUE, T.num from(SELECT itemId, channel_name, channel_number, COUNT(*) AS VALUE, ROW_NUMBER() over(order by value DESC) NUM FROM item_drop_exception WHERE  itemId = (SELECT itemId FROM item_exception WHERE itemid = '${id}') AND date(droped_at) = DATE(NOW()) GROUP BY channel_name, channel_number) T WHERE T.NUM<=5 ORDER BY T.NUM` 
           //console.log("id: "+ id);
           db.query( resultSql,id,(err, data) => {      
               if(!err) {
                  //console.log("data : " + data)
                  res.send(data);
               } else {
                   //console.log(err);
                   res.send(err);
               }
           })
        } else {
            //console.log(err);
            res.send(err);
        }
    })
})

//원그래프 용 쿼리
app.get('/api/search_epic2/:searchName', (req, res) => {
    const searchName =req.params.searchName;
    let id;
    let valueSql = `SELECT itemId as keyValue FROM item_exception WHERE NAME LIKE '${searchName}'`;
    db.query(valueSql,searchName,(err, data) => {    
        if(!err) {
           id=data[0].keyValue;
           console.log("dataLog2: "+ JSON.stringify(data))  
           var resultSql =`SELECT T.channel_name, T.channel_number, T.VALUE, T.num from(SELECT itemId, channel_name, channel_number, COUNT(*) AS VALUE, ROW_NUMBER() over(order by value DESC) NUM FROM item_drop_exception WHERE  itemId = (SELECT itemId FROM item_exception WHERE itemid = '${id}') AND date(droped_at) = DATE(NOW()) GROUP BY channel_name, channel_number) T WHERE T.NUM<=7 ORDER BY T.NUM` 
           db.query( resultSql,id,(err, data) => {      
               if(!err) {
                  res.send(data);
               } else {
                   console.log(err);
                   res.send(err);
               }
           })
        } else {
            //console.log(err);
            res.send(err);
        }
    })
   
})

app.get('/api/search_epic3/:searchName', (req, res) => {
    const searchName =req.params.searchName;
    var id;
    var i =0;
    var list1 = [];
    var valueSql = `SELECT itemId as keyValue FROM item_exception WHERE NAME LIKE '${searchName}'`;
    db.query(valueSql,searchName,(err, data) => {      
        if(!err) {
           id=data[0].keyValue;
            //반복문으로 진행?
            while(i<7)
            {
                //console.log("iterator: "+i)
                var resultSql1 =`SELECT T.channel_name, T.channel_number, T.VALUE, T.num from(SELECT itemId, channel_name, channel_number, COUNT(*) AS VALUE, ROW_NUMBER() over(order by value DESC) NUM FROM item_drop_exception WHERE  itemId = (SELECT itemId FROM item_exception WHERE itemid = '${id}') AND date(droped_at) = date(date_add(now(),interval -${i} DAY)) GROUP BY channel_name, channel_number) T WHERE T.NUM<=3 ORDER BY T.num` 
                db.query( resultSql1,id,(err, result) => {      
                if(!err) {
                    list1.push(result);
                    console.log(i+"번째 리스트 push, 내용: "+JSON.stringify(result));
                    
                    if(list1.length==7)
                    {
                        res.send(list1);
                    }
                } 
                else {
                    // console.log(err);
                    res.send(err);
                }
                })
                i++;
            }
        } else {
            console.log(err);
            res.send(err);
        }
    })
})


app.listen(PORT, () => {
    console.log(`Server On : http://localhost:${PORT}/`);
})