Commit bbd62e12 authored by Zhai Zirun's avatar Zhai Zirun

query function returns presence of specific ingredient in product

parent 645df31b
......@@ -2,13 +2,20 @@
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
};
function query(product) {
function query(product, ingredient) {
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./public.db', sqlite3.OPEN_READONLY, (err) => {
if (err) {
console.error(err.message);
console.error('Error connecting to database');
}
console.log('Connected to the in-memory SQlite database.');
});
......@@ -22,13 +29,28 @@ function query(product) {
db.get(sql, product, (err, row) => {
if (err) {
return console.error(err.message);
return console.error('Error querying database');
}
if (!row) {
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 row
? console.log(row.cg)
: console.log(`No product found with the name ${product}`);
});
db.close();
}
......
const Ingredient = {
GLUTEN: 1,
DAIRY: 2,
CORN: 3,
MEAT: 4
};
const sqlite3 = require('sqlite3').verbose();
let db = new sqlite3.Database('./public.db', sqlite3.OPEN_READONLY, (err) => {
......@@ -7,6 +15,8 @@ const sqlite3 = require('sqlite3').verbose();
console.log('Connected to the in-memory SQlite database.');
});
let ingredient = Ingredient.GLUTEN;
let product = 'Waitrose Millionaire Bites'
let sql = `SELECT Containsgluten cg,
Containsdairy cd,
......@@ -20,8 +30,30 @@ const sqlite3 = require('sqlite3').verbose();
if (err) {
return console.error(err.message);
}
return row
? console.log(row.cg)
: console.log(`No product found with the name ${product}`);
switch(ingredient) {
case Ingredient.GLUTEN:
return row
? console.log(row.cg)
: console.log(`No product found with the name ${product}`);
case Ingredient.DAIRY:
return row
? console.log(row.cd)
: console.log(`No product found with the name ${product}`);
case Ingredient.CORN:
return row
? console.log(row.cc)
: console.log(`No product found with the name ${product}`);
case Ingredient.MEAT:
return row
? console.log(row.cm)
: console.log(`No product found with the name ${product}`);
default:
break;
}
});
db.close();
\ No newline at end of file
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log('Database connection closed.');
});
\ No newline at end of file
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