-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use super::server::AppState; | ||
use crate::api::types::{InventoryResponse, OrderRequest}; | ||
use axum::{debug_handler, extract::State, http::StatusCode, Json}; | ||
use std::sync::Arc; | ||
|
||
pub async fn root() -> &'static str { | ||
"Hello, World!" | ||
} | ||
|
||
pub async fn get_orders() -> &'static str { | ||
"Hello, World!" | ||
} | ||
|
||
#[debug_handler] | ||
pub async fn create_order( | ||
State(state): State<Arc<AppState>>, | ||
Json(payload): Json<OrderRequest>, | ||
) -> Result<(), (StatusCode, String)> { | ||
let query: Vec<(String, String)> = payload | ||
.items | ||
.iter() | ||
.map(|i| ("sku".to_string(), i.sku.clone())) | ||
.collect(); | ||
// call inventory service; | ||
// takes a request of a list of order line items, checks they are all in stock (http call to the inventory service) and if so, creates an order entry in the database | ||
let client = reqwest::Client::new(); | ||
let all_in_stock = client | ||
// TODO: update this url | ||
.get("http://inventory-service/api/inventory") | ||
.query(&query) | ||
.send() | ||
.await | ||
.map_err(internal_error)? | ||
.json::<Vec<InventoryResponse>>() | ||
.await | ||
.map_err(internal_error)? | ||
.iter() | ||
.all(|i| i.is_in_stock); | ||
|
||
if all_in_stock { | ||
// TODO: Update SQL query | ||
let row: (i64,) = sqlx::query_as("INSERT into sometable values ($1)") | ||
.bind(150_i64) | ||
.fetch_one(&state.pool) | ||
.await | ||
.map_err(internal_error)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
/// Utility function for mapping any error into a `500 Internal Server Error` | ||
/// response. | ||
fn internal_error<E>(err: E) -> (StatusCode, String) | ||
where | ||
E: std::error::Error, | ||
{ | ||
(StatusCode::INTERNAL_SERVER_ERROR, err.to_string()) | ||
} | ||
|
||
pub async fn health() -> &'static str { | ||
"ok" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
use axum::{routing::get, Router}; | ||
|
||
use crate::api::handlers::{create_order, get_orders, health, root}; | ||
use crate::config::Config; | ||
use axum::{routing::get, routing::post, Router}; | ||
use sqlx::{Pool, Postgres}; | ||
use std::sync::Arc; | ||
|
||
pub struct AppState { | ||
pub pool: Pool<Postgres>, | ||
} | ||
|
||
pub async fn create(config: Config, pool: Pool<Postgres>) -> anyhow::Result<()> { | ||
let state = Arc::new(AppState { pool }); | ||
|
||
pub async fn create(config: Config) { | ||
// build our application with a route | ||
let app = Router::new().route("/", get(root)); | ||
let app = Router::new() | ||
.route("/", get(root)) | ||
.route("/health", get(health)) | ||
.route("/api/order", get(get_orders)) | ||
.route("/api/order", post(create_order)) | ||
.with_state(state); | ||
|
||
// run it | ||
//todo pass a port | ||
let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{}", config.port)) | ||
.await | ||
.unwrap(); | ||
println!("listening on {}", listener.local_addr().unwrap()); | ||
axum::serve(listener, app).await.unwrap(); | ||
} | ||
let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{}", config.port)).await?; | ||
println!("listening on {}", listener.local_addr()?); | ||
axum::serve(listener, app).await?; | ||
|
||
// basic handler that responds with a static string | ||
async fn root() -> &'static str { | ||
"Hello, World!" | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters