29 lines
801 B
TypeScript
Raw Normal View History

2019-08-16 12:27:30 +03:00
import {Action, decode, FetchRequest, PostRequest} from "./request";
import {PostResponse, UnknownResponse} from "./response";
import {addMessage, createScheme, getMessages, getPostsCount} from "./model";
2019-08-14 16:35:24 +03:00
2019-08-16 12:27:30 +03:00
createScheme();
2019-08-14 16:35:24 +03:00
// 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-16 12:27:30 +03:00
addMessage(post.msg, post.username);
let count = getPostsCount();
let response = new PostResponse(count);
2019-08-14 16:35:24 +03:00
return response.serialize()
} else if (request.action == Action.Fetch) {
2019-08-16 12:27:30 +03:00
let fetch = request as FetchRequest;
let result = getMessages(fetch.username);
return result;
2019-08-14 16:35:24 +03:00
}
let response = new UnknownResponse();
return response.serialize();
}