handle => username

This commit is contained in:
folex
2019-08-15 19:10:13 +03:00
parent 1a07013791
commit ddf235d759
27 changed files with 119 additions and 119 deletions

View File

@@ -16,6 +16,6 @@ export function deallocate(ptr: i32, size: usize): void {
// Returns pointer on a response.
export function invoke(ptr: i32, size: i32): i32 {
// this function will parse a request as a string and return result string as a pointer in memory
// you can look on other functions in 'assemblyscript-sdk' library to handle own types of requests and responses
// you can look on other functions in 'assemblyscript-sdk' library to username own types of requests and responses
return loggedStringHandler(ptr, size, handler, log);
}

View File

@@ -8,8 +8,8 @@ describe("example", () => {
log<u8>(244); // integers!
log<u64>(0xFFFFFFFF); // long values!
log<string>(handler('{"hi": "hi"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "handle": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "handle": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "username": "John Doe"}'));
log<string>(handler('{"action": "Fetch"}'));
});
});

View File

@@ -17,6 +17,6 @@ export function deallocate(ptr: i32, size: usize): void {
// Returns pointer on a response.
export function invoke(ptr: i32, size: i32): i32 {
// this function will parse a request as a string and return result string as a pointer in memory
// you can look on other functions in 'assemblyscript-sdk' library to handle own types of requests and responses
// you can look on other functions in 'assemblyscript-sdk' library to username own types of requests and responses
return loggedStringHandler(ptr, size, handler, log);
}

View File

@@ -14,12 +14,12 @@ export abstract class Request {
export class PostRequest extends Request {
public readonly msg: string;
public readonly handle: string;
public readonly username: string;
constructor(msg: string, handle: string) {
constructor(msg: string, username: string) {
super();
this.msg = msg;
this.handle = handle;
this.username = username;
this.action = Action.Post;
}
}
@@ -57,7 +57,7 @@ export function decode(input: string): Request {
if (action == "Fetch") {
request = new FetchRequest();
} else if (action == "Post") {
request = new PostRequest(jsonHandler.msg, jsonHandler.handle)
request = new PostRequest(jsonHandler.msg, jsonHandler.username)
} else {
request = new UnknownRequest()
}
@@ -69,7 +69,7 @@ class RequestJSONEventsHandler extends JSONHandler {
public action: string;
public msg: string;
public handle: string;
public username: string;
setString(name: string, value: string): void {
@@ -77,8 +77,8 @@ class RequestJSONEventsHandler extends JSONHandler {
this.action = value;
} else if (name == "msg") {
this.msg = value;
} else if (name == "handle") {
this.handle = value;
} else if (name == "username") {
this.username = value;
}
// json scheme is not strict, so we won't throw an error on excess fields
}

View File

@@ -9,11 +9,11 @@ export abstract class Response {
export class Twit {
msg: string;
handle: string;
username: string;
constructor(msg: string, handle: string) {
constructor(msg: string, username: string) {
this.msg = msg;
this.handle = handle;
this.username = username;
}
}
@@ -26,7 +26,7 @@ export class UnknownResponse extends Response {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
encoder.setString("msg", "cannot handle request");
encoder.setString("msg", "cannot username request");
encoder.popObject();
return encoder.toString();

View File

@@ -4,10 +4,10 @@ describe("example", () => {
it("can log some values to the console", () => {
log<string>(handler('{"hi": "hi"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "handle": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "handle": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "How is it going?", "handle": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Excellent! Thanks!", "handle": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "username": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "How is it going?", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Excellent! Thanks!", "username": "John Doe"}'));
log<string>(handler('{"action": "Fetch"}'));
log<string>(handler('{"action": "Fetch", "filter_handle": "John Doe"}'));
log<string>(handler('{"action": "Fetch", "filter_handle": "fluencer"}'));

View File

@@ -17,6 +17,6 @@ export function deallocate(ptr: i32, size: usize): void {
// Returns pointer on a response.
export function invoke(ptr: i32, size: i32): i32 {
// this function will parse a request as a string and return result string as a pointer in memory
// you can look on other functions in 'assemblyscript-sdk' library to handle own types of requests and responses
// you can look on other functions in 'assemblyscript-sdk' library to username own types of requests and responses
return loggedStringHandler(ptr, size, handler, log);
}

View File

@@ -10,7 +10,7 @@ export function handler(input: string): string {
if (request.action == Action.Post) {
let post = request as PostRequest;
posts.push(new Message(post.msg, post.handle));
posts.push(new Message(post.msg, post.username));
let response = new PostResponse(posts.length);
return response.serialize()
} else if (request.action == Action.Fetch) {
@@ -23,7 +23,7 @@ export function handler(input: string): string {
let filtered = new Array<Message>();
for (let i = 0; i < posts.length; i++) {
let message = posts[i];
if (message.handle == filter_handle) {
if (message.username == filter_handle) {
filtered.push(message)
}
}

View File

@@ -14,12 +14,12 @@ export abstract class Request {
export class PostRequest extends Request {
public readonly msg: string;
public readonly handle: string;
public readonly username: string;
constructor(msg: string, handle: string) {
constructor(msg: string, username: string) {
super();
this.msg = msg;
this.handle = handle;
this.username = username;
this.action = Action.Post;
}
}
@@ -60,7 +60,7 @@ export function decode(input: string): Request {
if (action == "Fetch") {
request = new FetchRequest(jsonHandler.filter_handle);
} else if (action == "Post") {
request = new PostRequest(jsonHandler.msg, jsonHandler.handle)
request = new PostRequest(jsonHandler.msg, jsonHandler.username)
} else {
request = new UnknownRequest()
}
@@ -72,7 +72,7 @@ class RequestJSONEventsHandler extends JSONHandler {
public action: string;
public msg: string;
public handle: string;
public username: string;
public filter_handle: string | null;
setString(name: string, value: string): void {
@@ -81,8 +81,8 @@ class RequestJSONEventsHandler extends JSONHandler {
this.action = value;
} else if (name == "msg") {
this.msg = value;
} else if (name == "handle") {
this.handle = value;
} else if (name == "username") {
this.username = value;
} else if (name == "filter_handle") {
this.filter_handle = value;
}

View File

@@ -9,11 +9,11 @@ export abstract class Response {
export class Message {
msg: string;
handle: string;
username: string;
constructor(msg: string, handle: string) {
constructor(msg: string, username: string) {
this.msg = msg;
this.handle = handle;
this.username = username;
}
}
@@ -26,7 +26,7 @@ export class UnknownResponse extends Response {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
encoder.setString("msg", "cannot handle request");
encoder.setString("msg", "cannot username request");
encoder.popObject();
return encoder.toString();
@@ -68,7 +68,7 @@ export class FetchResponse extends Response {
let twit = this.posts[i];
encoder.pushObject(null);
encoder.setString("msg", twit.msg);
encoder.setString("handle", twit.handle);
encoder.setString("username", twit.username);
encoder.popObject();
}
encoder.popArray();

View File

@@ -4,10 +4,10 @@ describe("example", () => {
it("can log some values to the console", () => {
log<string>(handler('{"hi": "hi"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "handle": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "handle": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "How is it going?", "handle": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Excellent! Thanks!", "handle": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, Fluence!", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Hello, fluencer!", "username": "John Doe"}'));
log<string>(handler('{"action": "Post", "msg": "How is it going?", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "msg": "Excellent! Thanks!", "username": "John Doe"}'));
log<string>(handler('{"action": "Fetch"}'));
log<string>(handler('{"action": "Fetch", "filter_handle": "John Doe"}'));
log<string>(handler('{"action": "Fetch", "filter_handle": "fluencer"}'));

View File

@@ -14,21 +14,21 @@ function doRequest(request: string): string {
}
export function initTables(): void {
let request = "CREATE TABLE messages(msg text, handle text)";
let request = "CREATE TABLE messages(msg text, username text)";
let resp = doRequest(request);
}
export function addMessage(msg: string, handle: string): void {
let request = "INSERT INTO messages VALUES(\"" + msg + "\", \"" + handle + "\")";
export function addMessage(msg: string, username: string): void {
let request = "INSERT INTO messages VALUES(\"" + msg + "\", \"" + username + "\")";
doRequest(request);
}
export function getMessages(handle: string | null): string {
if (handle) {
let request = "SELECT json_group_array(json_object('msg', msg, 'handle', handle)) AS json_result FROM (SELECT * FROM messages WHERE handle = '" + handle + "')";
export function getMessages(username: string | null): string {
if (username) {
let request = "SELECT json_group_array(json_object('msg', msg, 'username', username)) AS json_result FROM (SELECT * FROM messages WHERE username = '" + username + "')";
return doRequest(request);
} else {
let request = "SELECT json_group_array(json_object('msg', msg, 'handle', handle)) AS json_result FROM (SELECT * FROM messages)";
let request = "SELECT json_group_array(json_object('msg', msg, 'username', username)) AS json_result FROM (SELECT * FROM messages)";
return doRequest(request);
}
}

View File

@@ -17,6 +17,6 @@ export function deallocate(ptr: i32, size: usize): void {
// Returns pointer on a response.
export function invoke(ptr: i32, size: i32): i32 {
// this function will parse a request as a string and return result string as a pointer in memory
// you can look on other functions in 'assemblyscript-sdk' library to handle own types of requests and responses
// you can look on other functions in 'assemblyscript-sdk' library to username own types of requests and responses
return loggedStringHandler(ptr, size, handler, log);
}

View File

@@ -11,7 +11,7 @@ export function handler(input: string): string {
if (request.action == Action.Post) {
let post = request as PostRequest;
addMessage(post.msg, post.handle);
addMessage(post.msg, post.username);
let count = getPostsCount();
let response = new PostResponse(count);
@@ -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.handle);
let result = getMessages(fetch.username);
return result;
}

View File

@@ -14,23 +14,23 @@ export abstract class Request {
export class PostRequest extends Request {
public readonly msg: string;
public readonly handle: string;
public readonly username: string;
constructor(msg: string, handle: string) {
constructor(msg: string, username: string) {
super();
this.msg = msg;
this.handle = handle;
this.username = username;
this.action = Action.Post;
}
}
export class FetchRequest extends Request {
public readonly handle: string | null;
public readonly username: string | null;
constructor(handle: string | null) {
constructor(username: string | null) {
super();
this.action = Action.Fetch;
this.handle = handle;
this.username = username;
}
}
@@ -60,7 +60,7 @@ export function decode(input: string): Request {
if (action == "Fetch") {
request = new FetchRequest(jsonHandler.filter_handle);
} else if (action == "Post") {
request = new PostRequest(jsonHandler.msg, jsonHandler.handle)
request = new PostRequest(jsonHandler.msg, jsonHandler.username)
} else {
request = new UnknownRequest()
}
@@ -72,7 +72,7 @@ class RequestJSONEventsHandler extends JSONHandler {
public action: string;
public msg: string;
public handle: string;
public username: string;
public filter_handle: string | null;
setString(name: string, value: string): void {
@@ -81,8 +81,8 @@ class RequestJSONEventsHandler extends JSONHandler {
this.action = value;
} else if (name == "msg") {
this.msg = value;
} else if (name == "handle") {
this.handle = value;
} else if (name == "username") {
this.username = value;
this.filter_handle = value;
}
// json scheme is not strict, so we won't throw an error on excess fields

View File

@@ -9,11 +9,11 @@ export abstract class Response {
export class Message {
msg: string;
handle: string;
username: string;
constructor(msg: string, handle: string) {
constructor(msg: string, username: string) {
this.msg = msg;
this.handle = handle;
this.username = username;
}
}
@@ -26,7 +26,7 @@ export class UnknownResponse extends Response {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
encoder.setString("msg", "cannot handle request");
encoder.setString("msg", "cannot username request");
encoder.popObject();
return encoder.toString();
@@ -68,7 +68,7 @@ export class FetchResponse extends Response {
let twit = this.posts[i];
encoder.pushObject(null);
encoder.setString("msg", twit.msg);
encoder.setString("handle", twit.handle);
encoder.setString("username", twit.username);
encoder.popObject();
}
encoder.popArray();