2019-08-14 19:00:22 +03:00
|
|
|
import {Action, decode, FetchRequest, PostRequest} from "./request";
|
|
|
|
import {PostResponse, Message, FetchResponse, UnknownResponse} from "./response";
|
2019-08-15 12:50:41 +03:00
|
|
|
import {addMessage, getMessages, getPostsCount, initTables} from "./database";
|
2019-08-14 19:00:22 +03:00
|
|
|
|
|
|
|
initTables();
|
|
|
|
|
|
|
|
// 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;
|
2019-08-15 19:10:13 +03:00
|
|
|
addMessage(post.msg, post.username);
|
2019-08-15 12:50:41 +03:00
|
|
|
let count = getPostsCount();
|
|
|
|
|
|
|
|
let response = new PostResponse(count);
|
2019-08-14 19:00:22 +03:00
|
|
|
return response.serialize()
|
|
|
|
} else if (request.action == Action.Fetch) {
|
|
|
|
let fetch = request as FetchRequest;
|
|
|
|
|
2019-08-15 19:10:13 +03:00
|
|
|
let result = getMessages(fetch.username);
|
2019-08-14 19:00:22 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = new UnknownResponse();
|
|
|
|
return response.serialize();
|
|
|
|
}
|