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
38 changes: 19 additions & 19 deletions codbex-methods/codbex-methods.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": "Methods Managing module",
"description": "Module for managing methods",
"dataSource": "DefaultDB",
"fileName": "codbex-methods",
"genFolderName": "codbex-methods",
"roles": [
{
"entityName": "PaymentMethod",
"roleRead": "codbex-methods.PaymentMethod.PaymentMethodReadOnly",
"roleWrite": "codbex-methods.PaymentMethod.PaymentMethodFullAccess"
},
{
"entityName": "SentMethod",
"roleRead": "codbex-methods.SentMethod.SentMethodReadOnly",
"roleWrite": "codbex-methods.SentMethod.SentMethodFullAccess"
}
],
"tprefix": "codbex-methods-model",
"title": "Methods management module",
"description": "Managing methods data",
"projectName": "codbex-methods",
"workspaceName": "workspace",
"filePath": "codbex-methods.model",
"templateId": "template-application-angular/template/template.js",
"templateId": "template-application-angular-v2/template/template.js",
"fileName": "codbex-methods",
"genFolderName": "codbex-methods",
"dataSource": "DefaultDB",
"perspectives": {
"Settings": {
"views": [
Expand All @@ -39,6 +26,19 @@
"role": ""
}
},
"roles": [
{
"entityName": "PaymentMethod",
"roleRead": "codbex-methods.PaymentMethod.PaymentMethodReadOnly",
"roleWrite": "codbex-methods.PaymentMethod.PaymentMethodFullAccess"
},
{
"entityName": "SentMethod",
"roleRead": "codbex-methods.SentMethod.SentMethodReadOnly",
"roleWrite": "codbex-methods.SentMethod.SentMethodFullAccess"
}
],
"tprefix": "codbex-methods-model",
"models": [
{
"properties": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
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 { PaymentMethodRepository } from '../../data/Settings/PaymentMethodRepository'
import { PaymentMethodEntity } from '../../data/Settings/PaymentMethodEntity'

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

@Controller
@Documentation('codbex-methods - PaymentMethod Controller')
@Injected()
class PaymentMethodController {

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

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

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

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

@Put('/:id')
@Documentation('Update PaymentMethod by id')
public update(entity: PaymentMethodEntity, ctx: any): PaymentMethodEntity {
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 PaymentMethod 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('PaymentMethod 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-methods.PaymentMethod.PaymentMethodReadOnly') || user.isInRole('codbex-methods.PaymentMethod.PaymentMethodFullAccess'))) {
throw new ForbiddenError();
}
if (operationType === 'write' && !user.isInRole('codbex-methods.PaymentMethod.PaymentMethodFullAccess')) {
throw new ForbiddenError();
}
}

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

}
153 changes: 0 additions & 153 deletions codbex-methods/gen/codbex-methods/api/Settings/PaymentMethodService.ts

This file was deleted.

Loading