diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml new file mode 100644 index 0000000..6cdebaf --- /dev/null +++ b/.github/workflows/npm-publish.yml @@ -0,0 +1,33 @@ +# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created +# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages + +name: Node.js Package + +on: + release: + types: [created] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + - run: npm ci + - run: npm test + + publish-npm: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 16 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.npm_token}} diff --git a/database.js b/database.js index 43e3342..24113c6 100644 --- a/database.js +++ b/database.js @@ -1,9 +1,6 @@ var sqlite3 = require('sqlite3').verbose() var md5 = require('md5') - const DBSOURCE = "db.sqlite" - - let db = new sqlite3.Database(DBSOURCE, (err) => { if (err) { // Cannot open database @@ -37,9 +34,21 @@ let db = new sqlite3.Database(DBSOURCE, (err) => { db.run(`CREATE TABLE suppliers ( id INTEGER PRIMARY KEY AUTOINCREMENT, supplierName text, + db.run(`CREATE TABLE customer ( + customerId INTEGER PRIMARY KEY AUTOINCREMENT, + name text, address text, joinedDate text, mobileNo text + email text, + dateOfBirth text, + gender text, + age INTEGER, + cardHolderName text, + cardNumber INTEGER, + expiryDate text, + cvv INTERGER, + timeStamp text )`, (err) => { if (err) { // Table already created @@ -48,6 +57,8 @@ let db = new sqlite3.Database(DBSOURCE, (err) => { 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 customer (name, address, email, dateOfBirth, gender, age, cardHolderName, cardNumber, expiryDate, cvv,timeStamp) VALUES (?,?,?,?,?,?,?,?,?,?,?)' + db.run(insert, ["Jayantha De Silva", "345, Galle Road, Colombo 4", "customer2022@gmail.com", "2000.04.14", "Male", 22, "Laxshman Perera", 102030405060, "12/2023",888,"2022-12-31 23:59:59"]) } }) @@ -57,4 +68,3 @@ let db = new sqlite3.Database(DBSOURCE, (err) => { }) module.exports = db - diff --git a/server.js b/server.js index 9e2f78d..3a65808 100644 --- a/server.js +++ b/server.js @@ -6,7 +6,6 @@ var bodyParser = require("body-parser"); const { request, response } = require("express"); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); - let HTTP_PORT = 8080 const cors = require('cors'); app.use(cors({ @@ -125,6 +124,21 @@ app.post("/api/products/", (req, res, next) => { var sql = 'INSERT INTO products (productName, description, category, brand, expiredDate, manufacturedDate, batchNumber, unitPrice, quantity, createdDate) VALUES (?,?,?,?,?,?,?,?,?,?)' var params = [productName, description, category, brand, expiredDate, manufacturedDate, batchNumber, unitPrice, quantity, createdDate] + const { name, + address, + email, + dateOfBirth, + gender, + age, + cardHolderName, + cardNumber, + expiryDate, + cvv, + timeStamp + } = req.body; + + var sql = 'INSERT INTO customer (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) { @@ -133,14 +147,14 @@ app.post("/api/products/", (req, res, next) => { } else { res.json({ "message": "success", + "message": "customer $name has registered", "data": req.body, "id": this.lastID + "customerId": this.lastID }) } - }); - } catch (E) { - res.status(400).send(E); +@@ -144,26 +60,24 @@ app.post("/api/products/", (req, res, next) => { } }); @@ -160,73 +174,58 @@ app.put("/api/products/", (req, res, next) => { unitPrice, quantity, createdDate + customerId, + name, + address, + email, + dateOfBirth, + gender, + age, + cardHolderName, + cardNumber, + expiryDate, + cvv, + timeStamp } = req.body; db.run(`UPDATE products set productName = ?, description = ?, category = ?, brand = ?,expiredDate=?,manufacturedDate=?,batchNumber=?,unitPrice=?,quantity=?,createdDate=? WHERE id = ?`, [productName, description, category, brand, expiredDate, manufacturedDate, batchNumber, unitPrice, quantity, createdDate, id], + db.run(`UPDATE customer. set name = ?, address = ?, email = ?, dateOfBirth = ?, gender=?,age=?,cardHolderName=?,cardNumber=?,expiryDate=?,cvv=?,timeStamp=? WHERE customerId = ?`, + [name,address,email,dateOfBirth,gender,age,cardHolderName,cardNumber,expiryDate,cvv,timeStamp,customerId], function (err, result) { if (err) { res.status(400).json({ "error": res.message }) - return; - } - res.status(200).json({ updated: this.changes }); - }); -}); - - +@@ -177,7 +91,7 @@ app.put("/api/products/", (req, res, next) => { app.delete("/api/products/delete/:id", (req, res, next) => { try { db.run( 'DELETE FROM products WHERE id = ?', + 'DELETE FROM customer WHERE customerId = ?', req.params.id, function (err, result) { if (err) { - res.status(400).json({ "error": res.message }) - return; - } - res.json({ "message": "deleted", rows: this.changes }) - }); - } catch (E) { - res.status(400).send(E); - } -}); - +@@ -194,7 +108,7 @@ app.delete("/api/products/delete/:id", (req, res, next) => { app.delete("/api/products/deleteAll/:id", (req, res, next) => { try { db.run( 'DELETE FROM products WHERE id > ?', + 'DELETE FROM customer WHERE customerId = ?', req.params.id, function (err, result) { if (err) { - res.status(400).json({ "error": res.message }) - return; - } - res.json({ "message": "deleted", rows: this.changes }) - }); - } catch (E) { - res.status(400).send(E); - } +@@ -209,9 +123,9 @@ app.delete("/api/products/deleteAll/:id", (req, res, next) => { }); app.get("/api/suppliers/", (req, res, next) => { +app.get("/api/products/", (req, res, next) => { try { var sql = "select * from suppliers" + var sql = "select * from customer" var params = [] db.all(sql, params, (err, rows) => { if (err) { - res.status(400).json({ "error": err.message }); - return; - } - res.json({ - "message": "success", - "data": rows - }) - }); - } catch (E) { - res.status(400).send(E); - } - +@@ -230,61 +144,7 @@ app.get("/api/suppliers/", (req, res, next) => { }); @@ -287,4 +286,5 @@ app.delete("/api/suppliers/deleteAll/:id", (req, res, next) => { // Root path app.get("/", (req, res, next) => { res.json({ "message": "University of Moratuwa" }) -}); \ No newline at end of file +}); +});