diff --git a/backend-assemblyscript/step0-framework/run.sh b/backend-assemblyscript/step0-framework/run.sh index b5c3e3c..55cd13f 100755 --- a/backend-assemblyscript/step0-framework/run.sh +++ b/backend-assemblyscript/step0-framework/run.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e +set -o pipefail mkdir -p wasm diff --git a/backend-assemblyscript/step1-json-api/assembly/main.ts b/backend-assemblyscript/step1-json-api/assembly/main.ts index da680b0..b569052 100644 --- a/backend-assemblyscript/step1-json-api/assembly/main.ts +++ b/backend-assemblyscript/step1-json-api/assembly/main.ts @@ -19,7 +19,7 @@ export function handler(input: string): string { } else if (request.action == Action.Fetch) { let fetch = request as FetchRequest; - let result = getMessages(fetch.username); + let result = getMessages(fetch.username, fetch.offset, fetch.count); return result; } diff --git a/backend-assemblyscript/step1-json-api/assembly/model.ts b/backend-assemblyscript/step1-json-api/assembly/model.ts index ad09718..4fd0db0 100644 --- a/backend-assemblyscript/step1-json-api/assembly/model.ts +++ b/backend-assemblyscript/step1-json-api/assembly/model.ts @@ -6,7 +6,7 @@ export function createScheme(): void { export function addMessage(message: string, username: string): void { log("add message"); } -export function getMessages(username: string | null): string { +export function getMessages(username: string | null, offset: u32, count: u32): string { log("get messages"); return "[]"; } diff --git a/backend-assemblyscript/step1-json-api/assembly/request.ts b/backend-assemblyscript/step1-json-api/assembly/request.ts index c47ae83..4eeb80e 100644 --- a/backend-assemblyscript/step1-json-api/assembly/request.ts +++ b/backend-assemblyscript/step1-json-api/assembly/request.ts @@ -26,11 +26,15 @@ export class PostRequest extends Request { export class FetchRequest extends Request { public readonly username: string | null; + public readonly offset: u32; + public readonly count: u32; - constructor(username: string | null) { + constructor(username: string | null, offset: u32, count: u32) { super(); this.action = Action.Fetch; this.username = username; + this.offset = offset; + this.count = count; } } @@ -46,21 +50,21 @@ export function string2Bytes(str: string): Uint8Array { } export function decode(input: string): Request { - let jsonHandler = new RequestJSONEventsHandler(); - let decoder = new JSONDecoder(jsonHandler); + let json = new RequestJSONEventsHandler(); + let decoder = new JSONDecoder(json); let bytes = string2Bytes(input); decoder.deserialize(bytes); - let action = jsonHandler.action; + let action = json.action; let request: Request; if (action == "Fetch") { - request = new FetchRequest(jsonHandler.filter_handle); + request = new FetchRequest(json.filter_handle, json.offset, json.count); } else if (action == "Post") { - request = new PostRequest(jsonHandler.message, jsonHandler.username) + request = new PostRequest(json.message, json.username) } else { request = new UnknownRequest() } @@ -74,8 +78,12 @@ class RequestJSONEventsHandler extends JSONHandler { public message: string; public username: string; public filter_handle: string | null; + public offset: u32; + public count: u32; setString(name: string, value: string): void { + this.offset = 0; + this.count = 0; if (name == "action") { this.action = value; @@ -84,6 +92,10 @@ class RequestJSONEventsHandler extends JSONHandler { } else if (name == "username") { this.username = value; this.filter_handle = value; + } else if (name == "offset") { + this.offset = U32.parseInt(value); + } else if (name == "count") { + this.count = U32.parseInt(value); } // json scheme is not strict, so we won't throw an error on excess fields } diff --git a/backend-assemblyscript/step1-json-api/run.sh b/backend-assemblyscript/step1-json-api/run.sh index 8af0698..a19af16 100755 --- a/backend-assemblyscript/step1-json-api/run.sh +++ b/backend-assemblyscript/step1-json-api/run.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e +set -o pipefail mkdir -p wasm diff --git a/backend-assemblyscript/step2-database-only/run.sh b/backend-assemblyscript/step2-database-only/run.sh index 79d52d7..bc15ab9 100755 --- a/backend-assemblyscript/step2-database-only/run.sh +++ b/backend-assemblyscript/step2-database-only/run.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e +set -o pipefail mkdir -p wasm diff --git a/backend-assemblyscript/step3-finished-app/assembly/main.ts b/backend-assemblyscript/step3-finished-app/assembly/main.ts index f077d6a..8db8927 100644 --- a/backend-assemblyscript/step3-finished-app/assembly/main.ts +++ b/backend-assemblyscript/step3-finished-app/assembly/main.ts @@ -19,7 +19,7 @@ export function handler(input: string): string { } else if (request.action == Action.Fetch) { let fetch = request as FetchRequest; - let result = getMessages(fetch.username); + let result = getMessages(fetch.username, fetch.offset, fetch.count); let response = decodePosts(result); return response.serialize(); diff --git a/backend-assemblyscript/step3-finished-app/assembly/model.ts b/backend-assemblyscript/step3-finished-app/assembly/model.ts index ec14ab3..34a503f 100644 --- a/backend-assemblyscript/step3-finished-app/assembly/model.ts +++ b/backend-assemblyscript/step3-finished-app/assembly/model.ts @@ -10,19 +10,21 @@ export function addMessage(message: string, username: string): void { query(request); } -export function getMessages(username: string | null): string { +export function getMessages(username: string | null, offset: u32, count: u32): string { + let limitClause = ` LIMIT ` + count.toString() + ` OFFSET ` + offset.toString() + ` `; if (username) { + let whereClause = ` WHERE username = "` + username + `" `; let request = `SELECT json_group_array( json_object('message', message, 'username', username) ) AS json_result FROM - (SELECT * FROM messages WHERE username = "` + username + `")`; + (SELECT * FROM messages` + whereClause + limitClause + `)`; return query(request); } else { let request = `SELECT json_group_array( json_object('message', message, 'username', username) - ) AS json_result FROM (SELECT * FROM messages)`; + ) AS json_result FROM (SELECT * FROM messages` + limitClause + `)`; return query(request); } } diff --git a/backend-assemblyscript/step3-finished-app/assembly/request.ts b/backend-assemblyscript/step3-finished-app/assembly/request.ts index c47ae83..4eeb80e 100644 --- a/backend-assemblyscript/step3-finished-app/assembly/request.ts +++ b/backend-assemblyscript/step3-finished-app/assembly/request.ts @@ -26,11 +26,15 @@ export class PostRequest extends Request { export class FetchRequest extends Request { public readonly username: string | null; + public readonly offset: u32; + public readonly count: u32; - constructor(username: string | null) { + constructor(username: string | null, offset: u32, count: u32) { super(); this.action = Action.Fetch; this.username = username; + this.offset = offset; + this.count = count; } } @@ -46,21 +50,21 @@ export function string2Bytes(str: string): Uint8Array { } export function decode(input: string): Request { - let jsonHandler = new RequestJSONEventsHandler(); - let decoder = new JSONDecoder(jsonHandler); + let json = new RequestJSONEventsHandler(); + let decoder = new JSONDecoder(json); let bytes = string2Bytes(input); decoder.deserialize(bytes); - let action = jsonHandler.action; + let action = json.action; let request: Request; if (action == "Fetch") { - request = new FetchRequest(jsonHandler.filter_handle); + request = new FetchRequest(json.filter_handle, json.offset, json.count); } else if (action == "Post") { - request = new PostRequest(jsonHandler.message, jsonHandler.username) + request = new PostRequest(json.message, json.username) } else { request = new UnknownRequest() } @@ -74,8 +78,12 @@ class RequestJSONEventsHandler extends JSONHandler { public message: string; public username: string; public filter_handle: string | null; + public offset: u32; + public count: u32; setString(name: string, value: string): void { + this.offset = 0; + this.count = 0; if (name == "action") { this.action = value; @@ -84,6 +92,10 @@ class RequestJSONEventsHandler extends JSONHandler { } else if (name == "username") { this.username = value; this.filter_handle = value; + } else if (name == "offset") { + this.offset = U32.parseInt(value); + } else if (name == "count") { + this.count = U32.parseInt(value); } // json scheme is not strict, so we won't throw an error on excess fields } diff --git a/backend-assemblyscript/step3-finished-app/run.sh b/backend-assemblyscript/step3-finished-app/run.sh index 501671b..b244d6d 100755 --- a/backend-assemblyscript/step3-finished-app/run.sh +++ b/backend-assemblyscript/step3-finished-app/run.sh @@ -1,6 +1,7 @@ #!/usr/bin/env bash set -e +set -o pipefail command -v jq >/dev/null 2>&1 || { echo >&2 "jq is not installed, wouldn't parse responses" @@ -64,7 +65,7 @@ echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE" # Assign json to a variable using heredoc technique JSON=$(cat <