FIrst commit
This commit is contained in:
109
Classes/Computer.js
Normal file
109
Classes/Computer.js
Normal file
@@ -0,0 +1,109 @@
|
||||
import { database } from './Database';
|
||||
|
||||
class Computer {
|
||||
constructor(id, brand, model, state, status) {
|
||||
this.id = id;
|
||||
this.brand = brand;
|
||||
this.model = model;
|
||||
this.status = status;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
async get (id) {
|
||||
try {
|
||||
const rows = await database.execute('SELECT * FROM computers WHERE id = ?', [id]);
|
||||
if (rows.length === 0) {
|
||||
throw new Error('Computer not found');
|
||||
} else {
|
||||
this.id = rows[0].id;
|
||||
this.brand = rows[0].brand;
|
||||
this.model = rows[0].model;
|
||||
this.status = rows[0].status;
|
||||
this.state = rows[0].state;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting computer:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create() {
|
||||
try {
|
||||
const 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);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
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]
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('Error updating computer:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete() {
|
||||
try {
|
||||
await database.execute('DELETE FROM computers WHERE id = ?', [this.id]);
|
||||
} catch (error) {
|
||||
console.error('Error deleting computer:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Computer list class
|
||||
class Computers extends Array {
|
||||
|
||||
async getAll() {
|
||||
try {
|
||||
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);
|
||||
this.push(computer);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error getting computers:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create(brand, model, state, status) {
|
||||
try {
|
||||
const computer = new Computer(null, brand, model, state, status);
|
||||
await computer.create();
|
||||
this.push(computer);
|
||||
} catch (error) {
|
||||
console.error('Error creating computer:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id) {
|
||||
try {
|
||||
const computerIndex = this.findIndex(computer => computer.id === id);
|
||||
if (computerIndex === -1) {
|
||||
throw new Error('Computer not found');
|
||||
}
|
||||
const computer = this[computerIndex];
|
||||
await computer.delete();
|
||||
this.splice(computerIndex, 1);
|
||||
} catch (error) {
|
||||
console.error('Error deleting computer:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export { Computer, Computers };
|
||||
52
Classes/Database.js
Normal file
52
Classes/Database.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
class Database {
|
||||
constructor(config) {
|
||||
this.pool = mysql.createPool(config);
|
||||
}
|
||||
|
||||
async query(sql, params) {
|
||||
const connection = await this.pool.getConnection();
|
||||
try {
|
||||
const [rows] = await connection.query(sql, params);
|
||||
return rows;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
async execute(sql, params) {
|
||||
const connection = await this.pool.getConnection();
|
||||
try {
|
||||
const [result] = await connection.execute(sql, params);
|
||||
return result;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
|
||||
async transaction(callback) {
|
||||
const connection = await this.pool.getConnection();
|
||||
try {
|
||||
await connection.beginTransaction();
|
||||
const result = await callback(connection);
|
||||
await connection.commit();
|
||||
return result;
|
||||
} catch (error) {
|
||||
await connection.rollback();
|
||||
throw error;
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const database = new Database({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASS,
|
||||
database: process.env.DB_NAME
|
||||
});
|
||||
|
||||
export { Database, database };
|
||||
Reference in New Issue
Block a user