diff --git a/src/command/memberCommand.ts b/src/command/memberCommand.ts new file mode 100644 index 0000000..0d76460 --- /dev/null +++ b/src/command/memberCommand.ts @@ -0,0 +1,22 @@ +import { Salutation } from "../enums/salutation"; + +export interface CreateMemberCommand { + salutation: Salutation; + firstname: string; + lastname: string; + nameaffix: string; + birthdate: Date; +} + +export interface UpdateMemberCommand { + id: number; + salutation: Salutation; + firstname: string; + lastname: string; + nameaffix: string; + birthdate: Date; +} + +export interface DeleteMemberCommand { + id: number; +} diff --git a/src/command/memberCommandHandler.ts b/src/command/memberCommandHandler.ts new file mode 100644 index 0000000..28fc79e --- /dev/null +++ b/src/command/memberCommandHandler.ts @@ -0,0 +1,75 @@ +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} + */ + static async create(createMember: CreateMemberCommand): Promise { + 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} + */ + static async update(updateMember: UpdateMemberCommand): Promise { + 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} + */ + static async delete(deleteMember: DeleteMemberCommand): Promise { + return await dataSource + .createQueryBuilder() + .delete() + .from(member) + .where("id = :id", { id: deleteMember.id }) + .execute() + .then(() => {}) + .catch((err) => { + throw new InternalException("Failed deleting member", err); + }); + } +} diff --git a/src/controller/admin/memberController.ts b/src/controller/admin/memberController.ts index 71b349b..155790b 100644 --- a/src/controller/admin/memberController.ts +++ b/src/controller/admin/memberController.ts @@ -11,6 +11,8 @@ import MemberExecutivePositionService from "../../service/memberExecutivePositio import MemberExecutivePositionFactory from "../../factory/admin/memberExecutivePosition"; import CommunicationService from "../../service/communicationService"; import CommunicationFactory from "../../factory/admin/communication"; +import { CreateMemberCommand, DeleteMemberCommand, UpdateMemberCommand } from "../../command/memberCommand"; +import MemberCommandHandler from "../../command/memberCommandHandler"; /** * @description get all members @@ -122,7 +124,16 @@ export async function createMember(req: Request, res: Response): Promise { const nameaffix = req.body.nameaffix; const birthdate = req.body.birthdate; - res.status(200).send(0); + let createMember: CreateMemberCommand = { + salutation, + firstname, + lastname, + nameaffix, + birthdate, + }; + let memberId = await MemberCommandHandler.create(createMember); + + res.status(200).send(memberId); } /** @@ -199,6 +210,16 @@ export async function updateMemberById(req: Request, res: Response): Promise { const memberId = parseInt(req.params.id); + let deleteMember: DeleteMemberCommand = { + id: memberId, + }; + await MemberCommandHandler.delete(deleteMember); + res.sendStatus(204); } diff --git a/src/enums/salutation.ts b/src/enums/salutation.ts index 214bb85..ba2fce4 100644 --- a/src/enums/salutation.ts +++ b/src/enums/salutation.ts @@ -1,6 +1,6 @@ export enum Salutation { - Sir = "sir", - Madam = "madam", + sir = "sir", + madam = "madam", other = "other", none = "none", }