diff --git a/database.js b/database.js index 43e3342..50ea20d 100644 --- a/database.js +++ b/database.js @@ -29,7 +29,7 @@ let db = new sqlite3.Database(DBSOURCE, (err) => { } else { // Table just created, creating some rows var insert = 'INSERT INTO products (productName, description, category, brand, expiredDate, manufacturedDate, batchNumber, unitPrice, quantity, createdDate) VALUES (?,?,?,?,?,?,?,?,?,?)' - db.run(insert, ["White Basmathi Rice", "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "Rice", "CIC", "2023.05.04", "2022.02.20", 324567, , 1020, 200, "2022.02.24"]) + db.run(insert, ["White Basmathi Rice", "White Basmathi Rice imported from Pakistan. High-quality rice with extra fragrance. Organically grown.", "Rice", "CIC", "2023.05.04", "2022.02.20", 324567, 1020, 200, "2022.02.24"]) } }) @@ -39,22 +39,52 @@ let db = new sqlite3.Database(DBSOURCE, (err) => { supplierName text, address text, joinedDate text, - mobileNo text + mobileNo text, + live text )`, (err) => { if (err) { // Table already created } else { // Table just created, creating some rows - var insert = 'INSERT INTO suppliers (supplierName, address, joinedDate, mobileNo) VALUES (?,?,?,?)' - db.run(insert, ["D.J.Ishara", "345A ,R.A De Mel Road, Colombo 3", "16/3/2022", "0776600933"]) + var insert = 'INSERT INTO suppliers (supplierName, address, joinedDate, mobileNo, live) VALUES (?,?,?,?,?)' + db.run(insert, ["D.J.Ishara", "345A ,R.A De Mel Road, Colombo 3", "16/3/2022", "0776600933", "tv"]) + } + + }) + + + + db.run(`CREATE TABLE customers ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name text, + address text, + emai text, + dateofBirth text, + gender text, + age text, + cardHolderName text, + expirydate text, + cvv text, + timeStamp text + )`, (err) => { + if (err) { + // Table already created + } else { + // Table just created, creating some rows + var insert = 'INSERT INTO customers (name, address, emai, dateOfBirth, gender, age, cardHolderName, cardNumber, expiryDate, cvv, timeStamp ) VALUES (?,?,?,?,?,?,?,?,?,?,?)' + db.run(insert, ["A.D.Lakith Dharmasiri", "No 324/A Ra De Mel Road,Colombo", "lakith@gmail.com", "1991.02.25", "female", "28", "A.D.L.Dharmasiri", "102445217895", "12/2022", "245", "2022-12-31 23:59:59"]) } }) + } }) module.exports = db + + + diff --git a/server.js b/server.js index 9e2f78d..c9efccf 100644 --- a/server.js +++ b/server.js @@ -242,11 +242,12 @@ app.post("/api/suppliers/", (req, res, next) => { const { supplierName, address, joinedDate, - mobileNo + mobileNo, + live } = req.body; - var sql = 'INSERT INTO suppliers (supplierName, address, joinedDate, mobileNo) VALUES (?,?,?,?)' - var params = [supplierName, address, joinedDate, mobileNo] + var sql = 'INSERT INTO suppliers (supplierName, address, joinedDate, mobileNo, live) VALUES (?,?,?,?,?)' + var params = [supplierName, address, joinedDate, mobileNo, live] db.run(sql, params, function (err, result) { if (err) { @@ -283,8 +284,58 @@ app.delete("/api/suppliers/deleteAll/:id", (req, res, next) => { } }); +app.post("/api/customers/", (req, res, next) => { + + try { + var errors = [] + + if (!req.body) { + errors.push("An invalid input"); + } + + const { + name, + address, + email, + dateOfBirth, + gender, + age, + cardHolderName, + cardNumber, + expiryDate, + cvv, + timeStamp, + } = req.body; + + var sql = 'INSERT INTO customers (name, address, email, dateOfBirth, gender, age, cardHolderName,cardNumber, expiryDate, cvv, timeStamp ) VALUES (?,?,?,?,?,?,?,?,?,?,?)' + var params = [name, address, email, dateOfBirth, gender, age, cardHolderName, cardNumber, expiryDate, cvv, timeStamp] + db.run(sql, params, function (err, result) { + + if (err) { + res.status(400).json({ "error": err.message }) + return; + } else { + res.json({ + "message": "success", + "data": req.body, + "id": this.lastID + }) + } + + }); + } catch (E) { + + res.status(400).send(E); + } +}); + + // Root path app.get("/", (req, res, next) => { res.json({ "message": "University of Moratuwa" }) -}); \ No newline at end of file +}); + + + +