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
28 changes: 14 additions & 14 deletions codbex-countries/codbex-countries.gen
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
{
"tablePrefix": "CODBEX_",
"brand": "codbex",
"brandUrl": "https://codbex.com",
"title": "Countries Management Module",
"brandUrl": "https://www.codbex.com/",
"title": "Countries management module",
"description": "Managing countries data",
"dataSource": "DefaultDB",
"fileName": "codbex-countries",
"genFolderName": "codbex-countries",
"roles": [
{
"entityName": "Country",
"roleRead": "codbex-countries.Countries.CountryReadOnly",
"roleWrite": "codbex-countries.Countries.CountryFullAccess"
}
],
"tprefix": "codbex-countries-model",
"projectName": "codbex-countries",
"workspaceName": "workspace",
"filePath": "codbex-countries.model",
"templateId": "template-application-angular/template/template.js",
"templateId": "template-application-angular-v2/template/template.js",
"fileName": "codbex-countries",
"genFolderName": "codbex-countries",
"dataSource": "DefaultDB",
"perspectives": {
"Settings": {
"views": [
Expand All @@ -33,6 +25,14 @@
"role": ""
}
},
"roles": [
{
"entityName": "Country",
"roleRead": "codbex-countries.Countries.CountryReadOnly",
"roleWrite": "codbex-countries.Countries.CountryFullAccess"
}
],
"tprefix": "codbex-countries-model",
"models": [
{
"properties": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
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 { CountryRepository } from '../../data/Settings/CountryRepository'
import { CountryEntity } from '../../data/Settings/CountryEntity'

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

@Controller
@Documentation('codbex-countries - Country Controller')
@Injected()
class CountryController {

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

@Get('/')
@Documentation('Get All Country')
public getAll(_: any, ctx: any): CountryEntity[] {
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 Country')
public create(entity: CountryEntity): CountryEntity {
try {
this.checkPermissions('write');
this.validateEntity(entity);
entity.Id = this.repository.create(entity) as any;
response.setHeader('Content-Location', '/services/ts/codbex-countries/gen/codbex-countries/api/Settings/CountryService.ts/' + entity.Id);
response.setStatus(response.CREATED);
return entity;
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Get('/count')
@Documentation('Count Country')
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 Country 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 Country')
public search(filter: any): CountryEntity[] {
try {
this.checkPermissions('read');
return this.repository.findAll(filter);
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Get('/:id')
@Documentation('Get Country by id')
public getById(_: any, ctx: any): CountryEntity {
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('Country not found');
}
} catch (error: any) {
this.handleError(error);
}
return undefined as any;
}

@Put('/:id')
@Documentation('Update Country by id')
public update(entity: CountryEntity, ctx: any): CountryEntity {
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 Country 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('Country 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-countries.Countries.CountryReadOnly') || user.isInRole('codbex-countries.Countries.CountryFullAccess'))) {
throw new ForbiddenError();
}
if (operationType === 'write' && !user.isInRole('codbex-countries.Countries.CountryFullAccess')) {
throw new ForbiddenError();
}
}

private validateEntity(entity: any): void {
if (entity.Name?.length > 255) {
throw new ValidationError(`The 'Name' exceeds the maximum length of [255] characters`);
}
if (entity.Code2?.length > 2) {
throw new ValidationError(`The 'Code2' exceeds the maximum length of [2] characters`);
}
if (entity.Code3?.length > 3) {
throw new ValidationError(`The 'Code3' exceeds the maximum length of [3] characters`);
}
if (entity.Numeric?.length > 3) {
throw new ValidationError(`The 'Numeric' exceeds the maximum length of [3] characters`);
}
for (const next of validationModules) {
next.validate(entity);
}
}

}
162 changes: 0 additions & 162 deletions codbex-countries/gen/codbex-countries/api/Settings/CountryService.ts

This file was deleted.

Loading