member edit, update and delete

This commit is contained in:
Julian Krauser 2024-09-26 13:16:43 +02:00
parent 72c47ba212
commit ade837b5ab
4 changed files with 126 additions and 3 deletions

View file

@ -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;
}

View file

@ -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<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);
});
}
}

View file

@ -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<any> {
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<any
const nameaffix = req.body.nameaffix;
const birthdate = req.body.birthdate;
let updateMember: UpdateMemberCommand = {
id: memberId,
salutation,
firstname,
lastname,
nameaffix,
birthdate,
};
await MemberCommandHandler.update(updateMember);
res.sendStatus(204);
}
@ -276,6 +297,11 @@ export async function updateCommunicationOfMember(req: Request, res: Response):
export async function deleteMemberById(req: Request, res: Response): Promise<any> {
const memberId = parseInt(req.params.id);
let deleteMember: DeleteMemberCommand = {
id: memberId,
};
await MemberCommandHandler.delete(deleteMember);
res.sendStatus(204);
}

View file

@ -1,6 +1,6 @@
export enum Salutation {
Sir = "sir",
Madam = "madam",
sir = "sir",
madam = "madam",
other = "other",
none = "none",
}