Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 72 additions & 48 deletions database.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,84 @@
var sqlite3 = require('sqlite3').verbose()
var md5 = require('md5')
// database.js

const DBSOURCE = "db.sqlite"
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
console.error(err.message)
throw err
} else {
console.log('Connected to the SQlite database.')
db.run(`CREATE TABLE products (
if (err) {
console.error(err.message);
throw err;
} else {
console.log("Connected to the SQlite database.");

// Create products table
db.run(
`CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
productName text,
description text,
category text,
brand text,
expiredDate text,
manufacturedDate text,
productName TEXT,
description TEXT,
category TEXT,
brand TEXT,
expiredDate TEXT,
manufacturedDate TEXT,
batchNumber INTEGER,
unitPrice INTEGER,
quantity INTEGER,
createdDate text
)`, (err) => {
if (err) {
// Table already created
} 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"])
}
})

createdDate TEXT
)`,
(err) => {
if (err) {
console.error(err.message);
} else {
console.log('Table "products" created successfully.');
}
}
);

db.run(`CREATE TABLE suppliers (
// Create suppliers table
db.run(
`CREATE TABLE IF NOT EXISTS suppliers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
supplierName text,
address text,
joinedDate text,
mobileNo 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"])

}
})

supplierName TEXT,
address TEXT,
joinedDate TEXT,
mobileNo TEXT
)`,
(err) => {
if (err) {
console.error(err.message);
} else {
console.log('Table "suppliers" created successfully.');
}
}
);

// Create customer table
db.run(
`CREATE TABLE IF NOT EXISTS customer (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
address TEXT,
email TEXT,
dateOfBirth TEXT,
gender TEXT,
age INTEGER,
cardHolderName TEXT,
cardNumber TEXT,
expiryDate TEXT,
cvv TEXT,
timestamp TEXT
)`,
(err) => {
if (err) {
console.error(err.message);
} else {
console.log('Table "customer" created successfully.');
}
}
);
}
});

}
})

module.exports = db

module.exports = db;
85 changes: 85 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,91 @@ app.listen(HTTP_PORT, () => {
console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT))
});


// Customer Post Method
app.post("/api/customers/", (req, res, next) => {
try {
var errors = [];

// Validation
const {
name,
address,
email,
dateOfBirth,
gender,
age,
cardHolderName,
cardNumber,
expiryDate,
cvv,
timestamp,
} = req.body;

if (
!name ||
!address ||
!email ||
!dateOfBirth ||
!gender ||
!age ||
!cardHolderName ||
!cardNumber ||
!expiryDate ||
!cvv ||
!timestamp
) {
errors.push("All fields are required.");
}

// Validate email format
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
errors.push("Invalid email address.");
}

// Validate credit card number format (12 digits)
const cardNumberRegex = /^\d{12}$/;
if (!cardNumberRegex.test(cardNumber)) {
errors.push("Invalid credit card number. It should be 12 digits.");
}

if (errors.length > 0) {
res.status(400).json({ error: errors.join(", ") });
return;
}

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) {
res.status(400).json({ error: err.message });
return;
} else {
res.status(201).json({
message: `Customer ${name} has registered`,
customerId: this.lastID,
});
}
});
} catch (E) {
res.status(400).send(E);
}
});

app.get("/api/products", (req, res, next) => {
try {
var sql = "select * from products"
Expand Down