diff --git a/Classes/Computer.js b/Classes/Computer.js index 6da1cd3..4d70b3d 100644 --- a/Classes/Computer.js +++ b/Classes/Computer.js @@ -1,11 +1,12 @@ import { database } from './Database'; class Computer { - constructor(id, brand, model, state, status) { + constructor(id, brand, model, state, date, status) { this.id = id; this.brand = brand; this.model = model; this.status = status; + this.dateOfEntry = date; this.state = state; } @@ -19,6 +20,7 @@ class Computer { this.brand = rows[0].brand; this.model = rows[0].model; this.status = rows[0].status; + this.dateOfEntry = rows[0].dateOfEntry; this.state = rows[0].state; } } catch (error) { @@ -29,10 +31,18 @@ class Computer { async create() { try { - const result = await database.execute( - 'INSERT INTO computers (brand, model, state, status) VALUES (?, ?, ?, ?)', - [this.brand, this.model, this.state, this.status] + let result + if (this.dateOfEntry != null) { + result = await database.execute( + 'INSERT INTO computers (brand, model, state, dateOfEntry, status) VALUES (?, ?, ?, ?, ?)', + [this.brand, this.model, this.state, this.dateOfEntry, this.status] ); + } else { + result = await database.execute( + 'INSERT INTO computers (brand, model, state, status) VALUES (?, ?, ?, ?)', + [this.brand, this.model, this.state, this.status] + ); + } this.id = result.insertId; } catch (error) { console.error('Error creating computer:', error); @@ -43,8 +53,8 @@ class Computer { async update() { try { await database.execute( - 'UPDATE computers SET brand = ?, model = ?, state = ?, status = ? WHERE id = ?', - [this.brand, this.model, this.state, this.status, this.id] + 'UPDATE computers SET brand = ?, model = ?, state = ?, dateOfEntry = ?, status = ? WHERE id = ?', + [this.brand, this.model, this.state, this.dateOfEntry, this.status, this.id] ); } catch (error) { console.error('Error updating computer:', error); @@ -70,7 +80,7 @@ class Computers extends Array { const rows = await database.execute('SELECT * FROM computers'); this.length = 0; // Clear the existing array rows.forEach(row => { - const computer = new Computer(row.id, row.brand, row.model, row.state, row.status); + const computer = new Computer(row.id, row.brand, row.model, row.state, formatDate(row.dateOfEntry), row.status); this.push(computer); }); } catch (error) { @@ -106,4 +116,8 @@ class Computers extends Array { } } +function formatDate(date) { + return new Date(date - new Date(date).getTimezoneOffset() * 60 * 1000).toISOString().split('T')[0]; +} + export { Computer, Computers }; diff --git a/Routes/computer.js b/Routes/computer.js index 03a409c..bad0170 100644 --- a/Routes/computer.js +++ b/Routes/computer.js @@ -8,7 +8,6 @@ router.get('/', async (req, res) => { const computers = new Computers(); await computers.getAll(); try { - res.status(200).json(computers); } catch (error) { res.status(500).json({ error: 'Internal Server Error' }); @@ -34,10 +33,11 @@ router.get('/:id', async (req, res) => { router.post('/', async (req, res) => { try { const { brand, model, state, status } = req.body; - if (!brand || !model || !state || !status) { + if ((!brand || brand == "") || (!model || model == "" ) || (!state || state == "" ) || (!status || status == "" )) { res.status(400).json({ error: 'Bad Request' }); } - const computer = new Computer(null, brand, model, state, status); + const dateOfEntry = req.body.dateOfEntry || null; + const computer = new Computer(null, brand, model, state, dateOfEntry, status); await computer.create(); res.status(201).json({ message: 'Computer created successfully' }); } catch (error) { @@ -53,10 +53,11 @@ router.put('/:id', async (req, res) => { if (computer.brand === null) { res.status(404).json({ error: 'Computer not found' }); } - const { brand, model, state, status } = req.body; + const { brand, model, state, dateOfEntry, status } = req.body; computer.brand = brand; computer.model = model; computer.state = state; + computer.dateOfEntry = dateOfEntry; computer.status = status; await computer.update(); res.status(200).json({ message: 'Computer updated successfully' }); diff --git a/database.sql b/database.sql index d303611..11df766 100644 --- a/database.sql +++ b/database.sql @@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS computers ( brand VARCHAR(255) NOT NULL, model VARCHAR(255) NOT NULL, status VARCHAR(255) NOT NULL, + dateOfEntry DATE NOT NULL DEFAULT CURRENT_DATE, state VARCHAR(255) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; diff --git a/public/c/index.html b/public/c/index.html index 1da48e5..d0f229e 100644 --- a/public/c/index.html +++ b/public/c/index.html @@ -113,6 +113,7 @@ Brand Model Status + Date of entry State Actions @@ -122,6 +123,7 @@ + @@ -139,7 +141,7 @@ .then(response => response.json()) .then(data => { const table = document.querySelector('.computer-table'); - table.innerHTML = 'IDBrandModelStatusStateActions'; + table.innerHTML = 'IDBrandModelStatusDate of entryStateActions'; data.forEach(computer => { const row = document.createElement('tr'); row.innerHTML = ` @@ -147,6 +149,7 @@ ${computer.brand} ${computer.model} ${computer.status} + ${computer.dateOfEntry} ${computer.state} @@ -227,7 +230,8 @@ brand: values[0], model: values[1], status: values[2], - state: values[3] + dateOfEntry: values[3], + state: values[4] }) }) .then(response => { @@ -278,6 +282,7 @@ const brand = document.getElementById('brand').value; const model = document.getElementById('model').value; const status = document.getElementById('status').value; + const dateOfEntry = document.getElementById('dateOfEntry').value; const state = document.getElementById('state').value; // Send a POST request to add the computer @@ -291,6 +296,7 @@ brand: brand, model: model, status: status, + dateOfEntry: dateOfEntry, state: state }) }) @@ -300,6 +306,7 @@ document.getElementById('brand').value = ''; document.getElementById('model').value = ''; document.getElementById('status').value = ''; + document.getElementById('dateOfEntry').value = ''; document.getElementById('state').value = ''; } else { alert('Failed to add computer');