2025-01-21 11:37:28 +01:00
|
|
|
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";
|
2025-01-21 11:37:28 +01:00
|
|
|
import InternalException from "../../../exceptions/internalException";
|
2025-01-22 11:57:19 +01:00
|
|
|
import {
|
|
|
|
CreateWebapiCommand,
|
|
|
|
DeleteWebapiCommand,
|
|
|
|
UpdateLastUsageWebapiCommand,
|
|
|
|
UpdateWebapiCommand,
|
|
|
|
} from "./webapiCommand";
|
2025-01-21 11:37:28 +01:00
|
|
|
|
2025-01-22 09:39:31 +01:00
|
|
|
export default abstract class WebapiCommandHandler {
|
2025-01-21 11:37:28 +01:00
|
|
|
/**
|
|
|
|
* @description create api
|
2025-01-22 09:39:31 +01:00
|
|
|
* @param {CreateWebapiCommand} createWebapi
|
2025-01-21 11:37:28 +01:00
|
|
|
* @returns {Promise<number>}
|
|
|
|
*/
|
2025-01-22 09:39:31 +01:00
|
|
|
static async create(createWebapi: CreateWebapiCommand): Promise<number> {
|
2025-01-21 11:37:28 +01:00
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.insert()
|
2025-01-22 09:39:31 +01:00
|
|
|
.into(webapi)
|
2025-01-21 11:37:28 +01:00
|
|
|
.values({
|
2025-01-22 09:39:31 +01:00
|
|
|
token: createWebapi.token,
|
|
|
|
title: createWebapi.title,
|
|
|
|
expiry: createWebapi.expiry,
|
2025-01-21 11:37:28 +01:00
|
|
|
})
|
|
|
|
.execute()
|
|
|
|
.then((result) => {
|
|
|
|
return result.identifiers[0].token;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("CREATE", "webapi", err);
|
2025-01-21 11:37:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update api
|
2025-01-22 09:39:31 +01:00
|
|
|
* @param {UpdateWebapiCommand} updateWebapi
|
2025-01-21 11:37:28 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2025-01-22 09:39:31 +01:00
|
|
|
static async update(updateWebapi: UpdateWebapiCommand): Promise<void> {
|
2025-01-21 11:37:28 +01:00
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
2025-01-22 09:39:31 +01:00
|
|
|
.update(webapi)
|
2025-01-21 11:37:28 +01:00
|
|
|
.set({
|
2025-01-22 09:39:31 +01:00
|
|
|
title: updateWebapi.title,
|
|
|
|
expiry: updateWebapi.expiry,
|
2025-01-21 11:37:28 +01:00
|
|
|
})
|
2025-01-22 09:39:31 +01:00
|
|
|
.where("id = :id", { id: updateWebapi.id })
|
2025-01-21 11:37:28 +01:00
|
|
|
.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);
|
2025-01-21 11:37:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description delete api
|
2025-01-22 09:39:31 +01:00
|
|
|
* @param {DeleteWebapiCommand} deleteWebapi
|
2025-01-21 11:37:28 +01:00
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
2025-01-22 09:39:31 +01:00
|
|
|
static async delete(deleteWebapi: DeleteWebapiCommand): Promise<void> {
|
2025-01-21 11:37:28 +01:00
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.delete()
|
2025-01-22 09:39:31 +01:00
|
|
|
.from(webapi)
|
|
|
|
.where("id = :id", { id: deleteWebapi.id })
|
2025-01-21 11:37:28 +01:00
|
|
|
.execute()
|
|
|
|
.then(() => {})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("DELETE", "webapi", err);
|
2025-01-21 11:37:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|