38 lines
1.3 KiB
Rust
38 lines
1.3 KiB
Rust
use axum::{
|
|
extract::State,
|
|
http::StatusCode,
|
|
response::IntoResponse,
|
|
routing::{get, patch},
|
|
Json, Router,
|
|
};
|
|
use sqlx::PgPool;
|
|
use db::models::catering_service::{CateringServiceRepository, UpsertCateringServiceProfilePayload};
|
|
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("CATERING_SERVICE"))
|
|
}
|
|
|
|
async fn get_profile(
|
|
State(pool): State<PgPool>,
|
|
auth: AuthUser,
|
|
) -> impl IntoResponse {
|
|
match CateringServiceRepository::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<UpsertCateringServiceProfilePayload>,
|
|
) -> impl IntoResponse {
|
|
match CateringServiceRepository::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(),
|
|
}
|
|
}
|