2019-08-14 22:15:38 +03:00
|
|
|
use crate::api::AppResult;
|
2019-08-14 13:31:26 +03:00
|
|
|
use fluence::sdk::*;
|
2019-08-14 22:15:38 +03:00
|
|
|
|
|
|
|
pub mod api;
|
|
|
|
pub mod database;
|
|
|
|
pub mod errors;
|
|
|
|
pub mod ffi;
|
|
|
|
pub mod model;
|
|
|
|
|
|
|
|
use api::Request;
|
|
|
|
use api::Response;
|
2019-08-14 13:31:26 +03:00
|
|
|
|
|
|
|
fn init() {
|
|
|
|
logger::WasmLogger::init_with_level(log::Level::Info).unwrap();
|
2019-08-14 22:15:38 +03:00
|
|
|
model::create_scheme().unwrap();
|
2019-08-14 13:31:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[invocation_handler(init_fn = init)]
|
2019-08-14 20:19:39 +03:00
|
|
|
fn run(arg: String) -> String {
|
2019-08-14 22:15:38 +03:00
|
|
|
let result = api::parse(arg).and_then(|request| match request {
|
|
|
|
Request::Post { msg, handle } => add_post(msg, handle),
|
|
|
|
Request::Fetch { handle } => fetch_posts(handle),
|
|
|
|
});
|
2019-08-14 14:02:22 +03:00
|
|
|
|
2019-08-14 22:15:38 +03:00
|
|
|
let result = match result {
|
|
|
|
Ok(good) => good,
|
|
|
|
Err(error) => Response::Error { error },
|
|
|
|
};
|
2019-08-14 13:31:26 +03:00
|
|
|
|
2019-08-14 22:15:38 +03:00
|
|
|
api::serialize(result)
|
|
|
|
}
|
2019-08-14 13:31:26 +03:00
|
|
|
|
2019-08-14 22:15:38 +03:00
|
|
|
fn add_post(msg: String, handle: String) -> AppResult<Response::Post> {
|
|
|
|
model::add_post(msg, handle)?;
|
|
|
|
let count = model::get_posts_count()?;
|
|
|
|
Response::Post { count }
|
2019-08-14 13:31:26 +03:00
|
|
|
}
|
|
|
|
|
2019-08-14 22:15:38 +03:00
|
|
|
fn fetch_posts(handle: String) -> AppResult<Response::Fetch> {
|
|
|
|
// TODO: filter posts by handle
|
|
|
|
let posts = model::get_posts()?;
|
|
|
|
Response::Fetch { posts }
|
|
|
|
}
|