76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
|
import { dataSource } from "../data-source";
|
||
|
import { member } from "../entity/member";
|
||
|
import InternalException from "../exceptions/internalException";
|
||
|
import { CreateMemberCommand, DeleteMemberCommand, UpdateMemberCommand } from "./memberCommand";
|
||
|
|
||
|
export default abstract class MemberCommandHandler {
|
||
|
/**
|
||
|
* @description create member
|
||
|
* @param CreateMemberCommand
|
||
|
* @returns {Promise<number>}
|
||
|
*/
|
||
|
static async create(createMember: CreateMemberCommand): Promise<number> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.insert()
|
||
|
.into(member)
|
||
|
.values({
|
||
|
salutation: createMember.salutation,
|
||
|
firstname: createMember.firstname,
|
||
|
lastname: createMember.lastname,
|
||
|
nameaffix: createMember.nameaffix,
|
||
|
birthdate: createMember.birthdate,
|
||
|
})
|
||
|
.execute()
|
||
|
.then((result) => {
|
||
|
return result.identifiers[0].id;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed creating member", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description update member
|
||
|
* @param UpdateMemberCommand
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
|
static async update(updateMember: UpdateMemberCommand): Promise<void> {
|
||
|
console.log(updateMember);
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.update(member)
|
||
|
.set({
|
||
|
salutation: updateMember.salutation,
|
||
|
firstname: updateMember.firstname,
|
||
|
lastname: updateMember.lastname,
|
||
|
nameaffix: updateMember.nameaffix,
|
||
|
birthdate: updateMember.birthdate,
|
||
|
})
|
||
|
.where("id = :id", { id: updateMember.id })
|
||
|
.execute()
|
||
|
.then(() => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed updating member", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description delete member
|
||
|
* @param DeleteMemberCommand
|
||
|
* @returns {Promise<void>}
|
||
|
*/
|
||
|
static async delete(deleteMember: DeleteMemberCommand): Promise<void> {
|
||
|
return await dataSource
|
||
|
.createQueryBuilder()
|
||
|
.delete()
|
||
|
.from(member)
|
||
|
.where("id = :id", { id: deleteMember.id })
|
||
|
.execute()
|
||
|
.then(() => {})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("Failed deleting member", err);
|
||
|
});
|
||
|
}
|
||
|
}
|