-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
93 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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}} |