Showing
1 changed file
with
52 additions
and
10 deletions
... | @@ -57,21 +57,63 @@ app.post('/food/recommendation', function(req, res) { | ... | @@ -57,21 +57,63 @@ app.post('/food/recommendation', function(req, res) { |
57 | var price = req.body.price; | 57 | var price = req.body.price; |
58 | var shape = req.body.shape; | 58 | var shape = req.body.shape; |
59 | var kinds = req.body.kinds; | 59 | var kinds = req.body.kinds; |
60 | - var list = { | ||
61 | - food: [] | ||
62 | - }; | ||
63 | 60 | ||
64 | - function add(users, price, shape, kinds, list, callback) { | 61 | + if (!Array.isArray(shape) && shape) { |
62 | + var shape = []; | ||
63 | + shape.push(req.body.shape); | ||
64 | + } | ||
65 | + if (!Array.isArray(kinds) && kinds) { | ||
66 | + var kinds = []; | ||
67 | + kinds.push(req.body.kinds); | ||
68 | + } | ||
69 | + // var len_shape=shape.length; | ||
70 | + // var len_kinds=kinds.length; | ||
71 | + | ||
72 | + var set1 = new Set(); | ||
73 | + var set2 = new Set(); | ||
74 | + var set3 = new Set(); | ||
75 | + | ||
76 | + function add(users, price, shape, kinds, callback) { | ||
65 | for (var foods in users) { | 77 | for (var foods in users) { |
66 | - if ((!price || users[foods]['price'] == price) && (!shape || users[foods]['shape'] == shape) && (!kinds || users[foods]['kinds'] == kinds)) { | 78 | + if (shape) { |
67 | - list.food.push(foods); | 79 | + for (var i = 0; i < shape.length; i++) { |
80 | + if (users[foods]['shape'] == shape[i]) { | ||
81 | + set1.add(foods); | ||
82 | + } | ||
83 | + } | ||
84 | + } else { | ||
85 | + set1.add(foods); | ||
86 | + } | ||
87 | + if (kinds) { | ||
88 | + for (var j = 0; j < kinds.length; j++) { | ||
89 | + if (users[foods]['kinds'] == kinds[j]) { | ||
90 | + set2.add(foods); | ||
91 | + } | ||
92 | + } | ||
93 | + } else { | ||
94 | + set2.add(foods); | ||
95 | + } | ||
96 | + if (!price || (price&&users[foods]['price'] <= Number(price)+5000 && users[foods]['price'] >= Number(price)-5000)) { | ||
97 | + set3.add(foods); | ||
98 | + console.log(users[foods]['price']); | ||
68 | } | 99 | } |
69 | - // $('<li>').text(foods).appendTo('#users'); | ||
70 | } | 100 | } |
71 | - callback(list); | 101 | + console.log(shape); |
102 | + console.log(kinds); | ||
103 | + console.log(set1); | ||
104 | + console.log(set2); | ||
105 | + console.log(set3); | ||
106 | + // console.log('shapeleng:' + shape.length); | ||
107 | + // console.log('kinds leng:' + kinds.length); | ||
108 | + callback(set1, set2, set3); | ||
72 | } | 109 | } |
73 | 110 | ||
74 | - add(users, price, shape, kinds, list, function(list) { | 111 | + add(users, price, shape, kinds, function(set1, set2, set3) { |
112 | + let difference1 = new Set([...set1].filter(i => set2.has(i))); | ||
113 | + let difference2 = new Set([...difference1].filter(i => set3.has(i))); | ||
114 | + var list = { | ||
115 | + food: [...difference2] | ||
116 | + }; | ||
75 | fs.writeFile('data/temp.json', JSON.stringify(list), "utf8", function(err) { | 117 | fs.writeFile('data/temp.json', JSON.stringify(list), "utf8", function(err) { |
76 | if (err) { | 118 | if (err) { |
77 | console.log(err); | 119 | console.log(err); |
... | @@ -97,7 +139,7 @@ app.post('/food/recommendation', function(req, res) { | ... | @@ -97,7 +139,7 @@ app.post('/food/recommendation', function(req, res) { |
97 | }); //readFile closed | 139 | }); //readFile closed |
98 | } | 140 | } |
99 | }); //writeFile closed | 141 | }); //writeFile closed |
100 | - });//add closed | 142 | + }); //add closed |
101 | 143 | ||
102 | } | 144 | } |
103 | }); //readFile closed | 145 | }); //readFile closed | ... | ... |
-
Please register or login to post a comment