ff-admin-server/src/controller/admin/unit/wearableController.ts

138 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-05-31 15:12:16 +02:00
import { Request, Response } from "express";
import WearableService from "../../../service/unit/wearable/wearableService";
import WearableFactory from "../../../factory/admin/unit/wearable/wearable";
import {
CreateWearableCommand,
DeleteWearableCommand,
UpdateWearableCommand,
} from "../../../command/unit/wearable/wearableCommand";
import WearableCommandHandler from "../../../command/unit/wearable/wearableCommandHandler";
/**
* @description get all wearables
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getAllWearables(req: Request, res: Response): Promise<any> {
let offset = parseInt((req.query.offset as string) ?? "0");
let count = parseInt((req.query.count as string) ?? "25");
let search = (req.query.search as string) ?? "";
let noLimit = req.query.noLimit === "true";
let ids = ((req.query.ids ?? "") as string).split(",").filter((i) => i);
2025-06-02 13:14:09 +02:00
let [wearables, total] = await WearableService.getAll({ offset, count, search, noLimit, ids });
2025-05-31 15:12:16 +02:00
res.json({
wearables: WearableFactory.mapToBase(wearables),
2025-05-31 15:12:16 +02:00
total: total,
offset: offset,
count: count,
});
}
/**
* @description get wearable by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getWearableById(req: Request, res: Response): Promise<any> {
const wearableId = req.params.id;
let wearable = await WearableService.getById(wearableId);
res.json(WearableFactory.mapToSingle(wearable));
}
/**
* @description get wearable by Ids
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function getWearablesByIds(req: Request, res: Response): Promise<any> {
let ids = req.body.ids as Array<string>;
let [wearables, total] = await WearableService.getAll({ noLimit: true, ids });
res.json({
wearables: WearableFactory.mapToBase(wearables),
total: total,
offset: 0,
count: total,
});
}
2025-05-31 15:12:16 +02:00
/**
* @description create wearable
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function createWearable(req: Request, res: Response): Promise<any> {
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const wearableTypeId = req.body.wearableTypeId;
const wearerId = req.body.wearerId || null;
2025-05-31 15:12:16 +02:00
let createWearable: CreateWearableCommand = {
code,
name,
location,
commissioned,
wearableTypeId,
wearerId,
2025-05-31 15:12:16 +02:00
};
let wearableId = await WearableCommandHandler.create(createWearable);
res.status(200).send(wearableId);
}
/**
* @description update wearable by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function updateWearableById(req: Request, res: Response): Promise<any> {
const wearableId = req.params.id;
const name = req.body.name;
const code = req.body.code || null;
const location = req.body.location;
const commissioned = req.body.commissioned;
const decommissioned = req.body.decommissioned || null;
const wearerId = req.body.wearerId || null;
2025-05-31 15:12:16 +02:00
let updateWearable: UpdateWearableCommand = {
id: wearableId,
code,
name,
location,
commissioned,
decommissioned,
wearerId,
2025-05-31 15:12:16 +02:00
};
await WearableCommandHandler.update(updateWearable);
res.sendStatus(204);
}
/**
* @description delete wearable by id
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function deleteWearableById(req: Request, res: Response): Promise<any> {
const wearableId = req.params.id;
let deleteWearable: DeleteWearableCommand = {
id: wearableId,
};
await WearableCommandHandler.delete(deleteWearable);
res.sendStatus(204);
}