Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions codbex-uoms/codbex-uoms.gen
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,15 @@
"tablePrefix": "CODBEX_",
"brand": "codbex",
"brandUrl": "https://www.codbex.com/",
"title": "UoM Management Application",
"description": "Managing UoMs data",
"fileName": "codbex-uoms",
"genFolderName": "codbex-uoms",
"dataSource": "DefaultDB",
"roles": [
{
"entityName": "Dimension",
"roleRead": "codbex-uoms.Dimensions.DimensionReadOnly",
"roleWrite": "codbex-uoms.Dimensions.DimensionFullAccess"
},
{
"entityName": "UoM",
"roleRead": "codbex-uoms.UnitsOfMeasures.UoMReadOnly",
"roleWrite": "codbex-uoms.UnitsOfMeasures.UoMFullAccess"
}
],
"tprefix": "codbex-uoms-model",
"title": "UOMs Management Module",
"description": "Managing UOMs Data",
"projectName": "codbex-uoms",
"workspaceName": "workspace",
"filePath": "codbex-uoms.model",
"templateId": "template-application-angular/template/template.js",
"templateId": "template-application-angular-v2/template/template.js",
"fileName": "codbex-uoms",
"genFolderName": "codbex-uoms",
"dataSource": "DefaultDB",
"perspectives": {
"Settings": {
"views": [
Expand All @@ -39,6 +26,19 @@
"role": ""
}
},
"roles": [
{
"entityName": "Dimension",
"roleRead": "codbex-uoms.Dimensions.DimensionReadOnly",
"roleWrite": "codbex-uoms.Dimensions.DimensionFullAccess"
},
{
"entityName": "UoM",
"roleRead": "codbex-uoms.UnitsOfMeasures.UoMReadOnly",
"roleWrite": "codbex-uoms.UnitsOfMeasures.UoMFullAccess"
}
],
"tprefix": "codbex-uoms-model",
"models": [
{
"properties": [
Expand All @@ -61,6 +61,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Id",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"inputRule": ""
Expand All @@ -85,6 +86,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Name",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"minLength": 0,
Expand All @@ -111,6 +113,7 @@
"isCalculatedProperty": false,
"widgetLabel": "SAP",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"minLength": 0,
Expand Down Expand Up @@ -172,6 +175,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Id",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"inputRule": ""
Expand All @@ -196,6 +200,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Name",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"minLength": 0,
Expand All @@ -222,6 +227,7 @@
"isCalculatedProperty": false,
"widgetLabel": "ISO",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"minLength": 0,
Expand Down Expand Up @@ -253,6 +259,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Dimension",
"widgetDropdownUrl": "/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionService.ts",
"widgetDropdownControllerUrl": "/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"inputRule": ""
Expand Down Expand Up @@ -281,6 +288,7 @@
"isCalculatedProperty": false,
"widgetLabel": "SAP",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "string",
"dataTypeTypescript": "string",
"minLength": 0,
Expand All @@ -306,6 +314,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Numerator",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "long",
"dataTypeTypescript": "number",
"inputRule": ""
Expand All @@ -329,6 +338,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Denominator",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "long",
"dataTypeTypescript": "number",
"inputRule": ""
Expand All @@ -352,6 +362,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Rounding",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "int",
"dataTypeTypescript": "number",
"inputRule": ""
Expand All @@ -375,6 +386,7 @@
"isCalculatedProperty": false,
"widgetLabel": "Base",
"widgetDropdownUrl": "",
"widgetDropdownControllerUrl": "",
"dataTypeJava": "boolean",
"dataTypeTypescript": "boolean",
"inputRule": ""
Expand Down
181 changes: 181 additions & 0 deletions codbex-uoms/gen/codbex-uoms/api/Settings/DimensionController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { Controller, Get, Post, Put, Delete, Documentation, request, response } from '@aerokit/sdk/http'
import { HttpUtils } from "@aerokit/sdk/http/utils";
import { ValidationError } from '@aerokit/sdk/http/errors'
import { ForbiddenError } from '@aerokit/sdk/http/errors'
import { user } from '@aerokit/sdk/security'
import { Options } from '@aerokit/sdk/db'
import { Extensions } from "@aerokit/sdk/extensions"
import { Injected, Inject } from '@aerokit/sdk/component'
import { DimensionRepository } from '../../data/Settings/DimensionRepository'
import { DimensionEntity } from '../../data/Settings/DimensionEntity'

const validationModules = await Extensions.loadExtensionModules('codbex-uoms-Settings-Dimension', ['validate']);

@Controller
@Documentation('codbex-uoms - Dimension Controller')
@Injected()
class DimensionController {

@Inject('DimensionRepository')
private readonly repository!: DimensionRepository;

@Get('/')
@Documentation('Get All Dimension')
public getAll(_: any, ctx: any): DimensionEntity[] {
try {
this.checkPermissions('read');
const options: Options = {
limit: ctx.queryParameters["$limit"] ? parseInt(ctx.queryParameters["$limit"]) : 20,
offset: ctx.queryParameters["$offset"] ? parseInt(ctx.queryParameters["$offset"]) : 0,
language: request.getLocale().slice(0, 2)
};

return this.repository.findAll(options);
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Post('/')
@Documentation('Create Dimension')
public create(entity: DimensionEntity): DimensionEntity {
try {
this.checkPermissions('write');
this.validateEntity(entity);
entity.Id = this.repository.create(entity) as any;
response.setHeader('Content-Location', '/services/ts/codbex-uoms/gen/codbex-uoms/api/Settings/DimensionService.ts/' + entity.Id);
response.setStatus(response.CREATED);
return entity;
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Get('/count')
@Documentation('Count Dimension')
public count(): { count: number } {
try {
this.checkPermissions('read');
return { count: this.repository.count() };
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Post('/count')
@Documentation('Count Dimension with filter')
public countWithFilter(filter: any): { count: number } {
try {
this.checkPermissions('read');
return { count: this.repository.count(filter) };
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Post('/search')
@Documentation('Search Dimension')
public search(filter: any): DimensionEntity[] {
try {
this.checkPermissions('read');
return this.repository.findAll(filter);
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Get('/:id')
@Documentation('Get Dimension by id')
public getById(_: any, ctx: any): DimensionEntity {
try {
this.checkPermissions('read');
const id = parseInt(ctx.pathParameters.id);
const options: Options = {
language: request.getLocale().slice(0, 2)
};
const entity = this.repository.findById(id, options);
if (entity) {
return entity;
} else {
HttpUtils.sendResponseNotFound('Dimension not found');
}
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Put('/:id')
@Documentation('Update Dimension by id')
public update(entity: DimensionEntity, ctx: any): DimensionEntity {
try {
this.checkPermissions('write');
const id = parseInt(ctx.pathParameters.id);
entity.Id = id;
this.validateEntity(entity);
this.repository.update(entity);
return entity;
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Delete('/:id')
@Documentation('Delete Dimension by id')
public deleteById(_: any, ctx: any): void {
try {
this.checkPermissions('write');
const id = parseInt(ctx.pathParameters.id);
const entity = this.repository.findById(id);
if (entity) {
this.repository.deleteById(id);
HttpUtils.sendResponseNoContent();
} else {
HttpUtils.sendResponseNotFound('Dimension not found');
}
} catch (error: any) {
this.handleError(error);
}
}

private handleError(error: any) {
if (error.name === 'ForbiddenError') {
HttpUtils.sendForbiddenRequest(error.message);
} else if (error.name === 'ValidationError') {
HttpUtils.sendResponseBadRequest(error.message);
} else {
HttpUtils.sendInternalServerError(error.message);
}
}

private checkPermissions(operationType: string) {
if (operationType === 'read' && !(user.isInRole('codbex-uoms.Dimensions.DimensionReadOnly') || user.isInRole('codbex-uoms.Dimensions.DimensionFullAccess'))) {
throw new ForbiddenError();
}
if (operationType === 'write' && !user.isInRole('codbex-uoms.Dimensions.DimensionFullAccess')) {
throw new ForbiddenError();
}
}

private validateEntity(entity: any): void {
if (entity.Name === null || entity.Name === undefined) {
throw new ValidationError(`The 'Name' property is required, provide a valid value`);
}
if (entity.Name?.length > 100) {
throw new ValidationError(`The 'Name' exceeds the maximum length of [100] characters`);
}
if (entity.SAP?.length > 20) {
throw new ValidationError(`The 'SAP' exceeds the maximum length of [20] characters`);
}
for (const next of validationModules) {
next.validate(entity);
}
}

}
Loading