ff-admin-server/src/command/unit/maintenanceCommandHandler.ts

72 lines
2.4 KiB
TypeScript

import { dataSource } from "../../data-source";
import { maintenance } from "../../entity/unit/maintenance";
import DatabaseActionException from "../../exceptions/databaseActionException";
import { CreateMaintenanceCommand, UpdateMaintenanceCommand, DeleteMaintenanceCommand } from "./maintenanceCommand";
export default abstract class MaintenanceCommandHandler {
/**
* @description create maintenance
* @param {CreateMaintenanceCommand} createMaintenance
* @returns {Promise<number>}
*/
static async create(createMaintenance: CreateMaintenanceCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
.into(maintenance)
.values({
status: "gestartet",
description: createMaintenance.description,
equipmentId: createMaintenance.affected == "equipment" ? createMaintenance.affectedId : null,
vehicleId: createMaintenance.affected == "vehicle" ? createMaintenance.affectedId : null,
wearableId: createMaintenance.affected == "wearable" ? createMaintenance.affectedId : null,
})
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
throw new DatabaseActionException("CREATE", "maintenance", err);
});
}
/**
* @description update maintenance
* @param {UpdateMaintenanceCommand} updateMaintenance
* @returns {Promise<void>}
*/
static async update(updateMaintenance: UpdateMaintenanceCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(maintenance)
.set({
status: updateMaintenance.status,
done: updateMaintenance.done,
description: updateMaintenance.description,
})
.where("id = :id", { id: updateMaintenance.id })
.execute()
.then(() => {})
.catch((err) => {
throw new DatabaseActionException("UPDATE", "maintenance", err);
});
}
/**
* @description delete maintenance
* @param {DeleteMaintenanceCommand} deleteMaintenance
* @returns {Promise<void>}
*/
static async delete(deleteMaintenance: DeleteMaintenanceCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.delete()
.from(maintenance)
.where("id = :id", { id: deleteMaintenance.id })
.execute()
.then(() => {})
.catch((err) => {
throw new DatabaseActionException("DELETE", "maintenance", err);
});
}
}