오세헌

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

...@@ -33,20 +33,55 @@ app.post('/hook', function (req, res) { ...@@ -33,20 +33,55 @@ app.post('/hook', function (req, res) {
33 33
34 db.connect(); 34 db.connect();
35 35
36 - db.query('SELECT * FROM data', function (error, results, fields) { 36 + db.query('SELECT * FROM recipe', function (error, results, fields) {
37 if (error) { 37 if (error) {
38 console.log(error); 38 console.log(error);
39 } 39 }
40 - var arr = new Array(); 40 + var arr1 = new Array();
41 + var arr2 = new Array();
41 var input_ingredients_list = message.text.split(" "); 42 var input_ingredients_list = message.text.split(" ");
43 + console.log(input_ingredients_list);
42 for (var i = 0; i < results.length; i++) { 44 for (var i = 0; i < results.length; i++) {
43 - var ingredients_list = results[i].ingredients.split(", "); 45 + var ingredients_list = results[i].ingrediant.split(",");
44 - if (ingredients_list.filter(x => input_ingredients_list.includes(x)).length === input_ingredients_list.length) { 46 + var count = ingredients_list.filter(x => input_ingredients_list.includes(x)).length;
45 - arr.push(i); 47 + if (count === input_ingredients_list.length) {
48 + arr1.push(i);
46 } 49 }
50 + else if (arr1.length === 0 && count != 0) {
51 + arr2.push(i);
52 + }
53 + }
54 + if (arr1.length != 0) {
55 + var num = arr1[Math.floor(Math.random() * arr1.length)];
56 + request.post(
57 + {
58 + url: TARGET_URL,
59 + headers: {
60 + 'Authorization': `Bearer ${TOKEN}`
61 + },
62 + json: {
63 + "replyToken": eventObj.replyToken,
64 + "messages": [
65 + {
66 + "type": "text",
67 + "text": `메뉴는 "${results[[num]].menu}" 입니다.`
68 + },
69 + {
70 + "type": "text",
71 + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
72 + },
73 + {
74 + "type": "text",
75 + "text": `레시피\n\n${results[[num]].recipe}`
76 + }
77 + ]
78 + }
79 + }, (error, response, body) => {
80 + console.log(body)
81 + });
47 } 82 }
48 - if (arr.length != 0) { 83 + else if (arr2.length != 0) {
49 - var num = arr[Math.floor(Math.random() * arr.length)]; 84 + var num = arr2[Math.floor(Math.random() * arr2.length)];
50 request.post( 85 request.post(
51 { 86 {
52 url: TARGET_URL, 87 url: TARGET_URL,
...@@ -62,7 +97,7 @@ app.post('/hook', function (req, res) { ...@@ -62,7 +97,7 @@ app.post('/hook', function (req, res) {
62 }, 97 },
63 { 98 {
64 "type": "text", 99 "type": "text",
65 - "text": `필요한 재료는\n\n"${results[[num]].ingredients}"\n\n입니다.` 100 + "text": `필요한 재료는\n\n"${results[[num]].ingrediant}"\n\n입니다.`
66 }, 101 },
67 { 102 {
68 "type": "text", 103 "type": "text",
...@@ -113,5 +148,4 @@ try { ...@@ -113,5 +148,4 @@ try {
113 } catch (error) { 148 } catch (error) {
114 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.'); 149 console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
115 console.log(error); 150 console.log(error);
116 -} 151 +}
117 -
...\ No newline at end of file ...\ No newline at end of file
......