ff-admin-server/src/command/user/webapi/webapiCommandHandler.ts

70 lines
1.9 KiB
TypeScript
Raw Normal View History

import { dataSource } from "../../../data-source";
2025-01-22 09:39:31 +01:00
import { webapi } from "../../../entity/user/webapi";
import InternalException from "../../../exceptions/internalException";
2025-01-22 09:39:31 +01:00
import { CreateWebapiCommand, DeleteWebapiCommand, UpdateWebapiCommand } from "./webapiCommand";
2025-01-22 09:39:31 +01:00
export default abstract class WebapiCommandHandler {
/**
* @description create api
2025-01-22 09:39:31 +01:00
* @param {CreateWebapiCommand} createWebapi
* @returns {Promise<number>}
*/
2025-01-22 09:39:31 +01:00
static async create(createWebapi: CreateWebapiCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
2025-01-22 09:39:31 +01:00
.into(webapi)
.values({
2025-01-22 09:39:31 +01:00
token: createWebapi.token,
title: createWebapi.title,
expiry: createWebapi.expiry,
})
.execute()
.then((result) => {
return result.identifiers[0].token;
})
.catch((err) => {
throw new InternalException("Failed creating api", err);
});
}
/**
* @description update api
2025-01-22 09:39:31 +01:00
* @param {UpdateWebapiCommand} updateWebapi
* @returns {Promise<void>}
*/
2025-01-22 09:39:31 +01:00
static async update(updateWebapi: UpdateWebapiCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
2025-01-22 09:39:31 +01:00
.update(webapi)
.set({
2025-01-22 09:39:31 +01:00
title: updateWebapi.title,
expiry: updateWebapi.expiry,
})
2025-01-22 09:39:31 +01:00
.where("id = :id", { id: updateWebapi.id })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating api", err);
});
}
/**
* @description delete api
2025-01-22 09:39:31 +01:00
* @param {DeleteWebapiCommand} deleteWebapi
* @returns {Promise<void>}
*/
2025-01-22 09:39:31 +01:00
static async delete(deleteWebapi: DeleteWebapiCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.delete()
2025-01-22 09:39:31 +01:00
.from(webapi)
.where("id = :id", { id: deleteWebapi.id })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting api", err);
});
}
}