Adding support for data of entry
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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' });
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -113,6 +113,7 @@
|
||||
<th>Brand</th>
|
||||
<th>Model</th>
|
||||
<th>Status</th>
|
||||
<th>Date of entry</th>
|
||||
<th>State</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
@@ -122,6 +123,7 @@
|
||||
<input type="text" id="brand" name="brand" placeholder="Brand" 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="dateOfEntry" name="dateOfEntry" placeholder="Date of entry" required>
|
||||
<input type="text" id="state" name="state" placeholder="State" required>
|
||||
<button class="add-button" onclick="addComputer()">Add Computer</button>
|
||||
</div>
|
||||
@@ -139,7 +141,7 @@
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
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 => {
|
||||
const row = document.createElement('tr');
|
||||
row.innerHTML = `
|
||||
@@ -147,6 +149,7 @@
|
||||
<td>${computer.brand}</td>
|
||||
<td>${computer.model}</td>
|
||||
<td>${computer.status}</td>
|
||||
<td>${computer.dateOfEntry}</td>
|
||||
<td>${computer.state}</td>
|
||||
<td>
|
||||
<button class="edit-button" onclick="editComputer(${computer.id})">Edit</button>
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user