From fa870b6988bdbcaf37c6e193eb8870836f6e41de Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Wed, 5 Mar 2025 14:05:15 +0100 Subject: [PATCH] json models --- backend/src/models.c | 99 +++++++++++++++++++++++++++++++++++-- backend/src/models_sqlite.h | 16 ------ 2 files changed, 94 insertions(+), 21 deletions(-) delete mode 100644 backend/src/models_sqlite.h diff --git a/backend/src/models.c b/backend/src/models.c index 7563281..e467b64 100644 --- a/backend/src/models.c +++ b/backend/src/models.c @@ -162,6 +162,9 @@ static inline bool obj_conforms( return true; } +#define GET_INT(K) json_int(json_object_get(json, K)) +#define GET_STR(K) str_dup(json_string(json_object_get(json, K))) + int user_from_json(User* m, const JsonValue* json) { ObjField fields[] = { @@ -173,11 +176,97 @@ int user_from_json(User* m, const JsonValue* json) if (!obj_conforms(json, fields, sizeof(fields) / sizeof(fields[0]))) return -1; *m = (User) { - .id = json_int(json_object_get(json, "id")), - .email = str_dup(json_string(json_object_get(json, "email"))), - .password_hash - = str_dup(json_string(json_object_get(json, "password_hash"))), - .balance_dkk_cent = json_int(json_object_get(json, "balance_dkk_cent")), + .id = GET_INT("id"), + .email = GET_STR("email"), + .password_hash = GET_STR("password_hash"), + .balance_dkk_cent = GET_INT("balance_dkk_cent"), + }; + return 0; +} + +int coord_from_json(Coord* m, const JsonValue* json) +{ + ObjField fields[] = { + { "id", JsonType_Number }, + { "x", JsonType_Number }, + { "y", JsonType_Number }, + }; + if (!obj_conforms(json, fields, sizeof(fields) / sizeof(fields[0]))) + return -1; + *m = (Coord) { + .id = GET_INT("id"), + .x = GET_INT("x"), + .y = GET_INT("y"), + }; + return 0; +} + +int product_from_json(Product* m, const JsonValue* json) +{ + ObjField fields[] = { + { "id", JsonType_Number }, + { "name", JsonType_String }, + { "price_dkk_cent", JsonType_Number }, + { "coord_id", JsonType_Number }, + { "barcode", JsonType_String }, + }; + if (!obj_conforms(json, fields, sizeof(fields) / sizeof(fields[0]))) + return -1; + *m = (Product) { + .id = GET_INT("id"), + .name = GET_STR("name"), + .price_dkk_cent = GET_INT("price_dkk_cent"), + .coord_id = GET_INT("coord_id"), + .barcode = GET_STR("y"), + }; + return 0; +} + +int product_price_from_json(ProductPrice* m, const JsonValue* json) +{ + ObjField fields[] = { + { "id", JsonType_Number }, + { "product_id", JsonType_Number }, + { "price_dkk_cent", JsonType_Number }, + }; + if (!obj_conforms(json, fields, sizeof(fields) / sizeof(fields[0]))) + return -1; + *m = (ProductPrice) { + .id = GET_INT("id"), + .product_id = GET_INT("product_id"), + .price_dkk_cent = GET_INT("price_dkk_cent"), + }; + return 0; +} + +int cart_from_json(Cart* m, const JsonValue* json) +{ + ObjField fields[] = { + { "id", JsonType_Number }, + { "user_id", JsonType_Number }, + }; + if (!obj_conforms(json, fields, sizeof(fields) / sizeof(fields[0]))) + return -1; + *m = (Cart) { + .id = GET_INT("id"), + .user_id = GET_INT("user_id"), + }; + return 0; +} + +int cart_item_from_json(CartItem* m, const JsonValue* json) +{ + ObjField fields[] = { + { "id", JsonType_Number }, + { "cart_id", JsonType_Number }, + { "amount", JsonType_Number }, + }; + if (!obj_conforms(json, fields, sizeof(fields) / sizeof(fields[0]))) + return -1; + *m = (CartItem) { + .id = GET_INT("id"), + .cart_id = GET_INT("cart_id"), + .amount = GET_INT("amount"), }; return 0; } diff --git a/backend/src/models_sqlite.h b/backend/src/models_sqlite.h deleted file mode 100644 index 85489a1..0000000 --- a/backend/src/models_sqlite.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include "models.h" -#include - -int user_sqlite_from_id(User* model, sqlite3* db, int64_t id); - -int coord_sqlite_from(Coord* model, sqlite3* db); - -int product_sqlite_from(Product* model, sqlite3_stmt* stmt); - -int product_price_sqlite_from(ProductPrice* model, sqlite3_stmt* stmt); - -int cart_sqlite_from(Cart* model, sqlite3_stmt* stmt); - -int cart_item_sqlite_from(CartItem* model, sqlite3_stmt* stmt);