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

96 lines
2.6 KiB
TypeScript
Raw Normal View History

import { dataSource } from "../../../data-source";
2025-02-15 10:59:54 +01:00
import { webapi } from "../../../entity/management/webapi";
2025-01-29 09:42:22 +01:00
import DatabaseActionException from "../../../exceptions/databaseActionException";
import InternalException from "../../../exceptions/internalException";
2025-01-22 11:57:19 +01:00
import {
CreateWebapiCommand,
DeleteWebapiCommand,
UpdateLastUsageWebapiCommand,
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) => {
2025-01-29 09:42:22 +01:00
throw new DatabaseActionException("CREATE", "webapi", 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) => {
2025-01-29 09:42:22 +01:00
throw new DatabaseActionException("UPDATE", "webapi", err);
2025-01-22 11:57:19 +01:00
});
}
/**
* @description update api usage
* @param {UpdateLastUsageWebapiCommand} updateWebapi
* @returns {Promise<void>}
*/
static async updateUsage(updateWebapi: UpdateLastUsageWebapiCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(webapi)
.set({
lastUsage: new Date(),
})
.where("id = :id", { id: updateWebapi.id })
.execute()
.then(() => {})
.catch((err) => {
2025-01-29 09:42:22 +01:00
throw new DatabaseActionException("UPDATE", "webapi", 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) => {
2025-01-29 09:42:22 +01:00
throw new DatabaseActionException("DELETE", "webapi", err);
});
}
}