rename lots of things

This commit is contained in:
folex 2019-08-16 12:00:50 +03:00
parent 9dd2a2d9c8
commit 50747bbcd3
47 changed files with 31 additions and 227 deletions

View File

@ -1,38 +0,0 @@
import {Action, decode, FetchRequest, PostRequest} from "./request";
import {PostResponse, Message, FetchResponse, UnknownResponse} from "./response";
let posts: Array<Message> = new Array<Message>();
// main handler for an application
export function handler(input: string): string {
let request = decode(input);
if (request.action == Action.Post) {
let post = request as PostRequest;
posts.push(new Message(post.msg, post.username));
let response = new PostResponse(posts.length);
return response.serialize()
} else if (request.action == Action.Fetch) {
let fetch = request as FetchRequest;
var messages: Array<Message>;
if (fetch.filter_handle == null) {
messages = posts
} else {
let filter_handle = fetch.filter_handle as string;
let filtered = new Array<Message>();
for (let i = 0; i < posts.length; i++) {
let message = posts[i];
if (message.username == filter_handle) {
filtered.push(message)
}
}
messages = filtered;
}
let response = new FetchResponse(messages);
return response.serialize()
}
let response = new UnknownResponse();
return response.serialize();
}

View File

@ -1,91 +0,0 @@
import {JSONDecoder, JSONHandler} from "../node_modules/assemblyscript-json/assembly/decoder";
export enum Action {
Post,
Fetch,
Unknown
// Error
}
export abstract class Request {
public action: Action = Action.Unknown;
}
export class PostRequest extends Request {
public readonly msg: string;
public readonly username: string;
constructor(msg: string, username: string) {
super();
this.msg = msg;
this.username = username;
this.action = Action.Post;
}
}
export class FetchRequest extends Request {
public readonly filter_handle: string | null;
constructor(filter_handle: string | null) {
super();
this.action = Action.Fetch;
this.filter_handle = filter_handle;
}
}
export class UnknownRequest extends Request {
constructor() {
super();
this.action = Action.Unknown;
}
}
export function string2Bytes(str: string): Uint8Array {
return Uint8Array.wrap(String.UTF8.encode(str));
}
export function decode(input: string): Request {
let jsonHandler = new RequestJSONEventsHandler();
let decoder = new JSONDecoder<RequestJSONEventsHandler>(jsonHandler);
let bytes = string2Bytes(input);
decoder.deserialize(bytes);
let action = jsonHandler.action;
let request: Request;
if (action == "Fetch") {
request = new FetchRequest(jsonHandler.filter_handle);
} else if (action == "Post") {
request = new PostRequest(jsonHandler.msg, jsonHandler.username)
} else {
request = new UnknownRequest()
}
return request;
}
class RequestJSONEventsHandler extends JSONHandler {
public action: string;
public msg: string;
public username: string;
public filter_handle: string | null;
setString(name: string, value: string): void {
if (name == "action") {
this.action = value;
} else if (name == "msg") {
this.msg = value;
} else if (name == "username") {
this.username = value;
} else if (name == "filter_handle") {
this.filter_handle = value;
}
// json scheme is not strict, so we won't throw an error on excess fields
}
}

View File

@ -1,79 +0,0 @@
import {JSONEncoder} from "../node_modules/assemblyscript-json/assembly/encoder";
export abstract class Response {
serialize(): string {
unreachable();
return "";
};
}
export class Message {
msg: string;
username: string;
constructor(msg: string, username: string) {
this.msg = msg;
this.username = username;
}
}
export class UnknownResponse extends Response {
constructor() {
super();
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Unknown");
encoder.setString("msg", "cannot username request");
encoder.popObject();
return encoder.toString();
}
}
export class PostResponse extends Response {
msgCount: i32;
constructor(msgCount: i32) {
super();
this.msgCount = msgCount;
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Post");
encoder.setInteger("count", this.msgCount);
encoder.popObject();
return encoder.toString();
}
}
export class FetchResponse extends Response {
posts: Array<Message>;
constructor(posts: Array<Message>) {
super();
this.posts = posts;
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Fetch");
encoder.pushArray("posts");
for (let i = 0; i < this.posts.length; i++) {
let twit = this.posts[i];
encoder.pushObject(null);
encoder.setString("msg", twit.msg);
encoder.setString("username", twit.username);
encoder.popObject();
}
encoder.popArray();
encoder.popObject();
return encoder.toString();
}
}

View File

@ -4,7 +4,6 @@ export enum Action {
Post, Post,
Fetch, Fetch,
Unknown Unknown
// Error
} }
export abstract class Request { export abstract class Request {
@ -38,7 +37,7 @@ export class UnknownRequest extends Request {
} }
} }
export function string2Bytes(str: string): Uint8Array { function string2Bytes(str: string): Uint8Array {
return Uint8Array.wrap(String.UTF8.encode(str)); return Uint8Array.wrap(String.UTF8.encode(str));
} }

View File

@ -7,16 +7,6 @@ export abstract class Response {
}; };
} }
export class Twit {
msg: string;
username: string;
constructor(msg: string, username: string) {
this.msg = msg;
this.username = username;
}
}
export class UnknownResponse extends Response { export class UnknownResponse extends Response {
constructor() { constructor() {
super(); super();

View File

@ -0,0 +1,22 @@
import {log} from "../node_modules/assemblyscript-sdk/assembly/logger";
import {query} from "../node_modules/db-connector/assembly/sqlite"
// main handler for an application
export function handler(username: string): string {
// Create table for messages storage
query(`CREATE TABLE messages(msg text, username text)`);
// Insert message 'Hello, username!' using `username` as author's username
query(`INSERT INTO messages VALUES("Hello, username!","` + username + `")`);
// Get all messages
let messages = query(`SELECT * FROM messages`);
log("messages: " + messages);
// Get all messages as JSON via SQLite's JSON extension
return query(
`SELECT json_group_array(
json_object('msg', msg, 'username', username)
) AS json_result FROM (SELECT * FROM messages)`
)
}

View File

@ -140,7 +140,7 @@
"optional": true "optional": true
}, },
"db-connector": { "db-connector": {
"version": "github:fluencelabs/db-connector#164fca2e43c1aeef6070dee77d32c97cff79afc0", "version": "github:fluencelabs/db-connector#493612495f4f7a88c7f31bede4153ca719a96b7c",
"from": "github:fluencelabs/db-connector", "from": "github:fluencelabs/db-connector",
"dev": true "dev": true
}, },

View File

@ -14,6 +14,7 @@
"assemblyscript": "github:assemblyscript/assemblyscript", "assemblyscript": "github:assemblyscript/assemblyscript",
"assemblyscript-json": "github:fluencelabs/assemblyscript-json#update-as1", "assemblyscript-json": "github:fluencelabs/assemblyscript-json#update-as1",
"assemblyscript-sdk": "github:fluencelabs/assemblyscript-sdk", "assemblyscript-sdk": "github:fluencelabs/assemblyscript-sdk",
"db-connector": "github:fluencelabs/db-connector",
"@as-pect/assembly": "^2.3.1", "@as-pect/assembly": "^2.3.1",
"@as-pect/cli": "^2.3.1", "@as-pect/cli": "^2.3.1",
"@as-pect/core": "^2.3.1" "@as-pect/core": "^2.3.1"

View File

@ -51,7 +51,7 @@ fn fetch_posts(username: Option<String>) -> AppResult<Response> {
// Get all posts if no filter username was passed // Get all posts if no filter username was passed
None => model::get_all_posts()?, None => model::get_all_posts()?,
// Or get only posts from specified author // Or get only posts from specified author
Some(h) => model::get_posts_by_handle(h)?, Some(h) => model::get_posts_by_username(h)?,
}; };
// Some Rust-specific detail to play nice with serialization, doesn't matter // Some Rust-specific detail to play nice with serialization, doesn't matter

View File

@ -34,7 +34,7 @@ pub fn get_all_posts() -> AppResult<String> {
.map_err(|e| err_msg(&format!("Error retrieving posts: {}", e))) .map_err(|e| err_msg(&format!("Error retrieving posts: {}", e)))
} }
pub fn get_posts_by_handle(username: String) -> AppResult<String> { pub fn get_posts_by_username(username: String) -> AppResult<String> {
database::query(format!( database::query(format!(
"SELECT json_group_array( "SELECT json_group_array(
json_object('msg', msg, 'username', username) json_object('msg', msg, 'username', username)

View File

@ -49,7 +49,7 @@ fn fetch_posts(username: Option<String>) -> AppResult<Response> {
// Get all posts if no filter username was passed // Get all posts if no filter username was passed
None => model::get_all_posts()?, None => model::get_all_posts()?,
// Or get only posts from specified author // Or get only posts from specified author
Some(h) => model::get_posts_by_handle(h)?, Some(h) => model::get_posts_by_username(h)?,
}; };
// Some Rust-specific detail to play nice with serialization, doesn't matter // Some Rust-specific detail to play nice with serialization, doesn't matter

View File

@ -14,7 +14,7 @@ pub fn get_all_posts() -> AppResult<String> {
Ok("[]".to_string()) Ok("[]".to_string())
} }
pub fn get_posts_by_handle(username: String) -> AppResult<String> { pub fn get_posts_by_username(username: String) -> AppResult<String> {
log::info!("get all posts by username {}", username); log::info!("get all posts by username {}", username);
Ok("[]".to_string()) Ok("[]".to_string())
} }

View File

@ -51,7 +51,7 @@ fn fetch_posts(username: Option<String>) -> AppResult<Response> {
// Get all posts if no filter username was passed // Get all posts if no filter username was passed
None => model::get_all_posts()?, None => model::get_all_posts()?,
// Or get only posts from specified author // Or get only posts from specified author
Some(h) => model::get_posts_by_handle(h)?, Some(h) => model::get_posts_by_username(h)?,
}; };
// Some Rust-specific detail to play nice with serialization, doesn't matter // Some Rust-specific detail to play nice with serialization, doesn't matter

View File

@ -34,7 +34,7 @@ pub fn get_all_posts() -> AppResult<String> {
.map_err(|e| err_msg(&format!("Error retrieving posts: {}", e))) .map_err(|e| err_msg(&format!("Error retrieving posts: {}", e)))
} }
pub fn get_posts_by_handle(username: String) -> AppResult<String> { pub fn get_posts_by_username(username: String) -> AppResult<String> {
database::query(format!( database::query(format!(
"SELECT json_group_array( "SELECT json_group_array(
json_object('msg', msg, 'username', username) json_object('msg', msg, 'username', username)