143 lines
4.1 KiB
TypeScript
143 lines
4.1 KiB
TypeScript
import pool from '../config/database';
|
|
import logger from '../utils/logger';
|
|
|
|
class FahrzeugTypService {
|
|
async getAll() {
|
|
try {
|
|
const result = await pool.query(
|
|
`SELECT * FROM fahrzeug_typen ORDER BY name ASC`
|
|
);
|
|
return result.rows;
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypService.getAll failed', { error });
|
|
throw new Error('Fahrzeug-Typen konnten nicht geladen werden');
|
|
}
|
|
}
|
|
|
|
async getById(id: number) {
|
|
try {
|
|
const result = await pool.query(
|
|
`SELECT * FROM fahrzeug_typen WHERE id = $1`,
|
|
[id]
|
|
);
|
|
return result.rows[0] || null;
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypService.getById failed', { error, id });
|
|
throw new Error('Fahrzeug-Typ konnte nicht geladen werden');
|
|
}
|
|
}
|
|
|
|
async create(data: { name: string; beschreibung?: string; icon?: string }) {
|
|
try {
|
|
const result = await pool.query(
|
|
`INSERT INTO fahrzeug_typen (name, beschreibung, icon)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING *`,
|
|
[data.name, data.beschreibung ?? null, data.icon ?? null]
|
|
);
|
|
return result.rows[0];
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypService.create failed', { error });
|
|
throw new Error('Fahrzeug-Typ konnte nicht erstellt werden');
|
|
}
|
|
}
|
|
|
|
async update(id: number, data: { name?: string; beschreibung?: string; icon?: string }) {
|
|
try {
|
|
const setClauses: string[] = [];
|
|
const values: any[] = [];
|
|
let idx = 1;
|
|
|
|
if (data.name !== undefined) {
|
|
setClauses.push(`name = $${idx}`);
|
|
values.push(data.name);
|
|
idx++;
|
|
}
|
|
if (data.beschreibung !== undefined) {
|
|
setClauses.push(`beschreibung = $${idx}`);
|
|
values.push(data.beschreibung);
|
|
idx++;
|
|
}
|
|
if (data.icon !== undefined) {
|
|
setClauses.push(`icon = $${idx}`);
|
|
values.push(data.icon);
|
|
idx++;
|
|
}
|
|
|
|
if (setClauses.length === 0) {
|
|
return this.getById(id);
|
|
}
|
|
|
|
values.push(id);
|
|
const result = await pool.query(
|
|
`UPDATE fahrzeug_typen SET ${setClauses.join(', ')} WHERE id = $${idx} RETURNING *`,
|
|
values
|
|
);
|
|
return result.rows[0] || null;
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypService.update failed', { error, id });
|
|
throw new Error('Fahrzeug-Typ konnte nicht aktualisiert werden');
|
|
}
|
|
}
|
|
|
|
async delete(id: number) {
|
|
try {
|
|
const result = await pool.query(
|
|
`DELETE FROM fahrzeug_typen WHERE id = $1 RETURNING *`,
|
|
[id]
|
|
);
|
|
return result.rows[0] || null;
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypService.delete failed', { error, id });
|
|
throw new Error('Fahrzeug-Typ konnte nicht gelöscht werden');
|
|
}
|
|
}
|
|
|
|
async getTypesForVehicle(fahrzeugId: string) {
|
|
try {
|
|
const result = await pool.query(
|
|
`SELECT ft.* FROM fahrzeug_typen ft
|
|
JOIN fahrzeug_fahrzeug_typen fft ON fft.fahrzeug_typ_id = ft.id
|
|
WHERE fft.fahrzeug_id = $1
|
|
ORDER BY ft.name ASC`,
|
|
[fahrzeugId]
|
|
);
|
|
return result.rows;
|
|
} catch (error) {
|
|
logger.error('FahrzeugTypService.getTypesForVehicle failed', { error, fahrzeugId });
|
|
throw new Error('Fahrzeug-Typen konnten nicht geladen werden');
|
|
}
|
|
}
|
|
|
|
async setTypesForVehicle(fahrzeugId: string, typIds: number[]) {
|
|
const client = await pool.connect();
|
|
try {
|
|
await client.query('BEGIN');
|
|
|
|
await client.query(
|
|
`DELETE FROM fahrzeug_fahrzeug_typen WHERE fahrzeug_id = $1`,
|
|
[fahrzeugId]
|
|
);
|
|
|
|
for (const typId of typIds) {
|
|
await client.query(
|
|
`INSERT INTO fahrzeug_fahrzeug_typen (fahrzeug_id, fahrzeug_typ_id) VALUES ($1, $2)`,
|
|
[fahrzeugId, typId]
|
|
);
|
|
}
|
|
|
|
await client.query('COMMIT');
|
|
|
|
return this.getTypesForVehicle(fahrzeugId);
|
|
} catch (error) {
|
|
await client.query('ROLLBACK').catch(() => {});
|
|
logger.error('FahrzeugTypService.setTypesForVehicle failed', { error, fahrzeugId });
|
|
throw new Error('Fahrzeug-Typen konnten nicht gesetzt werden');
|
|
} finally {
|
|
client.release();
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new FahrzeugTypService();
|