Adding support for data of entry

This commit is contained in:
2023-12-06 11:20:49 +01:00
parent 5aab318e79
commit fcd3bfa0bd
4 changed files with 36 additions and 13 deletions

View File

@@ -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 };