Commit c6c30f46 authored by Zhai Zirun's avatar Zhai Zirun

query now queries according to diet number and not individual ingredient

parent 0d817c90
......@@ -2,15 +2,21 @@
var MOUSE_VISITED_CLASSNAME = 'crx_mouse_visited_true';
var MOUSE_VISITED_CLASSNAME_FALSE = 'crx_mouse_visited_false';
const Ingredient = {
GLUTEN: 1,
DAIRY: 2,
CORN: 3,
MEAT: 4
};
var srcName;
var requestResult;
var dietNum = 0;
function dietNumInit() {
var a = document.getElementById("Dairy").checked;
var b = document.getElementById("Meat").checked;
var c = document.getElementById("Corn").checked;
var d = document.getElementById("Gluten").checked;
dietNum = a + (b << 1) + (c << 2) + (d << 3);
}
function query(product, ingredient) {
function query(product) {
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./public.db', sqlite3.OPEN_READONLY, (err) => {
......@@ -20,10 +26,10 @@ function query(product, ingredient) {
console.log('Connected to the in-memory SQlite database.');
});
let sql = `SELECT Containsgluten cg,
Containsdairy cd,
let sql = `SELECT Containsdairy cd,
Containsmeat cm,
Containscorn cc,
Containsmeat cm
Containsgluten cg
FROM productsIngredients
WHERE Product = ?`;
......@@ -37,43 +43,12 @@ function query(product, ingredient) {
return console.error(`No product found with the name ${product}`);
}
switch(ingredient) {
case Ingredient.GLUTEN:
return row.cg;
case Ingredient.DAIRY:
return row.cd;
case Ingredient.CORN:
return row.cc;
case Ingredient.MEAT:
return row.cm;
default:
break;
}
return !((cd && (1 && dietNum)) || (cm && (2 && dietNum)) || (cc && (4 && dietNum)) || (cg && (8 && dietNum)))
});
db.close();
}
var srcName;
var dietNum = 0;
var requestResult;
// var currName;
//Querying database on heroku
// import { Client } from 'pg';
// const client = new Client({
// connectionString: process.env.DATABASE_URL, //default name for main database is DATABASE_URL
// ssl: {
// rejectUnauthorized: false
// }
// });
//https://www.w3schools.com/nodejs/nodejs_mysql_select.asp
function callBack(canEat) {
if (canEat == undefined) {
requestResult = true;
......@@ -161,30 +136,3 @@ if(document){
document.getElementById("Submit").addEventListener("click", myFunction);
});
}
function myFunction() {
var a = document.getElementById("Dairy").checked;
var b = document.getElementById("Meat").checked;
var c = document.getElementById("Corn").checked;
var d = document.getElementById("Gluten").checked;
dietNum = a + (b << 1) + (c << 2) + (d << 3);
}
// function checkAllergens() {
// client.connect();
// //TODO: find a way to connect to client at startup (le??) to remove the need to connect and disconnect repeatedly
// //TODO: fill in names of columns of db
// client.query('SELECT Containsgluten, Containsdairy, Containscorn, Containsmeat FROM productsIngredients WHERE Product = currName;', (err, res) => {
// if (err) throw err;
// for (let col of res.cols) {
// if (col) {
// return col;
// }
// //returns true if any allergen is present.
// //TODO: modify behaviour according to different allergens
// }
// client.end();
// return false;
// });
// }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment