nxtgauge-backend-rust/apps/developers/src/handlers.rs

40 lines
1.3 KiB
Rust
Raw Normal View History

use axum::{
extract::State,
http::StatusCode,
response::IntoResponse,
routing::get,
Json, Router,
};
use sqlx::PgPool;
use db::models::developer::{DeveloperRepository, UpsertDeveloperProfilePayload};
use contracts::auth_middleware::AuthUser;
pub fn router() -> Router<PgPool> {
Router::new()
.route("/profile/me", get(get_profile).patch(update_profile))
.merge(contracts::profession_shared::shared_routes("DEVELOPER"))
}
async fn get_profile(
State(pool): State<PgPool>,
auth: AuthUser,
) -> impl IntoResponse {
match DeveloperRepository::get_by_user_id(&pool, auth.user_id).await {
Ok(Some(profile)) => (StatusCode::OK, Json(profile)).into_response(),
Ok(None) => (StatusCode::NOT_FOUND, "Profile not found").into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}
async fn update_profile(
State(pool): State<PgPool>,
auth: AuthUser,
Json(payload): Json<UpsertDeveloperProfilePayload>,
) -> impl IntoResponse {
match DeveloperRepository::upsert(&pool, auth.user_id, payload).await {
Ok(profile) => (StatusCode::OK, Json(profile)).into_response(),
Err(e) => (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()).into_response(),
}
}