get protocol item

This commit is contained in:
Julian Krauser 2024-10-03 13:31:05 +02:00
parent 72fb6fbc20
commit 5c4e521bd8
10 changed files with 155 additions and 3 deletions

View file

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

View file

@ -29,6 +29,8 @@ import { memberQualifications } from "./entity/memberQualifications";
import { membership } from "./entity/membership"; import { membership } from "./entity/membership";
import { Memberdata1726301836849 } from "./migrations/1726301836849-memberdata"; import { Memberdata1726301836849 } from "./migrations/1726301836849-memberdata";
import { CommunicationFields1727439800630 } from "./migrations/1727439800630-communicationFields"; import { CommunicationFields1727439800630 } from "./migrations/1727439800630-communicationFields";
import { protocol } from "./entity/protocol";
import { ProtocolInit1727953803404 } from "./migrations/1727953803404-protocol-init";
const dataSource = new DataSource({ const dataSource = new DataSource({
type: DB_TYPE as any, type: DB_TYPE as any,
@ -58,6 +60,7 @@ const dataSource = new DataSource({
memberExecutivePositions, memberExecutivePositions,
memberQualifications, memberQualifications,
membership, membership,
protocol,
], ],
migrations: [ migrations: [
Initial1724317398939, Initial1724317398939,
@ -68,6 +71,7 @@ const dataSource = new DataSource({
MemberBaseData1725435669492, MemberBaseData1725435669492,
Memberdata1726301836849, Memberdata1726301836849,
CommunicationFields1727439800630, CommunicationFields1727439800630,
ProtocolInit1727953803404,
], ],
migrationsRun: true, migrationsRun: true,
migrationsTransactionMode: "each", migrationsTransactionMode: "each",

13
src/entity/protocol.ts Normal file
View file

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

View file

@ -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<protocol>} records
* @returns {Array<ProtocolViewModel>}
*/
public static mapToBase(records: Array<protocol>): Array<ProtocolViewModel> {
return records.map((r) => this.mapToSingle(r));
}
}

View file

@ -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<void> {
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<void> {
await queryRunner.dropTable("protocol");
}
}

View file

@ -8,6 +8,7 @@ import membershipStatus from "./membershipStatus";
import qualification from "./qualification"; import qualification from "./qualification";
import member from "./member"; import member from "./member";
import protocol from "./protocol";
import role from "./role"; import role from "./role";
import user from "./user"; 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("/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("/role", PermissionHelper.passCheckMiddleware("read", "user", "role"), role);
router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user); router.use("/user", PermissionHelper.passCheckMiddleware("read", "user", "user"), user);

View file

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

View file

@ -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<protocol>, number]>}
*/
static async getAll(offset: number = 0, count: number = 25): Promise<[Array<protocol>, 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<protocol>}
*/
static async getById(id: number): Promise<protocol> {
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);
});
}
}

View file

@ -4,7 +4,7 @@ export type PermissionModule =
| "member" | "member"
| "calendar" | "calendar"
| "newsletter" | "newsletter"
| "protocoll" | "protocol"
| "qualification" | "qualification"
| "award" | "award"
| "executive_position" | "executive_position"
@ -39,7 +39,7 @@ export const permissionModules: Array<PermissionModule> = [
"member", "member",
"calendar", "calendar",
"newsletter", "newsletter",
"protocoll", "protocol",
"qualification", "qualification",
"award", "award",
"executive_position", "executive_position",
@ -50,7 +50,7 @@ export const permissionModules: Array<PermissionModule> = [
]; ];
export const permissionTypes: Array<PermissionType> = ["read", "create", "update", "delete"]; export const permissionTypes: Array<PermissionType> = ["read", "create", "update", "delete"];
export const sectionsAndModules: SectionsAndModulesObject = { export const sectionsAndModules: SectionsAndModulesObject = {
club: ["member", "calendar", "newsletter", "protocoll"], club: ["member", "calendar", "newsletter", "protocol"],
settings: ["qualification", "award", "executive_position", "communication", "membership_status"], settings: ["qualification", "award", "executive_position", "communication", "membership_status"],
user: ["user", "role"], user: ["user", "role"],
}; };

View file

@ -0,0 +1,5 @@
export interface ProtocolViewModel {
id: number;
title: string;
date: Date;
}