import "reflect-metadata"; import { BaseApiRoute, QContext, ApiMethod, Context, CBody, Query } from "../base"; import * as t from "./types-menu"; export class Menu extends BaseApiRoute { @ApiMethod() getCategories(@Context() ctx: QContext = {}): Promise { return this.queryRunner<{}, t.Category[]>(ctx); } @ApiMethod() getCategory(category_id: number, @Context() ctx: QContext = {}): Promise { ctx.query = { category_id }; return this.queryRunner(ctx); } @ApiMethod() createCategory(@CBody() body: t.CreateCategoryBody, @Context() ctx: QContext = {}) { return this.queryRunner(ctx); } @ApiMethod() updateCategory( @CBody() body: t.UpdateCategoryBody, @Context() ctx: QContext = {}, ): Promise { return this.queryRunner(ctx); } @ApiMethod() removeCategory(@CBody() category_id: number, @Context() ctx: QContext = {}) { return this.queryRunner(ctx); } @ApiMethod() getProducts( category_id?: number, type?: t.GetProductType, @Context() ctx: QContext = {}, ): Promise { const query: t.GetProductsQuery = {}; if (category_id !== undefined) { query.category_id = category_id; } type && (query.type = type); if (category_id !== undefined || type) { ctx.query = query; } return this.queryRunner(ctx); } @ApiMethod() getProduct(product_id: number, @Context() ctx: QContext = {}): Promise { const query: t.GetProductQuery = { product_id, }; ctx.query = query; return this.queryRunner(ctx); } private multipartArrays = ["barcode", "product_code", "modificator_name", "cost", "price", "visible"]; @ApiMethod() createProduct( body: t.CreateProductBody, @Context() ctx: QContext = {}, ): Promise { if (body.modifications) { for (const key of this.multipartArrays) { const data = (body as any)[key]; if (data) { if (!Array.isArray(data)) { throw new Error(`If modifications == ${body.modifications} then ${key} should be an array!`); } data.forEach((value, index) => { (data as any)[`${key}[${index}]`] = value; }); (body as any)[key] = undefined; } } } return this.queryRunner(ctx); } }