add support for all crud and list in the service files

This commit is contained in:
2021-01-23 01:14:04 -07:00
parent c176de3297
commit 17deed412a
5 changed files with 119 additions and 72 deletions

View File

@ -0,0 +1,46 @@
import {EndpointDef} from "../systemGenService";
import {lowercaseFirstLetter} from "./helpers";
import {buildMapperFunctionName} from "./mapper-creator";
function createServiceFunc(view: EndpointDef) {
// TODO: add support for doing things other than talking to the mapper e.g. API calls and cache checks
return buildServiceFunction(view);
}
function buildServiceFunction(view: EndpointDef): string {
let func: string = '';
let isListOrSearch: boolean = view.type === 'list' || view.type === 'search';
let funcName: string = buildServiceFunctionName(view);
// there should never be an 'undefined' item in funcParams but the compiler complained that it was possible
// TODO: funcParams should be sorted by 'required' so the optional params can come last and be left off when calling
let funcParams: (string | undefined)[] = (view.columns?.filter(column => column.param).map(column => column.param) || []).concat(view.filters?.filter(filter => filter.param).map(filter => filter.param) || []);
let columnParams: string[] = view.columns?.filter(column => column.param).map(column => { return '\n ' + column.param + ': ' + column.param; });
let filterParams: string[] = view.filters?.filter(filter => filter.param).map(filter => { return '\n ' + filter.param + ': ' + filter.param; }) || [];
if (isListOrSearch) {
funcParams.push('offset');
funcParams.push('limit');
}
func = `
public ${funcName}(${funcParams.join(', ')}) {
let params = {${columnParams}${columnParams.length ? ',' : ''}${filterParams}
};
return this.${mapperInstance(view)}.${buildMapperFunctionName(view)}(params${isListOrSearch ? ', offset, limit' : ''});
}
`;
return func;
}
function buildServiceFunctionName(view: EndpointDef): string {
return buildMapperFunctionName(view); // the functions should have the same name... I'm pretty sure...
}
function mapperInstance(view: EndpointDef): string {
return `${lowercaseFirstLetter(view.component)}Mapper`;
}
export {
createServiceFunc
}