feat(presentation): state, errors, extractors, auth and user handlers
This commit is contained in:
0
crates/presentation/src/handlers/api_keys.rs
Normal file
0
crates/presentation/src/handlers/api_keys.rs
Normal file
35
crates/presentation/src/handlers/auth.rs
Normal file
35
crates/presentation/src/handlers/auth.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
|
||||
use api_types::{requests::{LoginRequest, RegisterRequest}, responses::{AuthResponse, UserResponse}};
|
||||
use application::use_cases::auth::{login, register, LoginInput, RegisterInput};
|
||||
use crate::{errors::ApiError, state::AppState};
|
||||
|
||||
pub fn to_user_response(u: &domain::models::user::User) -> UserResponse {
|
||||
UserResponse {
|
||||
id: u.id.as_uuid(),
|
||||
username: u.username.to_string(),
|
||||
display_name: u.display_name.clone(),
|
||||
bio: u.bio.clone(),
|
||||
avatar_url: u.avatar_url.clone(),
|
||||
header_url: u.header_url.clone(),
|
||||
local: u.local,
|
||||
created_at: u.created_at,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn post_register(State(s): State<AppState>, Json(body): Json<RegisterRequest>) -> Result<impl IntoResponse, ApiError> {
|
||||
let out = register(&*s.users, &*s.hasher, &*s.auth, &*s.events, RegisterInput {
|
||||
username: body.username,
|
||||
email: body.email,
|
||||
password: body.password,
|
||||
}).await?;
|
||||
let resp = AuthResponse { token: out.token, user: to_user_response(&out.user) };
|
||||
Ok((StatusCode::CREATED, Json(resp)))
|
||||
}
|
||||
|
||||
pub async fn post_login(State(s): State<AppState>, Json(body): Json<LoginRequest>) -> Result<impl IntoResponse, ApiError> {
|
||||
let out = login(&*s.users, &*s.hasher, &*s.auth, LoginInput {
|
||||
email: body.email,
|
||||
password: body.password,
|
||||
}).await?;
|
||||
Ok(Json(AuthResponse { token: out.token, user: to_user_response(&out.user) }))
|
||||
}
|
||||
0
crates/presentation/src/handlers/feed.rs
Normal file
0
crates/presentation/src/handlers/feed.rs
Normal file
7
crates/presentation/src/handlers/mod.rs
Normal file
7
crates/presentation/src/handlers/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
pub mod api_keys;
|
||||
pub mod auth;
|
||||
pub mod feed;
|
||||
pub mod notifications;
|
||||
pub mod social;
|
||||
pub mod thoughts;
|
||||
pub mod users;
|
||||
0
crates/presentation/src/handlers/notifications.rs
Normal file
0
crates/presentation/src/handlers/notifications.rs
Normal file
0
crates/presentation/src/handlers/social.rs
Normal file
0
crates/presentation/src/handlers/social.rs
Normal file
0
crates/presentation/src/handlers/thoughts.rs
Normal file
0
crates/presentation/src/handlers/thoughts.rs
Normal file
15
crates/presentation/src/handlers/users.rs
Normal file
15
crates/presentation/src/handlers/users.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
use axum::{extract::{Path, State}, Json};
|
||||
use api_types::{requests::UpdateProfileRequest, responses::UserResponse};
|
||||
use application::use_cases::profile::{get_user_by_username, update_profile};
|
||||
use crate::{errors::ApiError, extractors::AuthUser, handlers::auth::to_user_response, state::AppState};
|
||||
|
||||
pub async fn get_user(State(s): State<AppState>, Path(username): Path<String>) -> Result<Json<UserResponse>, ApiError> {
|
||||
let user = get_user_by_username(&*s.users, &username).await?;
|
||||
Ok(Json(to_user_response(&user)))
|
||||
}
|
||||
|
||||
pub async fn patch_profile(State(s): State<AppState>, AuthUser(uid): AuthUser, Json(body): Json<UpdateProfileRequest>) -> Result<Json<UserResponse>, ApiError> {
|
||||
update_profile(&*s.users, &uid, body.display_name, body.bio, body.avatar_url, body.header_url, body.custom_css).await?;
|
||||
let user = s.users.find_by_id(&uid).await?.ok_or(domain::errors::DomainError::NotFound)?;
|
||||
Ok(Json(to_user_response(&user)))
|
||||
}
|
||||
Reference in New Issue
Block a user