67 lines
1.7 KiB
TypeScript
67 lines
1.7 KiB
TypeScript
|
import { dataSource } from "../data-source";
|
||
|
import { award } from "../entity/award";
|
||
|
import InternalException from "../exceptions/internalException";
|
||
|
import { CreateAwardCommand, DeleteAwardCommand, UpdateAwardCommand } from "./awardCommand";
|
||
|
|
||
|
export default abstract class AwardCommandHandler {
|
||
|
/**
|
||
|
* @description create award
|
||
|
* @param CreateAwardCommand
|
||
|
* @returns {Promise<number>}
|
||
|
*/
|
||
|
static async create(createAward: CreateAwardCommand): Promise<number> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.insert()
|
||
|
.into(award)
|
||
|
.values({
|
||
|
award: createAward.award,
|
||
|
})
|
||
|
.execute()
|
||
|
.then((result) => {
|
||
|
return result.identifiers[0].id;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed creating award");
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description update award
|
||
|
* @param UpdateAwardCommand
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
|
static async update(updateAward: UpdateAwardCommand): Promise<void> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.update(award)
|
||
|
.set({
|
||
|
award: updateAward.award,
|
||
|
})
|
||
|
.where("id = :id", { id: updateAward.id })
|
||
|
.execute()
|
||
|
.then(() => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed updating award");
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description delete award
|
||
|
* @param DeleteAwardCommand
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
|
static async delete(deletAward: DeleteAwardCommand): Promise<void> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.delete()
|
||
|
.from(award)
|
||
|
.where("id = :id", { id: deletAward.id })
|
||
|
.execute()
|
||
|
.then(() => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed deleting award");
|
||
|
});
|
||
|
}
|
||
|
}
|