Adding support for data of entry
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
import { database } from './Database';
|
import { database } from './Database';
|
||||||
|
|
||||||
class Computer {
|
class Computer {
|
||||||
constructor(id, brand, model, state, status) {
|
constructor(id, brand, model, state, date, status) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.brand = brand;
|
this.brand = brand;
|
||||||
this.model = model;
|
this.model = model;
|
||||||
this.status = status;
|
this.status = status;
|
||||||
|
this.dateOfEntry = date;
|
||||||
this.state = state;
|
this.state = state;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,6 +20,7 @@ class Computer {
|
|||||||
this.brand = rows[0].brand;
|
this.brand = rows[0].brand;
|
||||||
this.model = rows[0].model;
|
this.model = rows[0].model;
|
||||||
this.status = rows[0].status;
|
this.status = rows[0].status;
|
||||||
|
this.dateOfEntry = rows[0].dateOfEntry;
|
||||||
this.state = rows[0].state;
|
this.state = rows[0].state;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -29,10 +31,18 @@ class Computer {
|
|||||||
|
|
||||||
async create() {
|
async create() {
|
||||||
try {
|
try {
|
||||||
const result = await database.execute(
|
let result
|
||||||
'INSERT INTO computers (brand, model, state, status) VALUES (?, ?, ?, ?)',
|
if (this.dateOfEntry != null) {
|
||||||
[this.brand, this.model, this.state, this.status]
|
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;
|
this.id = result.insertId;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating computer:', error);
|
console.error('Error creating computer:', error);
|
||||||
@@ -43,8 +53,8 @@ class Computer {
|
|||||||
async update() {
|
async update() {
|
||||||
try {
|
try {
|
||||||
await database.execute(
|
await database.execute(
|
||||||
'UPDATE computers SET brand = ?, model = ?, state = ?, status = ? WHERE id = ?',
|
'UPDATE computers SET brand = ?, model = ?, state = ?, dateOfEntry = ?, status = ? WHERE id = ?',
|
||||||
[this.brand, this.model, this.state, this.status, this.id]
|
[this.brand, this.model, this.state, this.dateOfEntry, this.status, this.id]
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error updating computer:', error);
|
console.error('Error updating computer:', error);
|
||||||
@@ -70,7 +80,7 @@ class Computers extends Array {
|
|||||||
const rows = await database.execute('SELECT * FROM computers');
|
const rows = await database.execute('SELECT * FROM computers');
|
||||||
this.length = 0; // Clear the existing array
|
this.length = 0; // Clear the existing array
|
||||||
rows.forEach(row => {
|
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);
|
this.push(computer);
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} 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 };
|
export { Computer, Computers };
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ router.get('/', async (req, res) => {
|
|||||||
const computers = new Computers();
|
const computers = new Computers();
|
||||||
await computers.getAll();
|
await computers.getAll();
|
||||||
try {
|
try {
|
||||||
|
|
||||||
res.status(200).json(computers);
|
res.status(200).json(computers);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
res.status(500).json({ error: 'Internal Server Error' });
|
res.status(500).json({ error: 'Internal Server Error' });
|
||||||
@@ -34,10 +33,11 @@ router.get('/:id', async (req, res) => {
|
|||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { brand, model, state, status } = req.body;
|
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' });
|
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();
|
await computer.create();
|
||||||
res.status(201).json({ message: 'Computer created successfully' });
|
res.status(201).json({ message: 'Computer created successfully' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -53,10 +53,11 @@ router.put('/:id', async (req, res) => {
|
|||||||
if (computer.brand === null) {
|
if (computer.brand === null) {
|
||||||
res.status(404).json({ error: 'Computer not found' });
|
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.brand = brand;
|
||||||
computer.model = model;
|
computer.model = model;
|
||||||
computer.state = state;
|
computer.state = state;
|
||||||
|
computer.dateOfEntry = dateOfEntry;
|
||||||
computer.status = status;
|
computer.status = status;
|
||||||
await computer.update();
|
await computer.update();
|
||||||
res.status(200).json({ message: 'Computer updated successfully' });
|
res.status(200).json({ message: 'Computer updated successfully' });
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ CREATE TABLE IF NOT EXISTS computers (
|
|||||||
brand VARCHAR(255) NOT NULL,
|
brand VARCHAR(255) NOT NULL,
|
||||||
model VARCHAR(255) NOT NULL,
|
model VARCHAR(255) NOT NULL,
|
||||||
status VARCHAR(255) NOT NULL,
|
status VARCHAR(255) NOT NULL,
|
||||||
|
dateOfEntry DATE NOT NULL DEFAULT CURRENT_DATE,
|
||||||
state VARCHAR(255) NOT NULL,
|
state VARCHAR(255) NOT NULL,
|
||||||
PRIMARY KEY (id)
|
PRIMARY KEY (id)
|
||||||
) ENGINE=InnoDB;
|
) ENGINE=InnoDB;
|
||||||
|
|||||||
@@ -113,6 +113,7 @@
|
|||||||
<th>Brand</th>
|
<th>Brand</th>
|
||||||
<th>Model</th>
|
<th>Model</th>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
|
<th>Date of entry</th>
|
||||||
<th>State</th>
|
<th>State</th>
|
||||||
<th>Actions</th>
|
<th>Actions</th>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -122,6 +123,7 @@
|
|||||||
<input type="text" id="brand" name="brand" placeholder="Brand" required>
|
<input type="text" id="brand" name="brand" placeholder="Brand" required>
|
||||||
<input type="text" id="model" name="model" placeholder="Model" required>
|
<input type="text" id="model" name="model" placeholder="Model" required>
|
||||||
<input type="text" id="status" name="status" placeholder="Status" required>
|
<input type="text" id="status" name="status" placeholder="Status" required>
|
||||||
|
<input type="text" id="dateOfEntry" name="dateOfEntry" placeholder="Date of entry" required>
|
||||||
<input type="text" id="state" name="state" placeholder="State" required>
|
<input type="text" id="state" name="state" placeholder="State" required>
|
||||||
<button class="add-button" onclick="addComputer()">Add Computer</button>
|
<button class="add-button" onclick="addComputer()">Add Computer</button>
|
||||||
</div>
|
</div>
|
||||||
@@ -139,7 +141,7 @@
|
|||||||
.then(response => response.json())
|
.then(response => response.json())
|
||||||
.then(data => {
|
.then(data => {
|
||||||
const table = document.querySelector('.computer-table');
|
const table = document.querySelector('.computer-table');
|
||||||
table.innerHTML = '<tr><th>ID</th><th>Brand</th><th>Model</th><th>Status</th><th>State</th><th>Actions</th></tr>';
|
table.innerHTML = '<tr><th>ID</th><th>Brand</th><th>Model</th><th>Status</th><th>Date of entry</th><th>State</th><th>Actions</th></tr>';
|
||||||
data.forEach(computer => {
|
data.forEach(computer => {
|
||||||
const row = document.createElement('tr');
|
const row = document.createElement('tr');
|
||||||
row.innerHTML = `
|
row.innerHTML = `
|
||||||
@@ -147,6 +149,7 @@
|
|||||||
<td>${computer.brand}</td>
|
<td>${computer.brand}</td>
|
||||||
<td>${computer.model}</td>
|
<td>${computer.model}</td>
|
||||||
<td>${computer.status}</td>
|
<td>${computer.status}</td>
|
||||||
|
<td>${computer.dateOfEntry}</td>
|
||||||
<td>${computer.state}</td>
|
<td>${computer.state}</td>
|
||||||
<td>
|
<td>
|
||||||
<button class="edit-button" onclick="editComputer(${computer.id})">Edit</button>
|
<button class="edit-button" onclick="editComputer(${computer.id})">Edit</button>
|
||||||
@@ -227,7 +230,8 @@
|
|||||||
brand: values[0],
|
brand: values[0],
|
||||||
model: values[1],
|
model: values[1],
|
||||||
status: values[2],
|
status: values[2],
|
||||||
state: values[3]
|
dateOfEntry: values[3],
|
||||||
|
state: values[4]
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.then(response => {
|
.then(response => {
|
||||||
@@ -278,6 +282,7 @@
|
|||||||
const brand = document.getElementById('brand').value;
|
const brand = document.getElementById('brand').value;
|
||||||
const model = document.getElementById('model').value;
|
const model = document.getElementById('model').value;
|
||||||
const status = document.getElementById('status').value;
|
const status = document.getElementById('status').value;
|
||||||
|
const dateOfEntry = document.getElementById('dateOfEntry').value;
|
||||||
const state = document.getElementById('state').value;
|
const state = document.getElementById('state').value;
|
||||||
|
|
||||||
// Send a POST request to add the computer
|
// Send a POST request to add the computer
|
||||||
@@ -291,6 +296,7 @@
|
|||||||
brand: brand,
|
brand: brand,
|
||||||
model: model,
|
model: model,
|
||||||
status: status,
|
status: status,
|
||||||
|
dateOfEntry: dateOfEntry,
|
||||||
state: state
|
state: state
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -300,6 +306,7 @@
|
|||||||
document.getElementById('brand').value = '';
|
document.getElementById('brand').value = '';
|
||||||
document.getElementById('model').value = '';
|
document.getElementById('model').value = '';
|
||||||
document.getElementById('status').value = '';
|
document.getElementById('status').value = '';
|
||||||
|
document.getElementById('dateOfEntry').value = '';
|
||||||
document.getElementById('state').value = '';
|
document.getElementById('state').value = '';
|
||||||
} else {
|
} else {
|
||||||
alert('Failed to add computer');
|
alert('Failed to add computer');
|
||||||
|
|||||||
Reference in New Issue
Block a user