msg => message

This commit is contained in:
folex
2019-08-16 19:47:38 +03:00
parent 0fafa8661b
commit 8ca166aaf8
16 changed files with 64 additions and 68 deletions

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.username);
addMessage(post.message, post.username);
let count = getPostsCount();
let response = new PostResponse(count);

View File

@ -3,7 +3,7 @@ import {log} from "../node_modules/assemblyscript-sdk/assembly/logger";
export function createScheme(): void {
log("create scheme");
}
export function addMessage(msg: string, username: string): void {
export function addMessage(message: string, username: string): void {
log("add message");
}
export function getMessages(username: string | null): string {

View File

@ -13,12 +13,12 @@ export abstract class Request {
export class PostRequest extends Request {
public readonly msg: string;
public readonly message: string;
public readonly username: string;
constructor(msg: string, username: string) {
constructor(message: string, username: string) {
super();
this.msg = msg;
this.message = message;
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.username)
request = new PostRequest(jsonHandler.message, jsonHandler.username)
} else {
request = new UnknownRequest()
}
@ -71,7 +71,7 @@ export function decode(input: string): Request {
class RequestJSONEventsHandler extends JSONHandler {
public action: string;
public msg: string;
public message: string;
public username: string;
public filter_handle: string | null;
@ -79,8 +79,8 @@ class RequestJSONEventsHandler extends JSONHandler {
if (name == "action") {
this.action = value;
} else if (name == "msg") {
this.msg = value;
} else if (name == "message") {
this.message = value;
} else if (name == "username") {
this.username = value;
this.filter_handle = value;

View File

@ -8,11 +8,11 @@ export abstract class Response {
}
export class Message {
msg: string;
message: string;
username: string;
constructor(msg: string, username: string) {
this.msg = msg;
constructor(message: string, username: string) {
this.message = message;
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 username request");
encoder.setString("message", "cannot username request");
encoder.popObject();
return encoder.toString();
@ -34,17 +34,16 @@ export class UnknownResponse extends Response {
}
export class PostResponse extends Response {
msgCount: u32;
constructor(msgCount: u32) {
count: u32;
constructor(count: u32) {
super();
this.msgCount = msgCount;
this.count = count;
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Post");
encoder.setInteger("count", this.msgCount);
encoder.setInteger("count", this.count);
encoder.popObject();
return encoder.toString();
@ -62,12 +61,11 @@ export class FetchResponse extends Response {
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("message", twit.message);
encoder.setString("username", twit.username);
encoder.popObject();
}

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!", "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": "Post", "message": "Hello, Fluence!", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "message": "Hello, fluencer!", "username": "John Doe"}'));
log<string>(handler('{"action": "Post", "message": "How is it going?", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "message": "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

@ -4,7 +4,7 @@ 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)`);
query(`CREATE TABLE messages(message text, username text)`);
// Insert message 'Hello, username!' using `username` as author's username
query(`INSERT INTO messages VALUES("Hello, username!","` + username + `")`);
@ -16,7 +16,7 @@ export function handler(username: string): string {
// Get all messages as JSON via SQLite's JSON extension
return query(
`SELECT json_group_array(
json_object('msg', msg, 'username', username)
json_object('message', message, 'username', username)
) AS json_result FROM (SELECT * FROM messages)`
)
}

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!", "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": "Post", "message": "Hello, Fluence!", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "message": "Hello, fluencer!", "username": "John Doe"}'));
log<string>(handler('{"action": "Post", "message": "How is it going?", "username": "fluencer"}'));
log<string>(handler('{"action": "Post", "message": "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

@ -11,7 +11,7 @@ export function handler(input: string): string {
if (request.action == Action.Post) {
let post = request as PostRequest;
addMessage(post.msg, post.username);
addMessage(post.message, post.username);
let count = getPostsCount();
let response = new PostResponse(count);

View File

@ -1,12 +1,12 @@
import {query} from "../node_modules/db-connector/assembly/sqlite"
export function createScheme(): void {
let request = `CREATE TABLE messages(msg text, username text)`;
let request = `CREATE TABLE messages(message text, username text)`;
query(request);
}
export function addMessage(msg: string, username: string): void {
let request = `INSERT INTO messages VALUES("` + msg + `", "` + username + `")`;
export function addMessage(message: string, username: string): void {
let request = `INSERT INTO messages VALUES("` + message + `", "` + username + `")`;
query(request);
}
@ -15,14 +15,14 @@ export function getMessages(username: string | null): string {
let request =
`SELECT json_group_array(
json_object('msg', msg, 'username', username)
json_object('message', message, 'username', username)
) AS json_result FROM
(SELECT * FROM messages WHERE username = "` + username + `")`;
return query(request);
} else {
let request =
`SELECT json_group_array(
json_object('msg', msg, 'username', username)
json_object('message', message, 'username', username)
) AS json_result FROM (SELECT * FROM messages)`;
return query(request);
}

View File

@ -13,12 +13,12 @@ export abstract class Request {
export class PostRequest extends Request {
public readonly msg: string;
public readonly message: string;
public readonly username: string;
constructor(msg: string, username: string) {
constructor(message: string, username: string) {
super();
this.msg = msg;
this.message = message;
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.username)
request = new PostRequest(jsonHandler.message, jsonHandler.username)
} else {
request = new UnknownRequest()
}
@ -71,7 +71,7 @@ export function decode(input: string): Request {
class RequestJSONEventsHandler extends JSONHandler {
public action: string;
public msg: string;
public message: string;
public username: string;
public filter_handle: string | null;
@ -79,8 +79,8 @@ class RequestJSONEventsHandler extends JSONHandler {
if (name == "action") {
this.action = value;
} else if (name == "msg") {
this.msg = value;
} else if (name == "message") {
this.message = value;
} else if (name == "username") {
this.username = value;
this.filter_handle = value;

View File

@ -8,11 +8,11 @@ export abstract class Response {
}
export class Message {
msg: string;
message: string;
username: string;
constructor(msg: string, username: string) {
this.msg = msg;
constructor(message: string, username: string) {
this.message = message;
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 username request");
encoder.setString("message", "cannot username request");
encoder.popObject();
return encoder.toString();
@ -34,17 +34,16 @@ export class UnknownResponse extends Response {
}
export class PostResponse extends Response {
msgCount: u32;
constructor(msgCount: u32) {
count: u32;
constructor(count: u32) {
super();
this.msgCount = msgCount;
this.count = count;
}
serialize(): string {
let encoder = new JSONEncoder();
encoder.pushObject(null);
encoder.setString("action", "Post");
encoder.setInteger("count", this.msgCount);
encoder.setInteger("count", this.count);
encoder.popObject();
return encoder.toString();
@ -62,12 +61,11 @@ export class FetchResponse extends Response {
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("message", twit.message);
encoder.setString("username", twit.username);
encoder.popObject();
}

View File

@ -35,9 +35,9 @@ fn run(arg: String) -> String {
api::serialize(&result)
}
fn add_post(msg: String, username: String) -> AppResult<Response> {
fn add_post(message: String, username: String) -> AppResult<Response> {
// Store post
model::add_post(msg, username)?;
model::add_post(message, username)?;
// Get total number of posts
let count = model::get_posts_count()?;

View File

@ -6,8 +6,8 @@ pub fn create_scheme() -> AppResult<()> {
Ok(log::info!("creating scheme"))
}
pub fn add_post(msg: String, username: String) -> AppResult<()> {
Ok(log::info!("add post {} {}", msg, username))
pub fn add_post(message: String, username: String) -> AppResult<()> {
Ok(log::info!("add post {} {}", message, username))
}
pub fn get_all_posts() -> AppResult<String> {

View File

@ -11,7 +11,7 @@ fn init() {
#[invocation_handler(init_fn = init)]
fn run(nickname: String) -> String {
// Create table for messages storage
database::query("CREATE TABLE messages(msg text, username text)".to_string())
database::query("CREATE TABLE messages(message text, username text)".to_string())
.expect("error on CREATE TABLE");
// Insert message 'Hello, username!' using `nickname` as author's username
@ -29,7 +29,7 @@ fn run(nickname: String) -> String {
// Get all messages as JSON via SQLite's JSON extension
database::query(
"SELECT json_group_array(
json_object('msg', msg, 'username', username)
json_object('message', message, 'username', username)
) AS json_result FROM (SELECT * FROM messages)"
.to_string(),
)

View File

@ -37,9 +37,9 @@ fn run(arg: String) -> String {
api::serialize(&result)
}
fn add_post(msg: String, username: String) -> AppResult<Response> {
fn add_post(message: String, username: String) -> AppResult<Response> {
// Store post
model::add_post(msg, username)?;
model::add_post(message, username)?;
// Get total number of posts
let count = model::get_posts_count()?;

View File

@ -5,20 +5,20 @@ use crate::errors::err_msg;
use crate::errors::AppResult;
pub fn create_scheme() -> AppResult<()> {
database::query("CREATE TABLE messages(msg text, username text)".to_string())
database::query("CREATE TABLE messages(message text, username text)".to_string())
.map_err(|e| err_msg(&format!("Error creating table messages: {}", e)))
.map(drop)
}
pub fn add_post(msg: String, username: String) -> AppResult<()> {
pub fn add_post(message: String, username: String) -> AppResult<()> {
database::query(format!(
r#"INSERT INTO messages VALUES("{}","{}")"#,
msg, username
message, username
))
.map_err(|e| {
err_msg(&format!(
"Error inserting post {} by {}: {}",
msg, username, e
message, username, e
))
})
.map(drop)
@ -27,7 +27,7 @@ pub fn add_post(msg: String, username: String) -> AppResult<()> {
pub fn get_all_posts() -> AppResult<String> {
database::query(
"SELECT json_group_array(
json_object('msg', msg, 'username', username)
json_object('message', message, 'username', username)
) AS json_result FROM (SELECT * FROM messages)"
.to_string(),
)
@ -37,7 +37,7 @@ pub fn get_all_posts() -> AppResult<String> {
pub fn get_posts_by_username(username: String) -> AppResult<String> {
database::query(format!(
"SELECT json_group_array(
json_object('msg', msg, 'username', username)
json_object('message', message, 'username', username)
) AS json_result FROM (SELECT * FROM messages where username = '{}')",
username
))