server: Allow to search an user by name
This commit is contained in:
parent
68fa303a38
commit
b9666726d4
2 changed files with 30 additions and 2 deletions
|
|
@ -42,3 +42,9 @@ pub struct CreateHouseholdResponse {
|
|||
pub struct AddToHouseholdRequest {
|
||||
pub user: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct UserInfo {
|
||||
pub name: String,
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use api::{LoginRequest, LoginResponse};
|
||||
use api::{LoginRequest, LoginResponse, UserInfo};
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRef, FromRequestParts, State},
|
||||
extract::{FromRef, FromRequestParts, Path, State},
|
||||
headers::{authorization::Bearer, Authorization},
|
||||
http::{
|
||||
header::{AUTHORIZATION, CONTENT_TYPE},
|
||||
|
|
@ -138,6 +138,24 @@ async fn login(
|
|||
Ok(Json(LoginResponse { token }))
|
||||
}
|
||||
|
||||
async fn get_user_id(
|
||||
_: AuthenticatedUser,
|
||||
State(state): State<AppState>,
|
||||
Path(name): Path<String>,
|
||||
) -> Result<Result<Json<UserInfo>, StatusCode>, RouteError> {
|
||||
let Some(user) = User::find()
|
||||
.filter(user::Column::Name.eq(name))
|
||||
.one(&state.db)
|
||||
.await? else {
|
||||
return Ok(Err(StatusCode::NOT_FOUND))
|
||||
};
|
||||
|
||||
Ok(Ok(Json(UserInfo {
|
||||
name: user.name,
|
||||
id: user.id,
|
||||
})))
|
||||
}
|
||||
|
||||
pub(crate) fn router(api_allowed: Option<HeaderValue>) -> Router<AppState> {
|
||||
let origin: AllowOrigin = match api_allowed {
|
||||
Some(n) => n.into(),
|
||||
|
|
@ -151,6 +169,10 @@ pub(crate) fn router(api_allowed: Option<HeaderValue>) -> Router<AppState> {
|
|||
let mk_service = |m: Vec<Method>| cors_base.clone().allow_methods(m);
|
||||
|
||||
Router::new()
|
||||
.route(
|
||||
"/search/user/:name",
|
||||
get(get_user_id).layer(mk_service(vec![Method::GET])),
|
||||
)
|
||||
.route("/login", post(login).layer(mk_service(vec![Method::POST])))
|
||||
.route(
|
||||
"/household",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue