Christian Fraß

Christian Fraß commited on 2021-03-08 23:03:32
Zeige 9 geänderte Dateien mit 208 Einfügungen und 13 Löschungen.

... ...
@@ -0,0 +1,28 @@
1
+api.add_action<
2
+	{
3
+		type_id : int;
4
+		description ?: null|string;
5
+		tag_ids ?: Array<int>;
6
+		expression_ids ?: Array<int>;
7
+	},
8
+	int
9
+>
10
+(
11
+	{
12
+		"name": "concept_add",
13
+		"execution": async function (input)
14
+		{
15
+			const concept_entity : entities.concept =
16
+			{
17
+				"type_id": input.type_id,
18
+				"description": helpers.object.fetch<null|string>(input, "description", null),
19
+				"tag_ids": input.tag_ids,
20
+				"expression_ids": input.expression_ids,
21
+			};
22
+			const concept_id : int = await services.concept.add(concept_entity);
23
+			
24
+			return Promise.resolve<int>(concept_id);
25
+		}
26
+	}
27
+);
28
+
... ...
@@ -0,0 +1,43 @@
1
+api.add_action<
2
+	{
3
+		language_from : string;
4
+		language_to : string;
5
+		token : string;
6
+	},
7
+	Array<
8
+		{
9
+			language_from : string;
10
+			value_from : string;
11
+			language_to : string;
12
+			value_to : string;
13
+		}
14
+	>
15
+>
16
+(
17
+	{
18
+		"name": "translate",
19
+		"execution": async function (input)
20
+		{
21
+			const rows : Array<type_row> = await services.concept.get_translations
22
+			(
23
+				input.language_from,
24
+				input.language_to,
25
+				input.token
26
+			);
27
+			return (
28
+				rows.map
29
+				(
30
+					row => (
31
+						{
32
+							"language_from": row["language_from"],
33
+							"value_from": row["value_from"],
34
+							"language_to": row["language_to"],
35
+							"value_to": row["value_to"],
36
+						}
37
+					)
38
+				)
39
+			);
40
+		}
41
+	}
42
+);
43
+
... ...
@@ -0,0 +1,50 @@
1
+namespace api
2
+{
3
+	
4
+	/**
5
+	 */
6
+	export type type_action<type_input, type_output> =
7
+	{
8
+		name : string;
9
+		execution : (inupt : type_input)=>Promise<type_output>;
10
+	};
11
+	
12
+	
13
+	/**
14
+	 */
15
+	var _actions : {[name : string] : type_action<any, any>} = {};
16
+	
17
+	
18
+	/**
19
+	 */
20
+	export function add_action<type_input, type_output>
21
+	(
22
+		action : type_action<type_input, type_output>
23
+	) : void
24
+	{
25
+		_actions[action.name] = action;
26
+	}
27
+	
28
+	
29
+	/**
30
+	 */
31
+	export async function query
32
+	(
33
+		action_name : string,
34
+		input : any
35
+	) : Promise<any>
36
+	{
37
+		if (! _actions.hasOwnProperty(action_name))
38
+		{
39
+			return Promise.reject<any>("not such action");
40
+		}
41
+		else
42
+		{
43
+			const action : type_action<any, any> = _actions[action_name];
44
+			const output : any = await action.execution(input);
45
+			return Promise.resolve<any>(output);
46
+		}
47
+	}
48
+	
49
+}
50
+
... ...
@@ -5,10 +5,10 @@ namespace entities
5 5
 	 */
6 6
 	export type concept =
7 7
 	{
8
+		description : null|string;
8 9
 		type_id : int;
9
-		description : string;
10
-		tags : Array<int>;
11
-		expressions : Array<int>;
10
+		tag_ids : Array<int>;
11
+		expression_ids : Array<int>;
12 12
 	};
13 13
 	
14 14
 }
... ...
@@ -0,0 +1,24 @@
1
+namespace helpers.object
2
+{
3
+	
4
+	/**
5
+	 */
6
+	export function fetch<type_value>
7
+	(
8
+		object : {[field : string] : any},
9
+		field : string,
10
+		fallback : type_value
11
+	) : type_value
12
+	{
13
+		if (object.hasOwnProperty(field))
14
+		{
15
+			return object[field];
16
+		}
17
+		else
18
+		{
19
+			return fallback;
20
+		}
21
+	}
22
+	
23
+}
24
+
... ...
@@ -1,5 +1,3 @@
1
-#!/usr/bin/env node
2
-
3 1
 /*
4 2
 import {repositories.language} from './repository-language';
5 3
 import {repositories.type} from './repository-type';
... ...
@@ -32,6 +30,46 @@ async function main(args : Array<string>) : Promise<void>
32 30
 			process.exit(0);
33 31
 			break;
34 32
 		}
33
+		case "run":
34
+		{
35
+			const server : lib_comm.interface_server<string, lib_comm.type_response_server_http> = new lib_comm.class_server_http
36
+			(
37
+				{
38
+					"port": 7777,
39
+				}
40
+			);
41
+			server.setup
42
+			(
43
+				async (input_raw) =>
44
+				{
45
+					const stuff : any = JSON.parse(input_raw);
46
+					try
47
+					{
48
+						const output : any = await api.query(stuff.action, stuff.input);
49
+						return Promise.resolve<lib_comm.type_response_server_http>
50
+						(
51
+							{
52
+								"code": 200,
53
+								"text": JSON.stringify(output),
54
+							}
55
+						);
56
+					}
57
+					catch (exception)
58
+					{
59
+						console.error(exception);
60
+						return Promise.resolve<lib_comm.type_response_server_http>
61
+						(
62
+							{
63
+								"code": 500,
64
+								"text": 'error',
65
+							}
66
+						);
67
+					}
68
+				}
69
+			);
70
+			server.run();
71
+			break;
72
+		}
35 73
 		case "show":
36 74
 		{
37 75
 			const concept_id = parseInt(args.shift());
... ...
@@ -277,12 +277,12 @@ namespace repositories
277 277
 		{
278 278
 			const row_core : type_row = {"type_id": concept_entity.type_id, "description": concept_entity.description};
279 279
 			const concept_id : int = await concept_core.create(row_core);
280
-			for await (let tag_id of concept_entity.tags)
280
+			for await (const tag_id of concept_entity.tag_ids)
281 281
 			{
282 282
 				const row_tag : type_row = {"concept_id": concept_id, "tag_id": tag_id};
283 283
 				const concept_tag_id : int = await concept_tags.create(row_tag);
284 284
 			}
285
-			for await (let expression_id of concept_entity.expressions)
285
+			for await (const expression_id of concept_entity.expression_ids)
286 286
 			{
287 287
 				const row_expressions : type_row = {"concept_id": concept_id, "expression_id": expression_id};
288 288
 				const concept_expressions_id : int = await concept_expressions.create(row_expressions);
... ...
@@ -309,8 +309,8 @@ namespace repositories
309 309
 			{
310 310
 				"type_id": row_core["type_id"],
311 311
 				"description": row_core["description"],
312
-				"tags": rows_tags.map(row_tag => row_tag["tag_id"]),
313
-				"expressions": rows_expressions.map(row_expression => row_expression["expression_id"]),
312
+				"tag_ids": rows_tags.map(row_tag => row_tag["tag_id"]),
313
+				"expression_ids": rows_expressions.map(row_expression => row_expression["expression_id"]),
314 314
 			};
315 315
 			return Promise.resolve<entities.concept>(concept_entity);
316 316
 		},
... ...
@@ -46,7 +46,8 @@ namespace services.concept
46 46
 	
47 47
 	/**
48 48
 	 */
49
-	function parse_tags(
49
+	function parse_tags
50
+	(
50 51
 		tags_raw : string
51 52
 	) : Array<string>
52 53
 	{
... ...
@@ -80,6 +81,17 @@ namespace services.concept
80 81
 	}
81 82
 	
82 83
 	
84
+	/**
85
+	 */
86
+	export function add
87
+	(
88
+		concept_entity : entities.concept
89
+	) : Promise<int>
90
+	{
91
+		return repositories.concept.create(concept_entity);
92
+	}
93
+	
94
+	
83 95
 	/**
84 96
 	 */
85 97
 	export async function bloat
... ...
@@ -101,12 +113,12 @@ namespace services.concept
101 113
 			"tags": [],
102 114
 			"expressions": [],
103 115
 		};
104
-		for await (const tag_id of concept_entity.tags)
116
+		for await (const tag_id of concept_entity.tag_ids)
105 117
 		{
106 118
 			const tag_value : string = (await repositories.tag.read(tag_id))["value"];
107 119
 			result.tags.push({"id": tag_id, "value": tag_value});
108 120
 		}
109
-		for await (const expression_id of concept_entity.expressions)
121
+		for await (const expression_id of concept_entity.expression_ids)
110 122
 		{
111 123
 			const expression_row : type_row = await repositories.expression.read(expression_id);
112 124
 			const language_id : int = expression_row["language_id"];
... ...
@@ -1,3 +1,3 @@
1
-type int = number;
1
+// type int = number;
2 2
 type type_row = {[field : string] : any};	
3 3
 
4 4