backend: use sbl in carts purchase

This commit is contained in:
SimonFJ20 2025-03-27 15:39:45 +01:00
parent da6aae12b1
commit 7db372bbf4
3 changed files with 50 additions and 4 deletions

View File

@ -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/

View File

@ -3,6 +3,10 @@
#include "controllers.h"
#include <stdio.h>
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;
}

View File

@ -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