오세헌

if we don't have a recipe even though there's no type

......@@ -33,20 +33,55 @@ app.post('/hook', function (req, res) {
db.connect();
db.query('SELECT * FROM data', function (error, results, fields) {
db.query('SELECT * FROM recipe', function (error, results, fields) {
if (error) {
console.log(error);
}
var arr = new Array();
var arr1 = new Array();
var arr2 = new Array();
var input_ingredients_list = message.text.split(" ");
console.log(input_ingredients_list);
for (var i = 0; i < results.length; i++) {
var ingredients_list = results[i].ingredients.split(", ");
if (ingredients_list.filter(x => input_ingredients_list.includes(x)).length === input_ingredients_list.length) {
arr.push(i);
var ingredients_list = results[i].ingrediant.split(",");
var count = ingredients_list.filter(x => input_ingredients_list.includes(x)).length;
if (count === input_ingredients_list.length) {
arr1.push(i);
}
else if (arr1.length === 0 && count != 0) {
arr2.push(i);
}
}
if (arr1.length != 0) {
var num = arr1[Math.floor(Math.random() * arr1.length)];
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken": eventObj.replyToken,
"messages": [
{
"type": "text",
"text": `메뉴는 "${results[[num]].menu}" 입니다.`
},
{
"type": "text",
"text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
},
{
"type": "text",
"text": `레시피\n\n${results[[num]].recipe}`
}
]
}
}, (error, response, body) => {
console.log(body)
});
}
if (arr.length != 0) {
var num = arr[Math.floor(Math.random() * arr.length)];
else if (arr2.length != 0) {
var num = arr2[Math.floor(Math.random() * arr2.length)];
request.post(
{
url: TARGET_URL,
......@@ -62,7 +97,7 @@ app.post('/hook', function (req, res) {
},
{
"type": "text",
"text": `필요한 재료는\n\n"${results[[num]].ingredients}"\n\n입니다.`
"text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
},
{
"type": "text",
......@@ -113,5 +148,4 @@ try {
} catch (error) {
console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
console.log(error);
}
}
\ No newline at end of file
......