git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
faf7e12
Branches
Tags
develop-client_server
master
typescript
fs-words.git
server
source
services
concept.ts
[mov] server
Christian Fraß
commited
faf7e12
at 2021-03-08 23:05:42
concept.ts
Blame
History
Raw
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<string>; expressions : {[language : string] : Array<string>}; }; /** */ function parse_tags ( tags_raw : string ) : Array<string> { return ( (tags_raw === null) ? [] : tags_raw.split(",") ); } /** */ function parse_expressions ( expressions_raw : string ) : {[language : string] : Array<string>} { let result : {[language : string] : Array<string>} = {}; const parts : Array<string> = expressions_raw.split(","); parts.forEach ( (part) => { const [language, value] : Array<string> = part.split(":", 2); if (! result.hasOwnProperty(language)) result[language] = []; result[language].push(value); } ); return result; } /** */ export function add ( concept_entity : entities.concept ) : Promise<int> { return repositories.concept.create(concept_entity); } /** */ export async function bloat ( concept_id : int ) : Promise<type_bloated> { 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.tag_ids) { 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.expression_ids) { 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<type_bloated>(result); } /** */ export async function expose ( concept_id : int ) : Promise<type_exposal> { const bloated : type_bloated = await bloat(concept_id); return Promise.resolve<type_exposal> ( { "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<string>} = {}; 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<Array<type_exposal>> { const rows : Array<type_row> = await repositories.concept.export(); return Promise.resolve<Array<type_exposal>> ( 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<Array<type_exposal>> { const concept_ids : Array<int> = await repositories.concept.search(part); let result : Array<type_exposal> = []; for await (const concept_id of concept_ids) { result.push(await expose(concept_id)); } return Promise.resolve<Array<type_exposal>>(result); } /** */ export function get_translations ( language_from : string, language_to : string, part : string ) : Promise<Array<type_row>> { return repositories.concept.get_translations ( language_from, language_to, part ); } /** */ export async function suck ( concept_thing : any ) : Promise<int> { 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 <Array<string>>(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; } }