|
| 1 | +use axum::{ |
| 2 | + extract::{Path, Query, State}, |
| 3 | + http::StatusCode, |
| 4 | + Json, |
| 5 | +}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | +use std::str::FromStr; |
| 8 | +use std::sync::Arc; |
| 9 | + |
| 10 | +use crate::service::{Currency, ServiceContainer}; |
| 11 | + |
| 12 | +#[derive(Debug, Deserialize)] |
| 13 | +pub struct ConvertQuery { |
| 14 | + amount: i64, |
| 15 | + from: String, |
| 16 | + to: String, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Serialize)] |
| 20 | +pub struct ConvertResponse { |
| 21 | + original_amount: i64, |
| 22 | + original_currency: Currency, |
| 23 | + converted_amount: i64, |
| 24 | + converted_currency: Currency, |
| 25 | + rate: f64, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Debug, Serialize)] |
| 29 | +pub struct CurrenciesResponse { |
| 30 | + currencies: Vec<CurrencyInfo>, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, Serialize)] |
| 34 | +pub struct CurrencyInfo { |
| 35 | + code: String, |
| 36 | + name: String, |
| 37 | +} |
| 38 | + |
| 39 | +/// Convert an amount from one currency to another |
| 40 | +pub async fn convert_currency( |
| 41 | + State(services): State<Arc<ServiceContainer>>, |
| 42 | + Query(query): Query<ConvertQuery>, |
| 43 | +) -> Result<Json<ConvertResponse>, (StatusCode, String)> { |
| 44 | + let from = Currency::from_str(&query.from) |
| 45 | + .map_err(|_| (StatusCode::BAD_REQUEST, "Invalid 'from' currency".to_string()))?; |
| 46 | + let to = Currency::from_str(&query.to) |
| 47 | + .map_err(|_| (StatusCode::BAD_REQUEST, "Invalid 'to' currency".to_string()))?; |
| 48 | + |
| 49 | + let converted_amount = services |
| 50 | + .currency |
| 51 | + .convert(query.amount, from, to) |
| 52 | + .await |
| 53 | + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; |
| 54 | + |
| 55 | + let rate = services |
| 56 | + .currency |
| 57 | + .get_exchange_rate(from, to) |
| 58 | + .await |
| 59 | + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; |
| 60 | + |
| 61 | + Ok(Json(ConvertResponse { |
| 62 | + original_amount: query.amount, |
| 63 | + original_currency: from, |
| 64 | + converted_amount, |
| 65 | + converted_currency: to, |
| 66 | + rate: rate.rate, |
| 67 | + })) |
| 68 | +} |
| 69 | + |
| 70 | +/// Get exchange rate between two currencies |
| 71 | +pub async fn get_exchange_rate( |
| 72 | + State(services): State<Arc<ServiceContainer>>, |
| 73 | + Path((from, to)): Path<(String, String)>, |
| 74 | +) -> Result<Json<crate::service::ExchangeRate>, (StatusCode, String)> { |
| 75 | + let from_currency = Currency::from_str(&from) |
| 76 | + .map_err(|_| (StatusCode::BAD_REQUEST, "Invalid 'from' currency".to_string()))?; |
| 77 | + let to_currency = Currency::from_str(&to) |
| 78 | + .map_err(|_| (StatusCode::BAD_REQUEST, "Invalid 'to' currency".to_string()))?; |
| 79 | + |
| 80 | + let rate = services |
| 81 | + .currency |
| 82 | + .get_exchange_rate(from_currency, to_currency) |
| 83 | + .await |
| 84 | + .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?; |
| 85 | + |
| 86 | + Ok(Json(rate)) |
| 87 | +} |
| 88 | + |
| 89 | +/// Get list of supported currencies |
| 90 | +pub async fn get_supported_currencies( |
| 91 | + State(services): State<Arc<ServiceContainer>>, |
| 92 | +) -> Json<CurrenciesResponse> { |
| 93 | + let currencies = services.currency.get_supported_currencies(); |
| 94 | + let currency_info = currencies |
| 95 | + .into_iter() |
| 96 | + .map(|c| CurrencyInfo { |
| 97 | + code: c.as_str().to_string(), |
| 98 | + name: match c { |
| 99 | + Currency::USD => "US Dollar".to_string(), |
| 100 | + Currency::EUR => "Euro".to_string(), |
| 101 | + Currency::GBP => "British Pound".to_string(), |
| 102 | + Currency::JPY => "Japanese Yen".to_string(), |
| 103 | + }, |
| 104 | + }) |
| 105 | + .collect(); |
| 106 | + |
| 107 | + Json(CurrenciesResponse { |
| 108 | + currencies: currency_info, |
| 109 | + }) |
| 110 | +} |
0 commit comments