2024-12-14 16:11:53 +01:00
|
|
|
import { dataSource } from "../data-source";
|
|
|
|
import { query } from "../entity/query";
|
|
|
|
import InternalException from "../exceptions/internalException";
|
|
|
|
import { CreateQueryStoreCommand, DeleteQueryStoreCommand, UpdateQueryStoreCommand } from "./queryStoreCommand";
|
|
|
|
|
|
|
|
export default abstract class QueryStoreCommandHandler {
|
|
|
|
/**
|
|
|
|
* @description create queryStore
|
|
|
|
* @param CreateQueryStoreCommand
|
|
|
|
* @returns {Promise<number>}
|
|
|
|
*/
|
|
|
|
static async create(createQueryStore: CreateQueryStoreCommand): Promise<number> {
|
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.insert()
|
|
|
|
.into(query)
|
|
|
|
.values({
|
2024-12-18 22:27:33 +01:00
|
|
|
title: createQueryStore.title,
|
|
|
|
query:
|
|
|
|
typeof createQueryStore.query == "string" ? createQueryStore.query : JSON.stringify(createQueryStore.query),
|
2024-12-14 16:11:53 +01:00
|
|
|
})
|
|
|
|
.execute()
|
|
|
|
.then((result) => {
|
|
|
|
return result.identifiers[0].id;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new InternalException("Failed creating queryStore", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update queryStore
|
|
|
|
* @param UpdateQueryStoreCommand
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async update(updateQueryStore: UpdateQueryStoreCommand): Promise<void> {
|
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.update(query)
|
|
|
|
.set({
|
2024-12-18 22:27:33 +01:00
|
|
|
query:
|
|
|
|
typeof updateQueryStore.query == "string" ? updateQueryStore.query : JSON.stringify(updateQueryStore.query),
|
2024-12-14 16:11:53 +01:00
|
|
|
})
|
|
|
|
.where("id = :id", { id: updateQueryStore.id })
|
|
|
|
.execute()
|
|
|
|
.then(() => {})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new InternalException("Failed updating queryStore", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description delete queryStore
|
|
|
|
* @param DeleteQueryStoreCommand
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
static async delete(deletQueryStore: DeleteQueryStoreCommand): Promise<void> {
|
|
|
|
return await dataSource
|
|
|
|
.createQueryBuilder()
|
|
|
|
.delete()
|
|
|
|
.from(query)
|
|
|
|
.where("id = :id", { id: deletQueryStore.id })
|
|
|
|
.execute()
|
|
|
|
.then(() => {})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new InternalException("Failed deleting queryStore", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|