mirror of
https://github.com/fluencelabs/fluid
synced 2025-04-24 14:22:18 +00:00
Implement limit for Rust
This commit is contained in:
parent
959143a5d7
commit
ae9b018f04
@ -83,7 +83,7 @@ class RequestJSONEventsHandler extends JSONHandler {
|
||||
|
||||
setString(name: string, value: string): void {
|
||||
this.offset = 0;
|
||||
this.count = 0;
|
||||
this.count = 100;
|
||||
|
||||
if (name == "action") {
|
||||
this.action = value;
|
||||
|
@ -83,7 +83,7 @@ class RequestJSONEventsHandler extends JSONHandler {
|
||||
|
||||
setString(name: string, value: string): void {
|
||||
this.offset = 0;
|
||||
this.count = 0;
|
||||
this.count = 100;
|
||||
|
||||
if (name == "action") {
|
||||
this.action = value;
|
||||
|
@ -65,7 +65,7 @@ echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE"
|
||||
|
||||
# Assign json to a variable using heredoc technique
|
||||
JSON=$(cat <<JSON
|
||||
{"action":"Fetch", "handle": "random_joe", "offset": 0, "limit": 10}
|
||||
{"action":"Fetch", "handle": "random_joe", "offset": 0, "count": 10}
|
||||
JSON
|
||||
)
|
||||
|
||||
|
@ -5,8 +5,15 @@ use serde_json::value::RawValue;
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "action")]
|
||||
pub enum Request {
|
||||
Post { message: String, username: String },
|
||||
Fetch { username: Option<String> },
|
||||
Post {
|
||||
message: String,
|
||||
username: String,
|
||||
},
|
||||
Fetch {
|
||||
username: Option<String>,
|
||||
offset: Option<u32>,
|
||||
count: Option<u32>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
|
@ -21,7 +21,11 @@ fn run(arg: String) -> String {
|
||||
// Parse and username JSON request
|
||||
let result = api::parse(arg).and_then(|request| match request {
|
||||
Request::Post { message, username } => add_post(message, username),
|
||||
Request::Fetch { username } => fetch_posts(username),
|
||||
Request::Fetch {
|
||||
username,
|
||||
offset,
|
||||
count,
|
||||
} => fetch_posts(username, offset, count),
|
||||
});
|
||||
|
||||
let result = match result {
|
||||
@ -44,12 +48,18 @@ fn add_post(message: String, username: String) -> AppResult<Response> {
|
||||
Ok(Response::Post { count })
|
||||
}
|
||||
|
||||
fn fetch_posts(username: Option<String>) -> AppResult<Response> {
|
||||
fn fetch_posts(
|
||||
username: Option<String>,
|
||||
offset: Option<u32>,
|
||||
count: Option<u32>,
|
||||
) -> AppResult<Response> {
|
||||
let count = count.unwrap_or(0);
|
||||
let offset = offset.unwrap_or(0);
|
||||
let posts_str = match username {
|
||||
// Get all posts if no filter username was passed
|
||||
None => model::get_all_posts()?,
|
||||
None => model::get_all_posts(count, offset)?,
|
||||
// Or get only posts from specified author
|
||||
Some(h) => model::get_posts_by_username(h)?,
|
||||
Some(h) => model::get_posts_by_username(h, count, offset)?,
|
||||
};
|
||||
|
||||
// Some Rust-specific detail to play nice with serialization, doesn't matter
|
||||
|
@ -10,12 +10,12 @@ pub fn add_post(message: String, username: String) -> AppResult<()> {
|
||||
Ok(log::info!("add post {} {}", message, username))
|
||||
}
|
||||
|
||||
pub fn get_all_posts() -> AppResult<String> {
|
||||
pub fn get_all_posts(offset: u32, count: u32) -> AppResult<String> {
|
||||
log::info!("get all posts");
|
||||
Ok("[]".to_string())
|
||||
}
|
||||
|
||||
pub fn get_posts_by_username(username: String) -> AppResult<String> {
|
||||
pub fn get_posts_by_username(username: String, offset: u32, count: u32) -> AppResult<String> {
|
||||
log::info!("get all posts by username {}", username);
|
||||
Ok("[]".to_string())
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ echo "$RESPONSE" | jq . 2>/dev/null || echo "$RESPONSE"
|
||||
|
||||
# Assign json to a variable using heredoc technique
|
||||
JSON=$(cat <<JSON
|
||||
{"action":"Fetch", "handle": "random_joe"}
|
||||
{"action":"Fetch", "handle": "random_joe", "offset": 0, "count": 10}
|
||||
JSON
|
||||
)
|
||||
|
||||
|
@ -5,8 +5,15 @@ use serde_json::value::RawValue;
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "action")]
|
||||
pub enum Request {
|
||||
Post { message: String, username: String },
|
||||
Fetch { username: Option<String> },
|
||||
Post {
|
||||
message: String,
|
||||
username: String,
|
||||
},
|
||||
Fetch {
|
||||
username: Option<String>,
|
||||
offset: Option<u32>,
|
||||
count: Option<u32>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
|
@ -22,7 +22,11 @@ fn run(arg: String) -> String {
|
||||
// Parse and username JSON request
|
||||
let result = api::parse(arg).and_then(|request| match request {
|
||||
Request::Post { message, username } => add_post(message, username),
|
||||
Request::Fetch { username } => fetch_posts(username),
|
||||
Request::Fetch {
|
||||
username,
|
||||
offset,
|
||||
count,
|
||||
} => fetch_posts(username, offset, count),
|
||||
});
|
||||
|
||||
let result = match result {
|
||||
@ -45,12 +49,18 @@ fn add_post(message: String, username: String) -> AppResult<Response> {
|
||||
Ok(Response::Post { count })
|
||||
}
|
||||
|
||||
fn fetch_posts(username: Option<String>) -> AppResult<Response> {
|
||||
fn fetch_posts(
|
||||
username: Option<String>,
|
||||
offset: Option<u32>,
|
||||
count: Option<u32>,
|
||||
) -> AppResult<Response> {
|
||||
let count = count.unwrap_or(100);
|
||||
let offset = offset.unwrap_or(0);
|
||||
let posts_str = match username {
|
||||
// Get all posts if no filter username was passed
|
||||
None => model::get_all_posts()?,
|
||||
None => model::get_all_posts(offset, count)?,
|
||||
// Or get only posts from specified author
|
||||
Some(h) => model::get_posts_by_username(h)?,
|
||||
Some(h) => model::get_posts_by_username(h, offset, count)?,
|
||||
};
|
||||
|
||||
// Some Rust-specific detail to play nice with serialization, doesn't matter
|
||||
|
@ -19,21 +19,29 @@ pub fn add_post(message: String, username: String) -> AppResult<()> {
|
||||
.map(drop)
|
||||
}
|
||||
|
||||
pub fn get_all_posts() -> AppResult<String> {
|
||||
database::query(
|
||||
"SELECT json_group_array(
|
||||
json_object('message', message, 'username', username)
|
||||
) AS json_result FROM (SELECT * FROM messages)",
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_posts_by_username(username: String) -> AppResult<String> {
|
||||
pub fn get_all_posts(offset: u32, count: u32) -> AppResult<String> {
|
||||
database::query(
|
||||
format!(
|
||||
"SELECT json_group_array(
|
||||
json_object('message', message, 'username', username)
|
||||
) AS json_result FROM (SELECT * FROM messages where username = '{}')",
|
||||
username
|
||||
) AS json_result FROM (
|
||||
SELECT * FROM messages LIMIT {} OFFSET {}
|
||||
)",
|
||||
count, offset,
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_posts_by_username(username: String, offset: u32, count: u32) -> AppResult<String> {
|
||||
database::query(
|
||||
format!(
|
||||
"SELECT json_group_array(
|
||||
json_object('message', message, 'username', username)
|
||||
) AS json_result FROM (
|
||||
SELECT * FROM messages where username = '{}' LIMIT {} OFFSET {}
|
||||
)",
|
||||
username, count, offset
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user