| 5 | import { DefaultBlockInput, IFunction, FullTemplateModel, MacroModel } from './template.model' |
| 6 | |
| 7 | export default class TemplateController { |
| 8 | public async getTemplateInitialisedComponentList(id: string): Promise<ServiceResponse<IFunction[] | null>> { |
| 9 | try { |
| 10 | if (!id || id === 'undefined') { |
| 11 | return new ServiceResponse(ResponseStatus.Failed, 'ID was not provided in the query', null, StatusCodes.BAD_REQUEST) |
| 12 | } |
| 13 | |
| 14 | const blockData = await FullTemplateModel.findOne({ folder: id }) |
| 15 | |
| 16 | if (!blockData) { |
| 17 | return new ServiceResponse(ResponseStatus.Failed, 'Template not found', null, StatusCodes.NOT_FOUND) |
| 18 | } |
| 19 | |
| 20 | const indexData: string[] = blockData.startupBlocks |
| 21 | |
| 22 | const functions: IFunction[] = blockData.functions.map((block: IFunction) => { |
| 23 | if (indexData.includes(block.function)) { |
| 24 | return block |
| 25 | } |
| 26 | }) |
| 27 | |
| 28 | return new ServiceResponse<IFunction[]>(ResponseStatus.Success, 'Success', functions, StatusCodes.OK) |
| 29 | } catch (ex) { |
| 30 | return new ServiceResponse(ResponseStatus.Failed, 'Failed to get template', null, StatusCodes.INTERNAL_SERVER_ERROR) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | public async getTemplateMacros(body: DefaultBlockInput[]): Promise<ServiceResponse<string | null>> { |
| 35 | try { |
| 36 | if (!body) { |
| 37 | return new ServiceResponse(ResponseStatus.Failed, 'Body is empty', null, StatusCodes.BAD_REQUEST) |
| 38 | } |
| 39 | |
| 40 | let macros = ''; |
| 41 | for (const block of body) { |
| 42 | const {folder, function: name} = block; |
| 43 | const macro = await MacroModel.findOne({ |
| 44 | folder: folder, |
| 45 | name: name |
| 46 | }) |
| 47 | if (macro) { |
| 48 | macros += macro.content; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return new ServiceResponse<string>(ResponseStatus.Success, 'Success', macros, StatusCodes.OK) |
| 53 | } catch (ex) { |
| 54 | return new ServiceResponse(ResponseStatus.Failed, 'Failed to get template macros', null, StatusCodes.INTERNAL_SERVER_ERROR) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public async getTemplatePreview(id: string): Promise<ServiceResponse<string | null>> { |
| 59 | try { |
| 60 | if (!id || id == 'undefined') return new ServiceResponse(ResponseStatus.Success, 'Success', '', StatusCodes.OK) |
| 61 | |
| 62 | const filePath = `./public/${id}/preview.md` |
| 63 | if (!fs.existsSync(filePath)) { |
| 64 | return new ServiceResponse(ResponseStatus.Failed, 'Preview not found', null, StatusCodes.NOT_FOUND) |
nothing calls this directly
no outgoing calls
no test coverage detected