query store CRUD
This commit is contained in:
parent
2518a1046f
commit
7497787ae4
12 changed files with 327 additions and 5 deletions
12
src/command/queryStoreCommand.ts
Normal file
12
src/command/queryStoreCommand.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
export interface CreateQueryStoreCommand {
|
||||
query: string;
|
||||
}
|
||||
|
||||
export interface UpdateQueryStoreCommand {
|
||||
id: number;
|
||||
query: string;
|
||||
}
|
||||
|
||||
export interface DeleteQueryStoreCommand {
|
||||
id: number;
|
||||
}
|
66
src/command/queryStoreCommandHandler.ts
Normal file
66
src/command/queryStoreCommandHandler.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { query } from "../entity/query";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
import { CreateQueryStoreCommand, DeleteQueryStoreCommand, UpdateQueryStoreCommand } from "./queryStoreCommand";
|
||||
|
||||
export default abstract class QueryStoreCommandHandler {
|
||||
/**
|
||||
* @description create queryStore
|
||||
* @param CreateQueryStoreCommand
|
||||
* @returns {Promise<number>}
|
||||
*/
|
||||
static async create(createQueryStore: CreateQueryStoreCommand): Promise<number> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(query)
|
||||
.values({
|
||||
query: createQueryStore.query,
|
||||
})
|
||||
.execute()
|
||||
.then((result) => {
|
||||
return result.identifiers[0].id;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed creating queryStore", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description update queryStore
|
||||
* @param UpdateQueryStoreCommand
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async update(updateQueryStore: UpdateQueryStoreCommand): Promise<void> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.update(query)
|
||||
.set({
|
||||
queryStore: updateQueryStore.query,
|
||||
})
|
||||
.where("id = :id", { id: updateQueryStore.id })
|
||||
.execute()
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed updating queryStore", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete queryStore
|
||||
* @param DeleteQueryStoreCommand
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async delete(deletQueryStore: DeleteQueryStoreCommand): Promise<void> {
|
||||
return await dataSource
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(query)
|
||||
.where("id = :id", { id: deletQueryStore.id })
|
||||
.execute()
|
||||
.then(() => {})
|
||||
.catch((err) => {
|
||||
throw new InternalException("Failed deleting queryStore", err);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -37,13 +37,11 @@ export async function executeQuery(req: Request, res: Response): Promise<any> {
|
|||
let count = parseInt((req.query.count as string) ?? "25");
|
||||
const query = req.body.query;
|
||||
|
||||
//build query to sql
|
||||
//verify sql or return error
|
||||
//let [rows, total] = await executeQuery(query, offset, count);
|
||||
let [rows, total] = await DynamicQueryBuilder.buildQuery(query, offset, count).getManyAndCount();
|
||||
|
||||
res.json({
|
||||
rows: [],
|
||||
total: 0,
|
||||
rows: rows,
|
||||
total: total,
|
||||
offset: offset,
|
||||
count: count,
|
||||
});
|
||||
|
|
91
src/controller/admin/queryStoreController.ts
Normal file
91
src/controller/admin/queryStoreController.ts
Normal file
|
@ -0,0 +1,91 @@
|
|||
import { Request, Response } from "express";
|
||||
import QueryStoreFactory from "../../factory/admin/queryStore";
|
||||
import QueryStoreService from "../../service/queryStoreService";
|
||||
import {
|
||||
CreateQueryStoreCommand,
|
||||
DeleteQueryStoreCommand,
|
||||
UpdateQueryStoreCommand,
|
||||
} from "../../command/queryStoreCommand";
|
||||
import QueryStoreCommandHandler from "../../command/queryStoreCommandHandler";
|
||||
|
||||
/**
|
||||
* @description get all queryStores
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function getAllQueryStores(req: Request, res: Response): Promise<any> {
|
||||
let queryStores = await QueryStoreService.getAll();
|
||||
|
||||
res.json(QueryStoreFactory.mapToBase(queryStores));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get queryStore by id
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function getQueryStoreById(req: Request, res: Response): Promise<any> {
|
||||
const id = parseInt(req.params.id);
|
||||
|
||||
let queryStore = await QueryStoreService.getById(id);
|
||||
|
||||
res.json(QueryStoreFactory.mapToSingle(queryStore));
|
||||
}
|
||||
|
||||
/**
|
||||
* @description create new queryStore
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function createQueryStore(req: Request, res: Response): Promise<any> {
|
||||
const query = req.body.query;
|
||||
|
||||
let createQueryStore: CreateQueryStoreCommand = {
|
||||
query: query,
|
||||
};
|
||||
|
||||
await QueryStoreCommandHandler.create(createQueryStore);
|
||||
|
||||
res.sendStatus(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description update queryStore
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function updateQueryStore(req: Request, res: Response): Promise<any> {
|
||||
const id = parseInt(req.params.id);
|
||||
const query = req.body.query;
|
||||
|
||||
let updateQueryStore: UpdateQueryStoreCommand = {
|
||||
id: id,
|
||||
query: query,
|
||||
};
|
||||
|
||||
await QueryStoreCommandHandler.update(updateQueryStore);
|
||||
|
||||
res.sendStatus(204);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description delete queryStore
|
||||
* @param req {Request} Express req object
|
||||
* @param res {Response} Express res object
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
export async function deleteQueryStore(req: Request, res: Response): Promise<any> {
|
||||
const id = parseInt(req.params.id);
|
||||
|
||||
let deleteQueryStore: DeleteQueryStoreCommand = {
|
||||
id: id,
|
||||
};
|
||||
|
||||
await QueryStoreCommandHandler.delete(deleteQueryStore);
|
||||
|
||||
res.sendStatus(204);
|
||||
}
|
|
@ -44,6 +44,8 @@ import { reset } from "./entity/reset";
|
|||
import { ResetToken1732358596823 } from "./migrations/1732358596823-resetToken";
|
||||
import { SMSAlarming1732696919191 } from "./migrations/1732696919191-SMSAlarming";
|
||||
import { SecuringCalendarType1733249553766 } from "./migrations/1733249553766-securingCalendarType";
|
||||
import { query } from "./entity/query";
|
||||
import { QueryStore1734187754677 } from "./migrations/1734187754677-queryStore";
|
||||
|
||||
const dataSource = new DataSource({
|
||||
type: DB_TYPE as any,
|
||||
|
@ -82,6 +84,7 @@ const dataSource = new DataSource({
|
|||
protocolPrintout,
|
||||
calendar,
|
||||
calendarType,
|
||||
query,
|
||||
],
|
||||
migrations: [
|
||||
Initial1724317398939,
|
||||
|
@ -98,6 +101,7 @@ const dataSource = new DataSource({
|
|||
ResetToken1732358596823,
|
||||
SMSAlarming1732696919191,
|
||||
SecuringCalendarType1733249553766,
|
||||
QueryStore1734187754677,
|
||||
],
|
||||
migrationsRun: true,
|
||||
migrationsTransactionMode: "each",
|
||||
|
|
10
src/entity/query.ts
Normal file
10
src/entity/query.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { Column, Entity, PrimaryColumn } from "typeorm";
|
||||
|
||||
@Entity()
|
||||
export class query {
|
||||
@PrimaryColumn({ generated: "increment", type: "int" })
|
||||
id: number;
|
||||
|
||||
@Column({ type: "text", default: "" })
|
||||
query: string;
|
||||
}
|
25
src/factory/admin/queryStore.ts
Normal file
25
src/factory/admin/queryStore.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { query } from "../../entity/query";
|
||||
import { QueryStoreViewModel } from "../../viewmodel/admin/queryStore.models";
|
||||
|
||||
export default abstract class QueryStoreFactory {
|
||||
/**
|
||||
* @description map record to queryStore
|
||||
* @param {queryStore} record
|
||||
* @returns {QueryStoreViewModel}
|
||||
*/
|
||||
public static mapToSingle(record: query): QueryStoreViewModel {
|
||||
return {
|
||||
id: record.id,
|
||||
query: record.query,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description map records to queryStore
|
||||
* @param {Array<queryStore>} records
|
||||
* @returns {Array<QueryStoreViewModel>}
|
||||
*/
|
||||
public static mapToBase(records: Array<query>): Array<QueryStoreViewModel> {
|
||||
return records.map((r) => this.mapToSingle(r));
|
||||
}
|
||||
}
|
25
src/migrations/1734187754677-queryStore.ts
Normal file
25
src/migrations/1734187754677-queryStore.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
import { MigrationInterface, QueryRunner, Table } from "typeorm";
|
||||
import { DB_TYPE } from "../env.defaults";
|
||||
|
||||
export class QueryStore1734187754677 implements MigrationInterface {
|
||||
name = "QueryStore1734187754677";
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
const variableType_int = DB_TYPE == "mysql" ? "int" : "integer";
|
||||
|
||||
await queryRunner.createTable(
|
||||
new Table({
|
||||
name: "query",
|
||||
columns: [
|
||||
{ name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" },
|
||||
{ name: "query", type: "text", isNullable: false, default: "''" },
|
||||
],
|
||||
}),
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.dropTable("query");
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ import executivePosition from "./executivePosition";
|
|||
import membershipStatus from "./membershipStatus";
|
||||
import qualification from "./qualification";
|
||||
import calendarType from "./calendarType";
|
||||
import queryStore from "./queryStore";
|
||||
|
||||
import member from "./member";
|
||||
import protocol from "./protocol";
|
||||
|
@ -37,6 +38,7 @@ router.use(
|
|||
);
|
||||
router.use("/qualification", PermissionHelper.passCheckMiddleware("read", "settings", "qualification"), qualification);
|
||||
router.use("/calendartype", PermissionHelper.passCheckMiddleware("read", "settings", "calendar_type"), calendarType);
|
||||
router.use("/querystore", PermissionHelper.passCheckMiddleware("read", "settings", "query_store"), queryStore);
|
||||
|
||||
router.use("/member", PermissionHelper.passCheckMiddleware("read", "club", "member"), member);
|
||||
router.use("/protocol", PermissionHelper.passCheckMiddleware("read", "club", "protocol"), protocol);
|
||||
|
|
45
src/routes/admin/queryStore.ts
Normal file
45
src/routes/admin/queryStore.ts
Normal file
|
@ -0,0 +1,45 @@
|
|||
import express, { Request, Response } from "express";
|
||||
import PermissionHelper from "../../helpers/permissionHelper";
|
||||
import {
|
||||
createQueryStore,
|
||||
deleteQueryStore,
|
||||
getAllQueryStores,
|
||||
getQueryStoreById,
|
||||
updateQueryStore,
|
||||
} from "../../controller/admin/queryStoreController";
|
||||
|
||||
var router = express.Router({ mergeParams: true });
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
await getAllQueryStores(req, res);
|
||||
});
|
||||
|
||||
router.get("/:id", async (req: Request, res: Response) => {
|
||||
await getQueryStoreById(req, res);
|
||||
});
|
||||
|
||||
router.post(
|
||||
"/",
|
||||
PermissionHelper.passCheckMiddleware("create", "settings", "query_store"),
|
||||
async (req: Request, res: Response) => {
|
||||
await createQueryStore(req, res);
|
||||
}
|
||||
);
|
||||
|
||||
router.patch(
|
||||
"/:id",
|
||||
PermissionHelper.passCheckMiddleware("update", "settings", "query_store"),
|
||||
async (req: Request, res: Response) => {
|
||||
await updateQueryStore(req, res);
|
||||
}
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/:id",
|
||||
PermissionHelper.passCheckMiddleware("delete", "settings", "query_store"),
|
||||
async (req: Request, res: Response) => {
|
||||
await deleteQueryStore(req, res);
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
40
src/service/queryStoreService.ts
Normal file
40
src/service/queryStoreService.ts
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { dataSource } from "../data-source";
|
||||
import { query } from "../entity/query";
|
||||
import InternalException from "../exceptions/internalException";
|
||||
|
||||
export default abstract class QueryStoreService {
|
||||
/**
|
||||
* @description get all queryStores
|
||||
* @returns {Promise<Array<query>>}
|
||||
*/
|
||||
static async getAll(): Promise<Array<query>> {
|
||||
return await dataSource
|
||||
.getRepository(query)
|
||||
.createQueryBuilder("queryStore")
|
||||
.getMany()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("queryStores not found", err);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description get queryStore by id
|
||||
* @returns {Promise<query>}
|
||||
*/
|
||||
static async getById(id: number): Promise<query> {
|
||||
return await dataSource
|
||||
.getRepository(query)
|
||||
.createQueryBuilder("queryStore")
|
||||
.where("queryStore.id = :id", { id: id })
|
||||
.getOneOrFail()
|
||||
.then((res) => {
|
||||
return res;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new InternalException("queryStore not found by id", err);
|
||||
});
|
||||
}
|
||||
}
|
4
src/viewmodel/admin/queryStore.models.ts
Normal file
4
src/viewmodel/admin/queryStore.models.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
export interface QueryStoreViewModel {
|
||||
id: number;
|
||||
query: string;
|
||||
}
|
Loading…
Reference in a new issue