Skip to content

Commit

Permalink
fix: limit should have default and all data should use camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrchen committed Aug 18, 2024
1 parent bb202b0 commit c83f829
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion chat_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct User {
}

#[derive(Debug, Clone, FromRow, ToSchema, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Workspace {
pub id: i64,
pub name: String,
Expand All @@ -33,6 +34,7 @@ pub struct Workspace {
}

#[derive(Debug, Clone, FromRow, ToSchema, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ChatUser {
pub id: i64,
pub fullname: String,
Expand All @@ -41,7 +43,7 @@ pub struct ChatUser {

#[derive(Debug, Clone, ToSchema, Serialize, Deserialize, PartialEq, PartialOrd, sqlx::Type)]
#[sqlx(type_name = "chat_type", rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
#[serde(rename_all = "camelCase")]
pub enum ChatType {
Single,
Group,
Expand All @@ -50,6 +52,7 @@ pub enum ChatType {
}

#[derive(Debug, Clone, FromRow, ToSchema, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Chat {
pub id: i64,
pub ws_id: i64,
Expand All @@ -60,6 +63,7 @@ pub struct Chat {
}

#[derive(Debug, Clone, FromRow, ToSchema, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Message {
pub id: i64,
pub chat_id: i64,
Expand Down
9 changes: 8 additions & 1 deletion chat_server/src/models/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub struct CreateMessage {

#[derive(Debug, Clone, IntoParams, ToSchema, Serialize, Deserialize)]
pub struct ListMessages {
#[serde(default)]
pub last_id: Option<u64>,
#[serde(default)]
pub limit: u64,
}

Expand Down Expand Up @@ -67,6 +69,11 @@ impl AppState {
chat_id: u64,
) -> Result<Vec<Message>, AppError> {
let last_id = input.last_id.unwrap_or(i64::MAX as _);
let limit = match input.limit {
0 => i64::MAX,
1..=100 => input.limit as _,
_ => 100,
};

let messages: Vec<Message> = sqlx::query_as(
r#"
Expand All @@ -80,7 +87,7 @@ impl AppState {
)
.bind(chat_id as i64)
.bind(last_id as i64)
.bind(input.limit as i64)
.bind(limit)
.fetch_all(&self.pool)
.await?;

Expand Down

0 comments on commit c83f829

Please sign in to comment.