search.js 4.01 KB
var template = require('./template.js');
var qs = require('querystring');
var db = require('./database.js');

exports.foodInfo = function (request, response) {
    var title = '식품 영양정보';
    var description = '입력한 음식의 영양 정보(칼로리, 단백질, 지방, 탄수화물, 나트륨)를 알려드립니다!';
    var html = template.html(title, `
        <div style= "font-size:25px; background-color:#ffffff">
        <h2><font color="black">${title}<div style= "font-size:10px;"><br><div style= "font-size:20px;">${description}</font></div></h2>
        <form action="/foodInfo_search" method="post">
            <input type = 'text' name = 'search' placeholder = '검색어 입력' maxlength = 255 value = "" autocomplete = "off" style="width:300px;height:20px;font-size:20px;">
            <button type = "submit" name = "click" style="font-size:20px;">검색</button>
        </form>
        </div>
    
    `);
    response.writeHead(200);
    response.end(html);
}
function foodTable(foodInfo, list, num) {
    var tag = '';
    tag += `<table style='width:100%;'`;
    tag += ` 
        <tr>
            <td>이름</td>
            <td>종류</td>
            <td>지역/제조사</td>
            <td>1회 제공량(g)</td>
            <td>칼로리(kcal)</td>
            <td>단백질(g)</td>
            <td>지방(g)</td>
            <td>탄수화물(g)</td>
            <td>나트륨(mg)</td>
        </tr>
    `
    for (var i = 0; i < num; i++) {
        tag += `
                <tr>
                    <td>${foodInfo[list[i]].name}</td>
                    <td>${foodInfo[list[i]].type}</td>
                    <td>${foodInfo[list[i]].company}</td>
                    <td>${foodInfo[list[i]].servingSize}</td>
                    <td>${foodInfo[list[i]].kcal}</td>
                    <td>${foodInfo[list[i]].protein}</td>
                    <td>${foodInfo[list[i]].fat}</td>
                    <td>${foodInfo[list[i]].carbohydrate}</td>
                    <td>${foodInfo[list[i]].natrium}</td>
           </tr>
           `
    }
    tag += `</table>
    <style>
        table{
            border-collapse: collapse;
        }
        td{
            border:1px solid black;
        }
    </style>
    `;
    return tag;
}



exports.foodInfo_search = function (request, response) {
    var body = '';
    request.on('data', function (data) {
        body = body + data;
    });
    request.on('end', function () {
        var post = qs.parse(body);
        db.query(`SELECT * FROM fooddb`, function (error, foodInfo) {
            var title = '식품 영양정보';
            var description = '입력한 음식의 영양 정보(칼로리, 단백질, 지방, 탄수화물, 나트륨)를 알려드립니다!';
            var search = post.search; // 검색어
            var num = 0; // 검색된 개수
            var foodName = '';
            var list = [];
            //console.log(foodInfo[0]);
            for (var i = 0; i < foodInfo.length; i++) {
                foodName = foodInfo[i].name;
                if (foodName.indexOf(search) >= 0) {
                    list.push(i);
                    num += 1;
                }
            }
            var html = template.html(title, `
            <div style= "font-size:25px; background-color:#ffffff">
            <h2><font color="black">${title}<div style= "font-size:10px;"><br><div style= "font-size:20px;">${description}</font></div></h2>
            <form action="/foodInfo_search" method="post">
                <input type = 'text' name = 'search' placeholder = '검색어 입력' maxlength = 255 value = "" autocomplete = "off" style="width:300px;height:20px;font-size:20px;">
                <button type = "submit" name = "click" style="font-size:20px;">검색</button>
            </form>
            <p style= "font-size:20px;">${search}(으)로 검색된 결과 : ${num}개</p>
            </div>
            ${foodTable(foodInfo, list, num)}
              <br><br>
            `);
            response.writeHead(200);
            response.end(html);

        });
    });
}