Skip to content

Commit

Permalink
feature: add chat api handler
Browse files Browse the repository at this point in the history
  • Loading branch information
luffy2025 committed Aug 6, 2024
1 parent f82f8b3 commit 8987d5a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 25 deletions.
71 changes: 62 additions & 9 deletions chat_server/src/handlers/chat.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,70 @@
use axum::response::IntoResponse;
use crate::{
error::AppError,
models::{Chat, CreateChat},
AppState, User,
};
use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
Extension, Json,
};

pub(crate) async fn list_chats_handler() -> impl IntoResponse {
"list_chats"
pub(crate) async fn list_chats_handler(
Extension(user): Extension<User>,
State(state): State<AppState>,
) -> Result<impl IntoResponse, AppError> {
let chats = Chat::fetch_all(user.ws_id as _, &state.pool).await?;
Ok((StatusCode::OK, Json(chats)))
}

pub(crate) async fn create_chat_handler() -> impl IntoResponse {
"create_chat"
pub(crate) async fn create_chat_handler(
Extension(user): Extension<User>,
State(state): State<AppState>,
Json(input): Json<CreateChat>,
) -> Result<impl IntoResponse, AppError> {
let chat = Chat::create(input, user.ws_id as _, &state.pool).await?;
Ok((StatusCode::OK, Json(chat)))
}

pub(crate) async fn update_chat_handler() -> impl IntoResponse {
"update_chat"
pub(crate) async fn get_chat_handler(
State(state): State<AppState>,
Path(id): Path<u64>,
) -> Result<impl IntoResponse, AppError> {
let chat = Chat::get_by_id(id, &state.pool).await?;
Ok((StatusCode::OK, Json(chat)))
}

pub(crate) async fn delete_chat_handler() -> impl IntoResponse {
"delete_chat"
pub(crate) async fn update_chat_handler() -> Result<impl IntoResponse, AppError> {
Ok((StatusCode::OK, Json("")))
}

pub(crate) async fn delete_chat_handler() -> Result<impl IntoResponse, AppError> {
Ok((StatusCode::OK, Json("")))
}

#[cfg(test)]
mod tests {
use super::*;
use crate::AppConfig;
use anyhow::{Ok, Result};
use http_body_util::BodyExt;

#[tokio::test]
async fn chat_get_chat_handler_should_work() -> Result<()> {
let config = AppConfig::load()?;
let (_tdb, state) = AppState::new_for_test(config).await?;

let id: u64 = 1;
let ret = get_chat_handler(State(state), Path(id))
.await
.into_response();
assert_eq!(ret.status(), StatusCode::OK);
let body = ret.into_body().collect().await?.to_bytes();
let ret = serde_json::from_slice::<Chat>(&body)?;
assert_eq!(ret.id as u64, id);
assert_eq!(ret.members.len(), 5);

Ok(())
}
}
11 changes: 6 additions & 5 deletions chat_server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod utils;
use anyhow::Context;
use axum::{
middleware::from_fn_with_state,
routing::{get, patch, post},
routing::{get, post},
Router,
};
use core::fmt;
Expand Down Expand Up @@ -40,14 +40,15 @@ pub async fn get_router(config: AppConfig) -> Result<Router, AppError> {

let api = Router::new()
.route("/users", get(list_chat_users_handler))
.route("/chat", get(list_chats_handler).post(create_chat_handler))
.route("/chats", get(list_chats_handler).post(create_chat_handler))
.route(
"/chat/:id",
patch(update_chat_handler)
"/chats/:id",
get(get_chat_handler)
.patch(update_chat_handler)
.delete(delete_chat_handler)
.post(send_message_handler),
)
.route("/chat/:id/messages", get(list_message_handler))
.route("/chats/:id/messages", get(list_message_handler))
.layer(from_fn_with_state(state.clone(), verify_token))
// routes doesn't require auth
.route("/signin", post(signin_handler))
Expand Down
1 change: 1 addition & 0 deletions chat_server/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod chat;
mod user;
mod workspace;

pub use chat::CreateChat;
pub use user::{CreateUser, SigninUser};

use chrono::{DateTime, Utc};
Expand Down
35 changes: 24 additions & 11 deletions chat_server/test.rest
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ Content-Type: application/json
"password": "hunter42"
}

### signin user (invalid)
POST http://localhost:6688/api/signin
Content-Type: application/json

### signin user (valid)
{
"email": "[email protected]",
"password": "invalidpwd"
}

### signin user (valid)
# @name signin
POST http://localhost:6688/api/signin
Content-Type: application/json
Expand All @@ -34,19 +41,25 @@ Content-Type: application/json

@token = {{signin.response.body.token}}

### signin user (invalid)
POST http://localhost:6688/api/signin
### get user list
GET http://localhost:6688/api/users
Authorization: Bearer {{token}}

### get chat list
GET http://localhost:6688/api/chats
Authorization: Bearer {{token}}

### create chat
POST http://localhost:6688/api/chats
Authorization: Bearer {{token}}
Content-Type: application/json

{
"email": "[email protected]",
"password": "invalidpwd"
"name": "learning rust",
"members": [1, 2],
"public": false
}

### chat api
GET http://localhost:6688/api/chat
Authorization: Bearer {{token}}

### get user list
GET http://localhost:6688/api/users
### get chat by id
GET http://localhost:6688/api/chats/1
Authorization: Bearer {{token}}

0 comments on commit 8987d5a

Please sign in to comment.