use crate::registry::{services, types::*}; use axum::{ extract::{Path, State}, http::StatusCode, Json, }; use sqlx::PgPool; // ===== Company Handlers ===== pub async fn create_company_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::create_company(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to create company: {}", e), }), )), } } pub async fn update_company_status_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::update_company_status(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to update company status: {}", e), }), )), } } pub async fn get_company_handler( State(pool): State, Path(pubkey): Path, ) -> Result, (StatusCode, Json)> { match services::company::get_company(&pool, &pubkey).await { Ok(company) => Ok(Json(company)), Err(sqlx::Error::RowNotFound) => Err(( StatusCode::NOT_FOUND, Json(ErrorResponse { error: "Company not found".to_string(), }), )), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Database error: {}", e), }), )), } } pub async fn get_company_by_admin_handler( State(pool): State, Path(company_admin_pubkey): Path, ) -> Result, (StatusCode, Json)> { println!("company handler received req"); match services::company::get_company_by_admin(&pool, &company_admin_pubkey).await { Ok(company) => Ok(Json(company)), Err(sqlx::Error::RowNotFound) => Err(( StatusCode::NOT_FOUND, Json(ErrorResponse { error: "Company not found".to_string(), }), )), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Database error: {}", e), }), )), } } pub async fn list_companies_handler( State(pool): State, ) -> Result>, (StatusCode, Json)> { match services::company::list_companies(&pool).await { Ok(companies) => Ok(Json(companies)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to list companies: {}", e), }), )), } } pub async fn list_companies_by_local_admin_handler( State(pool): State, Path(local_admin_pubkey): Path, ) -> Result>, (StatusCode, Json)> { match services::company::list_companies_by_local_admin(&pool, &local_admin_pubkey).await { Ok(companies) => Ok(Json(companies)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to list companies: {}", e), }), )), } } // ===== Balance Handlers ===== pub async fn update_usdc_balance_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::update_usdc_balance(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to update USDC balance: {}", e), }), )), } } pub async fn update_eua_balance_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::update_eua_balance(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to update EUA balance: {}", e), }), )), } } pub async fn update_ghg_balance_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::update_ghg_balance(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to update GHG balance: {}", e), }), )), } } // ===== Auction Handlers ===== pub async fn update_auction_status_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::update_auction_status(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to update auction status: {}", e), }), )), } } pub async fn update_bid_status_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::update_bid_status(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to update bid status: {}", e), }), )), } } pub async fn list_companies_in_auction_handler( State(pool): State, Path(auction_id): Path, ) -> Result>, (StatusCode, Json)> { match services::company::list_companies_in_auction(&pool, auction_id).await { Ok(companies) => Ok(Json(companies)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to list companies in auction: {}", e), }), )), } } // ===== Locking Handlers ===== pub async fn lock_eua_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::lock_eua(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to lock EUA: {}", e), }), )), } } pub async fn unlock_eua_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::unlock_eua(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to unlock EUA: {}", e), }), )), } } pub async fn lock_usdc_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::lock_usdc(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to lock USDC: {}", e), }), )), } } pub async fn unlock_usdc_handler( State(pool): State, Json(req): Json, ) -> Result, (StatusCode, Json)> { match services::company::unlock_usdc(&pool, req).await { Ok(company) => Ok(Json(company)), Err(e) => Err(( StatusCode::INTERNAL_SERVER_ERROR, Json(ErrorResponse { error: format!("Failed to unlock USDC: {}", e), }), )), } }