Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Aug 14, 2024
1 parent 2aa5f8a commit 502ab8e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 14 deletions.
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion platform-wasmcloud/wadm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ spec:
- name: sqldb-postgres
type: capability
properties:
image: ghcr.io/wasmcloud/sqldb-postgres:0.3.0
image: ghcr.io/wasmcloud/sqldb-postgres:0.2.0
# this one also works ...
# image: ghcr.io/wasmcloud/sqldb-postgres:877830d
# but 0.3.0 is broken. Have pinged Victor Adossi at Cosmonic in slack
id: sqldb-postgres
config:
- name: default-pg
Expand Down
7 changes: 7 additions & 0 deletions wasm-components/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wasm-components/rust/orders-service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
common = { path = "../common" }
indoc = "2.0.5"
serde.workspace = true
serde_json.workspace = true
uuid = { version = "1.8.0", features = ["v4"] }
Expand Down
36 changes: 24 additions & 12 deletions wasm-components/rust/orders-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ wit_bindgen::generate!({
generate_all,
});

use indoc::indoc;
use std::collections::HashMap;

use uuid::Uuid;
Expand All @@ -35,7 +36,11 @@ struct Component;
impl Guest for Component {
#[doc = r" Creates an `order` for specified line items"]
fn create_order(items: Vec<LineItem>) -> Result<(), Error> {
log(Level::Info, "orders-service", "Order request received...");
log(
Level::Info,
"orders-service",
&format!("Order request received: {:?}", items),
);

let skus: Vec<String> = items.iter().map(|item| item.sku.clone()).collect();

Expand All @@ -61,9 +66,11 @@ impl Guest for Component {
];

let id = query(
"-- Create line item
INSERT INTO orders.t_order_line_items (price, quantity, sku)
VALUES ($1, $2, $3) RETURNING id;",
indoc! {"
-- Create order line item
INSERT INTO orders.t_order_line_items (price, quantity, sku)
VALUES ($1, $2, $3) RETURNING id;
"},
&params,
)
.expect("ORDER-SERVICE-CREATE-ORDER: Failed to insert order line item");
Expand All @@ -80,9 +87,11 @@ impl Guest for Component {
let order_number = Uuid::new_v4().to_string();

let pg_response = query(
"-- Create order entry
INSERT INTO orders.t_orders (order_number, total)
VALUES ($1, $2) RETURNING id",
indoc! {"
-- Create order entry
INSERT INTO orders.t_orders (order_number, total)
VALUES ($1, $2) RETURNING id;
"},
&[
PgValue::Text(order_number.clone()),
PgValue::Integer(*total),
Expand All @@ -100,9 +109,11 @@ impl Guest for Component {

for id in ids {
query(
"-- Link order and line items
INSERT INTO orders.t_orders_order_line_items_list (order_id, order_line_items_list_id)
VALUES ($1, $2);",
indoc! {"
-- Link order and line items
INSERT INTO orders.t_orders_order_line_items_list (order_id, order_line_items_list_id)
VALUES ($1, $2);
"},
&[PgValue::BigInt(order_id.parse().unwrap()), PgValue::BigInt(id.parse().unwrap())],
).expect("ORDER-SERVICE-CREATE-ORDER: Failed to link order and line items");
}
Expand Down Expand Up @@ -145,7 +156,7 @@ impl Guest for Component {

#[doc = r" Lists all orders"]
fn get_orders() -> Result<Vec<Order>, Error> {
let get_orders_query = r#"
let get_orders_query = indoc! {r#"
SELECT
"order".order_number,
"order".total as order_total,
Expand All @@ -155,7 +166,8 @@ impl Guest for Component {
FROM
orders.t_order_line_items as line_items
JOIN orders.t_orders_order_line_items_list as order_lines ON "order_lines".order_line_items_list_id = "line_items".id
JOIN orders.t_orders as "order" ON "order".id = "order_lines".order_id;"#;
JOIN orders.t_orders as "order" ON "order".id = "order_lines".order_id;
"#};

let rows =
query(get_orders_query, &[]).expect("ORDER-SERVICE-GET-ORDERS: Failed to get orders");
Expand Down
2 changes: 1 addition & 1 deletion wasm-components/wit/deps/wasmcloud/postgres/query.wit
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ interface prepared {
stmt-token: prepared-statement-token,
params: list<pg-value>,
) -> result<u64, prepared-statement-exec-error>;
}
}

0 comments on commit 502ab8e

Please sign in to comment.