Skip to content

Commit

Permalink
Draft handller for POST /order
Browse files Browse the repository at this point in the history
  • Loading branch information
pataruco committed Dec 19, 2023
1 parent 87a6227 commit 7a370b5
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 15 deletions.
63 changes: 63 additions & 0 deletions rust-containers-k8s/order-service/src/api/handlers.rs
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"
}
34 changes: 21 additions & 13 deletions rust-containers-k8s/order-service/src/api/server.rs
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(())
}
10 changes: 8 additions & 2 deletions rust-containers-k8s/order-service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dotenv::dotenv;
use order_service::{api::server, config::Config};
use sqlx::postgres::PgPoolOptions;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand All @@ -8,7 +9,12 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

println!("{:?}", config);

server::create(config).await;
let pool = PgPoolOptions::new()
.max_connections(5)
// TODO: paramterise this
.connect("postgres://commerce:commerce@localhost/order-service")
.await?;

server::create(config, pool).await?;
Ok(())
}

0 comments on commit 7a370b5

Please sign in to comment.