-
Notifications
You must be signed in to change notification settings - Fork 9
/
route-handler.js
128 lines (116 loc) · 4 KB
/
route-handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var db = require('./db/config');
var User = require('./db/models/users');
var Ingredient = require('./db/models/ingredients');
var path = require('path');
var vision = require('@google-cloud/vision')({
keyFilename: 'key.json',
projectId: 'ingredients2020-176919'
});
//createUser api route
exports.findOrCreateUser = function(req, res) {
var email = req.body.email;
User.findOne({username: email})
.exec(function(err, user) {
// if user doesn't exist, create a new user
if(!user) {
var newUser = new User({username: email})
newUser.save((err, user) => {
if (err) {
console.log('user not found and not saved!')
} else {
res.status(201).send(user.username);
}
});
} else if (user){
res.status(200).send(user.username);
} else {
res.sendStatus(400);
}
});
}
//ingredient search api route
exports.ingredients = function(req, res) {
var ingredient = req.body.data.ingredient;
var username = req.body.data.username;
Ingredient.findOne({name: ingredient})
.exec(function(err, ingredientObj) {
//if there is an ingredient, return the document JSON, on the front end, we can extrapolate the name and link!
if (!ingredientObj) {
res.status(401).send(`${ingredient} not in database`);
} else {
User.findOneAndUpdate({username: username}, {"$push": {"pastSearches": ingredientObj}})
.exec(function(err, user) {
if (err) {
throw err;
} else {
console.log(user);
}
})
res.json(ingredientObj);
}
});
};
//get past searches api route
exports.pastSearches = function(req, res) {
// var userID = req.body.data.userID;
var username = req.body.data.username;
User.findOne({username: username})
.exec(function (err, user) {
if (!user) {
res.status(401).send('user not found in database');
} else {
res.send(user.pastSearches);
}
})
}
exports.googleCloudSearch = function(req, res) {
var buf = new Buffer(req.body.data_uri.replace(/^data:image\/\w+;base64,/, ""),'base64');
vision.textDetection({ content: buf }, function(err, apiResponse) {
if(err) {
res.end('Cloud Vision Error:', err);
} else {
var detections = apiResponse.fullTextAnnotation.text;
var arrayOfIngredients = [];
var ingredientsArray = detections.replace(/\n/g, ' ').replace(/\./g, ',').toLowerCase().split(', ');
var toxicIngredients = [];
const numberOfIngredients = ingredientsArray.length;
var counter = 0;
ingredientsArray.forEach(function(ingredient, index) {
Ingredient.findOne({name: ingredient},
function(err, ingredientObj) {
//if there is an ingredient, return the document JSON, on the front end, we can extrapolate the name and link!
counter++;
if (err) {
//res.status(401).send(`${ingredient} not in database`);
console.log('ERROR:' + err);
} else if(ingredientObj) {
console.log(ingredientObj);
toxicIngredients.push(ingredientObj);
}
if (counter === numberOfIngredients-1){
var filenameObj = {
name: req.body.filename,
link: 'Below are the flagged ingredients from this image'
}
toxicIngredients.unshift(filenameObj);
//console.log(toxicIngredients);
if(toxicIngredients.slice(1).length > 0){
User.findOneAndUpdate({username: req.body.username}, {"$push": {"pastSearches": toxicIngredients}})
.exec(function(err, user) {
if (err) {
throw err;
} else {
//console.log(user);
}
})
}
res.json(toxicIngredients);
}
});
})
}
})
}
exports.callback = function(req, res) {
res.sendFile(path.join(__dirname, 'build/index.html'));
}