From c696a3b7801c51e94d47689366d9cfe9e005f7d6 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Tue, 12 May 2026 12:02:30 +0200 Subject: [PATCH] feat: add endpoint to retrieve user by username --- crates/presentation/src/handlers/html.rs | 17 +++++++++++++++++ crates/presentation/src/routes.rs | 1 + 2 files changed, 18 insertions(+) diff --git a/crates/presentation/src/handlers/html.rs b/crates/presentation/src/handlers/html.rs index f71192f..2848f28 100644 --- a/crates/presentation/src/handlers/html.rs +++ b/crates/presentation/src/handlers/html.rs @@ -497,6 +497,23 @@ pub async fn get_users_list( } } +pub async fn get_user_by_username( + State(state): State, + Path(username): Path, +) -> impl IntoResponse { + let uname = match domain::value_objects::Username::new(username) { + Ok(u) => u, + Err(_) => return StatusCode::NOT_FOUND.into_response(), + }; + match state.app_ctx.user_repository.find_by_username(&uname).await { + Ok(Some(user)) => { + axum::response::Redirect::permanent(&format!("/users/{}", user.id().value())) + .into_response() + } + _ => StatusCode::NOT_FOUND.into_response(), + } +} + pub async fn get_user_profile( OptionalCookieUser(user_id): OptionalCookieUser, State(state): State, diff --git a/crates/presentation/src/routes.rs b/crates/presentation/src/routes.rs index f983ab4..e08197f 100644 --- a/crates/presentation/src/routes.rs +++ b/crates/presentation/src/routes.rs @@ -60,6 +60,7 @@ fn html_routes(rate_limit: u64) -> Router { let base = Router::new() .route("/", routing::get(handlers::html::get_activity_feed)) .route("/users", routing::get(handlers::html::get_users_list)) + .route("/u/{username}", routing::get(handlers::html::get_user_by_username)) .route( "/users/{id}", routing::get(handlers::html::get_user_profile),