From 7db372bbf48a0b97c53092659cf2ee11e80ace54 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Thu, 27 Mar 2025 15:39:45 +0100 Subject: [PATCH] backend: use sbl in carts purchase --- backend/Makefile | 9 +++++++++ backend/src/controllers/carts.c | 21 +++++++++++++++++---- backend/src/controllers/carts.sbl | 24 ++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 backend/src/controllers/carts.sbl diff --git a/backend/Makefile b/backend/Makefile index 5abda50..c1fcc1e 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -50,6 +50,8 @@ HEADERS = $(shell find src/ -name *.h) C_FILES = $(shell find src/ -name *.c) O_FILES = $(patsubst src/%.c,build/%.o,$(C_FILES)) +O_FILES += build/controllers/carts.sbl.o + CC = gcc TARGET=server @@ -63,6 +65,13 @@ build/%.o: src/%.c $(HEADERS) @mkdir -p $(dir $@) $(CC) $< -c -o $@ $(C_FLAGS) $(OPTIMIZATION) $(F_FLAGS) +build/%.sbl.o: build/%.sbl.nasm + nasm -f elf64 $< -o $@ + +build/%.sbl.nasm: src/%.sbl + @mkdir -p $(dir $@) + deno run --allow-read --allow-write ../sbc/main.ts $< $@ + clean: rm -rf build/ diff --git a/backend/src/controllers/carts.c b/backend/src/controllers/carts.c index 8991f55..5b2e134 100644 --- a/backend/src/controllers/carts.c +++ b/backend/src/controllers/carts.c @@ -3,6 +3,10 @@ #include "controllers.h" #include +extern int64_t sbc_calculate_total_price(int64_t item_amount, + const CartsItemVec* items, + const ProductPriceVec* prices); + void route_post_carts_purchase(HttpCtx* ctx) { Cx* cx = http_ctx_user_ctx(ctx); @@ -27,9 +31,7 @@ void route_post_carts_purchase(HttpCtx* ctx) size_t item_amount = req.items.size; - // accumulate product_prices and total - - int64_t total_dkk_cent = 0; + // accumulate product_prices ProductPriceVec prices; product_price_vec_construct(&prices); @@ -42,10 +44,12 @@ void route_post_carts_purchase(HttpCtx* ctx) RESPOND_SERVER_ERROR(ctx); goto l0_return; } - total_dkk_cent += price.price_dkk_cent * req.items.data[i].amount; product_price_vec_push(&prices, price); } + int64_t total_dkk_cent + = sbc_calculate_total_price((int64_t)item_amount, &req.items, &prices); + // check and update user balance User user; @@ -105,3 +109,12 @@ l0_return: user_destroy(&user); carts_purchase_req_destroy(&req); } + +int64_t sbcs_prices_get_price(const ProductPriceVec* prices, int64_t i) +{ + return prices->data[i].price_dkk_cent; +} +int64_t sbcs_items_get_amount(const CartsItemVec* items, int64_t i) +{ + return items->data[i].amount; +} diff --git a/backend/src/controllers/carts.sbl b/backend/src/controllers/carts.sbl new file mode 100644 index 0000000..7ce3b97 --- /dev/null +++ b/backend/src/controllers/carts.sbl @@ -0,0 +1,24 @@ + +#[c_export("sbc_calculate_total_price")] +fn calculate_total_price(item_amount: int, items: *(), prices: *()) -> int { + let total_dkk_cent = 0; + + let i = 0; + while i < item_amount { + let price = prices_get_price(prices, i); + let amount = items_get_amount(items, i); + + total_dkk_cent = total_dkk_cent + price * amount; + + i = i + 1; + } + + return total_dkk_cent; +} + +#[c_function("sbcs_prices_get_price")] +fn prices_get_price(items: *(), i: int) -> int {} +#[c_function("sbcs_items_get_amount")] +fn items_get_amount(items: *(), i: int) -> int {} + +// vim: syntax=slige