namespace services.concept { /** */ export type type_bloated = { id : int; description : string; type : { id : int; value : string; }; tags : Array< { id : int; value : string; } >; expressions : Array< { id : int; language : { id : int; value : string; }; value : string; } >; }; /** */ export type type_exposal = { id : int; description : string; type : string; tags : Array; expressions : {[language : string] : Array}; }; /** */ function parse_tags( tags_raw : string ) : Array { return ( (tags_raw === null) ? [] : tags_raw.split(",") ); } /** */ function parse_expressions ( expressions_raw : string ) : {[language : string] : Array} { let result : {[language : string] : Array} = {}; const parts : Array = expressions_raw.split(","); parts.forEach ( (part) => { const [language, value] : Array = part.split(":", 2); if (! result.hasOwnProperty(language)) result[language] = []; result[language].push(value); } ); return result; } /** */ export async function bloat ( concept_id : int ) : Promise { const concept_entity : entities.concept = await repositories.concept.read(concept_id); const type_value : string = (await repositories.type.read(concept_entity.type_id))["value"]; let result : type_bloated = { "id": concept_id, "description": concept_entity.description, "type": { "id": concept_entity.type_id, "value": type_value, }, "tags": [], "expressions": [], }; for await (const tag_id of concept_entity.tags) { const tag_value : string = (await repositories.tag.read(tag_id))["value"]; result.tags.push({"id": tag_id, "value": tag_value}); } for await (const expression_id of concept_entity.expressions) { const expression_row : type_row = await repositories.expression.read(expression_id); const language_id : int = expression_row["language_id"]; const language_row : type_row = (await repositories.language.read(expression_row["language_id"])); result.expressions.push ( { "id": expression_id, "language": { "id": language_id, "value": language_row["value"] }, "value": expression_row["value"] } ); } return Promise.resolve(result); } /** */ export async function expose ( concept_id : int ) : Promise { const bloated : type_bloated = await bloat(concept_id); return Promise.resolve ( { "id": bloated.id, "description": bloated.description, "type": bloated.type.value, "tags": bloated.tags.map(tag_entry => tag_entry.value), "expressions": ( () => { let expressions : {[language : string] : Array} = {}; bloated.expressions.forEach ( expression_entry => { const language : string = expression_entry.language.value; if (! expressions.hasOwnProperty(language)) { expressions[language] = []; } expressions[language].push(expression_entry.value); } ); return expressions; } ) (), } ); } /** */ export async function export_ ( ) : Promise> { const rows : Array = await repositories.concept.export(); return Promise.resolve> ( rows.map ( (row) => ( { "id": row["id"], "type": row["type"], "description": row["description"], "tags": parse_tags(row["tags"]), "expressions": parse_expressions(row["expressions"]), } ) ) ); } /** */ export async function search ( part : string ) : Promise> { const concept_ids : Array = await repositories.concept.search(part); let result : Array = []; for await (const concept_id of concept_ids) { result.push(await expose(concept_id)); } return Promise.resolve>(result); } /** */ export function get_translations ( language_from : string, language_to : string, part : string ) : Promise> { return repositories.concept.get_translations ( language_from, language_to, part ); } /** */ export async function suck ( concept_thing : any ) : Promise { const type_id : int = await services.type.give(concept_thing["type"]); const concept_id : int = await repositories.concept_core.create ( { "type_id": type_id, "description": concept_thing["description"], } ); for await (const tag_value of concept_thing["tags"]) { const tag_id : int = await services.tag.give(tag_value); const concept_tag_id : int = await repositories.concept_tags.create ( { "concept_id": concept_id, "tag_id": tag_id, } ); } for await (const [language_value, expression_values] of Object.entries(concept_thing["expressions"])) { const language_id : int = await services.language.give(language_value); for await (const expression_value of >(expression_values)) { const expression_id : int = await services.expression.give(language_id, expression_value); const concept_expression_id : int = await repositories.concept_expressions.create ( { "concept_id": concept_id, "expression_id": expression_id, } ); } } return concept_id; } }