2019-08-14 19:00:22 +03:00
|
|
|
import {Action, decode, FetchRequest, PostRequest} from "./request";
|
2019-08-17 20:15:34 +03:00
|
|
|
import {decodePosts, PostResponse, UnknownResponse} from "./response";
|
2019-08-16 12:27:30 +03:00
|
|
|
import {addMessage, createScheme, getMessages, getPostsCount} from "./model";
|
2019-08-14 19:00:22 +03:00
|
|
|
|
2019-08-16 12:27:30 +03:00
|
|
|
createScheme();
|
2019-08-14 19:00:22 +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 19:47:38 +03:00
|
|
|
addMessage(post.message, 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-18 16:05:11 +03:00
|
|
|
let result = getMessages(fetch.username, fetch.offset, fetch.count);
|
2019-08-17 20:15:34 +03:00
|
|
|
|
|
|
|
let response = decodePosts(result);
|
|
|
|
return response.serialize();
|
2019-08-14 19:00:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let response = new UnknownResponse();
|
|
|
|
return response.serialize();
|
|
|
|
}
|