From 5c4e521bd8e503747ff93ba2b6884e4e7b3dbd24 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Thu, 3 Oct 2024 13:31:05 +0200 Subject: [PATCH] get protocol item --- src/controller/admin/protocolController.ts | 22 ++++++++++ src/data-source.ts | 4 ++ src/entity/protocol.ts | 13 ++++++ src/factory/admin/protocol.ts | 26 +++++++++++ src/migrations/1727953803404-protocol-init.ts | 26 +++++++++++ src/routes/admin/index.ts | 3 ++ src/routes/admin/protocol.ts | 10 +++++ src/service/protocolService.ts | 43 +++++++++++++++++++ src/type/permissionTypes.ts | 6 +-- src/viewmodel/admin/protocol.models.ts | 5 +++ 10 files changed, 155 insertions(+), 3 deletions(-) create mode 100644 src/controller/admin/protocolController.ts create mode 100644 src/entity/protocol.ts create mode 100644 src/factory/admin/protocol.ts create mode 100644 src/migrations/1727953803404-protocol-init.ts create mode 100644 src/routes/admin/protocol.ts create mode 100644 src/service/protocolService.ts create mode 100644 src/viewmodel/admin/protocol.models.ts diff --git a/src/controller/admin/protocolController.ts b/src/controller/admin/protocolController.ts new file mode 100644 index 0000000..6df7366 --- /dev/null +++ b/src/controller/admin/protocolController.ts @@ -0,0 +1,22 @@ +import { Request, Response } from "express"; +import ProtocolService from "../../service/protocolService"; +import ProtocolFactory from "../../factory/admin/protocol"; + +/** + * @description get all protocols + * @param req {Request} Express req object + * @param res {Response} Express res object + * @returns {Promise<*>} + */ +export async function getAllProtocols(req: Request, res: Response): Promise { + let offset = parseInt((req.query.offset as string) ?? "0"); + let count = parseInt((req.query.count as string) ?? "25"); + let [protocols, total] = await ProtocolService.getAll(offset, count); + + res.json({ + protocols: ProtocolFactory.mapToBase(protocols), + total: total, + offset: offset, + count: count, + }); +} diff --git a/src/data-source.ts b/src/data-source.ts index a78ab3c..4b01f48 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -29,6 +29,8 @@ import { memberQualifications } from "./entity/memberQualifications"; import { membership } from "./entity/membership"; import { Memberdata1726301836849 } from "./migrations/1726301836849-memberdata"; import { CommunicationFields1727439800630 } from "./migrations/1727439800630-communicationFields"; +import { protocol } from "./entity/protocol"; +import { ProtocolInit1727953803404 } from "./migrations/1727953803404-protocol-init"; const dataSource = new DataSource({ type: DB_TYPE as any, @@ -58,6 +60,7 @@ const dataSource = new DataSource({ memberExecutivePositions, memberQualifications, membership, + protocol, ], migrations: [ Initial1724317398939, @@ -68,6 +71,7 @@ const dataSource = new DataSource({ MemberBaseData1725435669492, Memberdata1726301836849, CommunicationFields1727439800630, + ProtocolInit1727953803404, ], migrationsRun: true, migrationsTransactionMode: "each", diff --git a/src/entity/protocol.ts b/src/entity/protocol.ts new file mode 100644 index 0000000..9285e0f --- /dev/null +++ b/src/entity/protocol.ts @@ -0,0 +1,13 @@ +import { Column, Entity, PrimaryColumn } from "typeorm"; + +@Entity() +export class protocol { + @PrimaryColumn({ generated: "increment", type: "int" }) + id: number; + + @Column({ type: "varchar", length: 255 }) + title: string; + + @Column({ type: "date" }) + date: Date; +} diff --git a/src/factory/admin/protocol.ts b/src/factory/admin/protocol.ts new file mode 100644 index 0000000..ef5a873 --- /dev/null +++ b/src/factory/admin/protocol.ts @@ -0,0 +1,26 @@ +import { protocol } from "../../entity/protocol"; +import { ProtocolViewModel } from "../../viewmodel/admin/protocol.models"; + +export default abstract class ProtocolFactory { + /** + * @description map record to protocol + * @param {protocol} record + * @returns {ProtocolViewModel} + */ + public static mapToSingle(record: protocol): ProtocolViewModel { + return { + id: record.id, + title: record.title, + date: record.date, + }; + } + + /** + * @description map records to protocol + * @param {Array} records + * @returns {Array} + */ + public static mapToBase(records: Array): Array { + return records.map((r) => this.mapToSingle(r)); + } +} diff --git a/src/migrations/1727953803404-protocol-init.ts b/src/migrations/1727953803404-protocol-init.ts new file mode 100644 index 0000000..f7bd435 --- /dev/null +++ b/src/migrations/1727953803404-protocol-init.ts @@ -0,0 +1,26 @@ +import { MigrationInterface, QueryRunner, Table } from "typeorm"; +import { DB_TYPE } from "../env.defaults"; + +export class ProtocolInit1727953803404 implements MigrationInterface { + name = "ProtocolInit1727953803404"; + + public async up(queryRunner: QueryRunner): Promise { + const variableType_int = DB_TYPE == "mysql" ? "int" : "integer"; + + await queryRunner.createTable( + new Table({ + name: "protocol", + columns: [ + { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, + { name: "title", type: "varchar", length: "255", isNullable: false }, + { name: "date", type: "date", isNullable: false }, + ], + }), + true + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropTable("protocol"); + } +} diff --git a/src/routes/admin/index.ts b/src/routes/admin/index.ts index c0c1ab9..6c891e1 100644 --- a/src/routes/admin/index.ts +++ b/src/routes/admin/index.ts @@ -8,6 +8,7 @@ import membershipStatus from "./membershipStatus"; import qualification from "./qualification"; import member from "./member"; +import protocol from "./protocol"; import role from "./role"; import user from "./user"; @@ -34,6 +35,8 @@ router.use("/qualification", PermissionHelper.passCheckMiddleware("read", "setti router.use("/member", PermissionHelper.passCheckMiddleware("read", "club", "member"), member); +router.use("/protocol", PermissionHelper.passCheckMiddleware("read", "club", "protocol"), protocol); + router.use("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role); router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user); diff --git a/src/routes/admin/protocol.ts b/src/routes/admin/protocol.ts new file mode 100644 index 0000000..e737ea5 --- /dev/null +++ b/src/routes/admin/protocol.ts @@ -0,0 +1,10 @@ +import express, { Request, Response } from "express"; +import { getAllProtocols } from "../../controller/admin/protocolController"; + +var router = express.Router({ mergeParams: true }); + +router.get("/", async (req: Request, res: Response) => { + await getAllProtocols(req, res); +}); + +export default router; diff --git a/src/service/protocolService.ts b/src/service/protocolService.ts new file mode 100644 index 0000000..553b550 --- /dev/null +++ b/src/service/protocolService.ts @@ -0,0 +1,43 @@ +import { dataSource } from "../data-source"; +import { protocol } from "../entity/protocol"; +import InternalException from "../exceptions/internalException"; + +export default abstract class ProtocolService { + /** + * @description get all protocols + * @returns {Promise<[Array, number]>} + */ + static async getAll(offset: number = 0, count: number = 25): Promise<[Array, number]> { + return await dataSource + .getRepository(protocol) + .createQueryBuilder("protocol") + .offset(offset) + .limit(count) + .orderBy("date") + .getManyAndCount() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocols not found", err); + }); + } + + /** + * @description get protocol by id + * @returns {Promise} + */ + static async getById(id: number): Promise { + return await dataSource + .getRepository(protocol) + .createQueryBuilder("protocol") + .where("protocol.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocol not found by id", err); + }); + } +} diff --git a/src/type/permissionTypes.ts b/src/type/permissionTypes.ts index 3bd54c8..cb8a989 100644 --- a/src/type/permissionTypes.ts +++ b/src/type/permissionTypes.ts @@ -4,7 +4,7 @@ export type PermissionModule = | "member" | "calendar" | "newsletter" - | "protocoll" + | "protocol" | "qualification" | "award" | "executive_position" @@ -39,7 +39,7 @@ export const permissionModules: Array = [ "member", "calendar", "newsletter", - "protocoll", + "protocol", "qualification", "award", "executive_position", @@ -50,7 +50,7 @@ export const permissionModules: Array = [ ]; export const permissionTypes: Array = ["read", "create", "update", "delete"]; export const sectionsAndModules: SectionsAndModulesObject = { - club: ["member", "calendar", "newsletter", "protocoll"], + club: ["member", "calendar", "newsletter", "protocol"], settings: ["qualification", "award", "executive_position", "communication", "membership_status"], user: ["user", "role"], }; diff --git a/src/viewmodel/admin/protocol.models.ts b/src/viewmodel/admin/protocol.models.ts new file mode 100644 index 0000000..051b832 --- /dev/null +++ b/src/viewmodel/admin/protocol.models.ts @@ -0,0 +1,5 @@ +export interface ProtocolViewModel { + id: number; + title: string; + date: Date; +}