diff --git a/backend/prepare.sql b/backend/prepare.sql index 12821e0..2f9fce4 100644 --- a/backend/prepare.sql +++ b/backend/prepare.sql @@ -32,21 +32,14 @@ CREATE TABLE IF NOT EXISTS product_prices ( FOREIGN KEY(product) REFERENCES products(id) ); -CREATE TABLE IF NOT EXISTS carts ( - id INTEGER PRIMARY KEY, - user INTEGER NOT NULL, - - FOREIGN KEY(user) REFERENCES users(id) -); - CREATE TABLE IF NOT EXISTS cart_items ( id INTEGER PRIMARY KEY, - cart INTEGER NOT NULL, + user INTEGER NOT NULL, product INTEGER NOT NULL, amount INTEGER NOT NULL, - FOREIGN KEY(cart) REFERENCES carts(id), + FOREIGN KEY(user) REFERENCES users(id), FOREIGN KEY(product) REFERENCES product(id) ); diff --git a/backend/src/controllers/controllers.h b/backend/src/controllers/controllers.h index 652301e..0716e55 100644 --- a/backend/src/controllers/controllers.h +++ b/backend/src/controllers/controllers.h @@ -68,7 +68,7 @@ const Session* middleware_session(HttpCtx* ctx); #define RESPOND_SERVER_ERROR(HTTP_CTX) \ RESPOND_JSON(HTTP_CTX, 500, "{\"ok\":false,\"msg\":\"server error\"}") -__attribute__((unused)) static inline void ___include_user(void) +__attribute__((unused)) static inline void ___controllers_include_user(void) { RESPOND((HttpCtx*)0, 200, "text/html", "") } diff --git a/backend/src/db/db.h b/backend/src/db/db.h index 2cc15cb..cadd373 100644 --- a/backend/src/db/db.h +++ b/backend/src/db/db.h @@ -30,4 +30,5 @@ DbRes db_user_with_email(Db* db, User* user, const char* email); /// Expects `vec` to be constructed. DbRes db_product_all(Db* db, ProductVec* vec); -DbRes db_cart_items_with_user_id(Db* db, CartItemVec* vec, int64_t user_id); +__attribute__((deprecated("store in memory instead"))) DbRes +db_cart_items_with_user_id(Db* db, CartItemVec* vec, int64_t user_id); diff --git a/backend/src/db/db_sqlite.c b/backend/src/db/db_sqlite.c index 87f4ff1..41383c0 100644 --- a/backend/src/db/db_sqlite.c +++ b/backend/src/db/db_sqlite.c @@ -282,7 +282,6 @@ l0_return: DbRes db_cart_items_with_user_id(Db* db, CartItemVec* vec, int64_t user_id) { - static_assert(sizeof(Cart) == 16, "model has changed"); static_assert(sizeof(CartItem) == 32, "model has changed"); sqlite3* connection; @@ -290,45 +289,19 @@ DbRes db_cart_items_with_user_id(Db* db, CartItemVec* vec, int64_t user_id) DbRes res; sqlite3_stmt* stmt; - int sqlite_res = sqlite3_prepare_v2(connection, - "SELECT id " - " FROM carts WHERE user = ?", - -1, - &stmt, - NULL); - if (sqlite_res != SQLITE_OK) { - fprintf(stderr, - "error: %s\n at %s:%d\n", - sqlite3_errmsg(connection), - __func__, - __LINE__); - res = DbRes_Error; - goto l0_return; - } - sqlite3_bind_int64(stmt, 1, user_id); - - int step_res = sqlite3_step(stmt); - if (step_res == SQLITE_DONE) { - res = DbRes_NotFound; - goto l0_return; - } else if (step_res != SQLITE_ROW) { - fprintf(stderr, "error: %s\n", sqlite3_errmsg(connection)); - res = DbRes_Error; - goto l0_return; - } - int64_t cart_id = GET_INT(0); + int sqlite_res; sqlite_res = sqlite3_prepare_v2(connection, - "SELECT id, cart, product, amount FROM cart_items WHERE cart = ?", + "SELECT id, user, product, amount FROM cart_items WHERE user = ?", -1, &stmt, NULL); - sqlite3_bind_int64(stmt, 1, cart_id); + sqlite3_bind_int64(stmt, 1, user_id); while ((sqlite_res = sqlite3_step(stmt)) == SQLITE_ROW) { CartItem cart_item = { .id = GET_INT(0), - .cart_id = GET_INT(1), + .user_id = GET_INT(1), .product_id = GET_INT(2), .amount = GET_INT(3), }; diff --git a/backend/src/main.c b/backend/src/main.c index 7c5619a..7a7771a 100644 --- a/backend/src/main.c +++ b/backend/src/main.c @@ -1,3 +1,4 @@ +#include "collections/kv_map.h" #include "controllers/controllers.h" #include "db/db_sqlite.h" #include "http/http.h" @@ -61,9 +62,13 @@ int main(void) db_sqlite_free(db); } +#ifdef RUN_TESTS void test(void) { - str_util_test(); - printf("ALL TESTS PASSED 💅\n"); + test_util_str(); + test_collections_kv_map(); + printf("\n\x1b[1;97m ALL TESTS \x1b[1;92mPASSED" + " \x1b[1;97mSUCCESSFULLY 💅\x1b[0m\n\n"); exit(0); } +#endif diff --git a/backend/src/models/models.c b/backend/src/models/models.c index 43e1c53..93b4c8a 100644 --- a/backend/src/models/models.c +++ b/backend/src/models/models.c @@ -38,13 +38,6 @@ void product_price_destroy(ProductPrice* m) (void)m; } -void cart_destroy(Cart* m) -{ - static_assert(sizeof(Cart) == 16, "model has changed"); - - (void)m; -} - void cart_item_destroy(CartItem* m) { static_assert(sizeof(CartItem) == 32, "model has changed"); @@ -163,25 +156,6 @@ char* product_price_to_json_string(const ProductPrice* m) return result; } -char* cart_to_json_string(const Cart* m) -{ - static_assert(sizeof(Cart) == 16, "model has changed"); - - String string; - string_construct(&string); - string_pushf(&string, - "{" - "\"id\":%ld," - "\"user_id\":%ld" - "}", - m->id, - m->user_id); - - char* result = string_copy(&string); - string_destroy(&string); - return result; -} - char* cart_item_to_json_string(const CartItem* m) { static_assert(sizeof(CartItem) == 32, "model has changed"); @@ -191,12 +165,12 @@ char* cart_item_to_json_string(const CartItem* m) string_pushf(&string, "{" "\"id\":%ld," - "\"cart_id\":%ld," + "\"user_id\":%ld," "\"product_id\":%ld," "\"amount\":%ld" "}", m->id, - m->cart_id, + m->user_id, m->product_id, m->amount); @@ -356,30 +330,13 @@ int product_price_from_json(ProductPrice* m, const JsonValue* json) return 0; } -int cart_from_json(Cart* m, const JsonValue* json) -{ - static_assert(sizeof(Cart) == 16, "model has changed"); - - 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) { static_assert(sizeof(CartItem) == 32, "model has changed"); ObjField fields[] = { { "id", JsonType_Number }, - { "cart_id", JsonType_Number }, + { "user_id", JsonType_Number }, { "product_id", JsonType_Number }, { "amount", JsonType_Number }, }; @@ -387,7 +344,7 @@ int cart_item_from_json(CartItem* m, const JsonValue* json) return -1; *m = (CartItem) { .id = GET_INT("id"), - .cart_id = GET_INT("cart_id"), + .user_id = GET_INT("user_id"), .product_id = GET_INT("product_id"), .amount = GET_INT("amount"), }; diff --git a/backend/src/models/models.h b/backend/src/models/models.h index d62b979..219a0ce 100644 --- a/backend/src/models/models.h +++ b/backend/src/models/models.h @@ -34,11 +34,6 @@ typedef struct { typedef struct { int64_t id; int64_t user_id; -} Cart; - -typedef struct { - int64_t id; - int64_t cart_id; int64_t product_id; int64_t amount; } CartItem; @@ -47,7 +42,6 @@ void user_destroy(User* model); void coord_destroy(Coord* model); void product_destroy(Product* model); void product_price_destroy(ProductPrice* model); -void cart_destroy(Cart* model); void cart_item_destroy(CartItem* model); // diff --git a/backend/src/models/models_json.h b/backend/src/models/models_json.h index 256d56f..7c63df1 100644 --- a/backend/src/models/models_json.h +++ b/backend/src/models/models_json.h @@ -9,7 +9,6 @@ DEFINE_MODEL_JSON(User, user) DEFINE_MODEL_JSON(Coord, coord) DEFINE_MODEL_JSON(Product, product) DEFINE_MODEL_JSON(ProductPrice, product_price) -DEFINE_MODEL_JSON(Cart, cart) DEFINE_MODEL_JSON(CartItem, cart_item) DEFINE_MODEL_JSON(UsersRegisterReq, users_register_req) diff --git a/backend/src/util/panic.h b/backend/src/util/panic.h index e6d26e7..b300ef4 100644 --- a/backend/src/util/panic.h +++ b/backend/src/util/panic.h @@ -4,12 +4,18 @@ #include #define PANIC(...) \ - (fputs("\x1b[1;31mpanic\x1b[97m: ", stderr), fprintf(stderr, __VA_ARGS__), \ - fprintf(stderr, "\x1b[0m\n\tin %s:%d\n\tat %s:%d\n", __func__, \ - __LINE__, __FILE__, __LINE__), \ - exit(1), (void)0) + (fputs("\x1b[1;31mpanic\x1b[97m: ", stderr), \ + fprintf(stderr, __VA_ARGS__), \ + fprintf(stderr, \ + "\x1b[0m\n\tin %s:%d\n\tat %s:%d\n", \ + __func__, \ + __LINE__, \ + __FILE__, \ + __LINE__), \ + exit(1), \ + (void)0) -__attribute__((unused)) static inline void ___include_user(void) +__attribute__((unused)) static inline void ___panic_h_include_user(void) { PANIC(""); } diff --git a/backend/src/util/str.c b/backend/src/util/str.c index 02d9161..b8054e5 100644 --- a/backend/src/util/str.c +++ b/backend/src/util/str.c @@ -260,7 +260,8 @@ char* str_random(size_t length) return string; } -void str_util_test(void) +#ifdef RUN_TESTS +void test_util_str(void) { { char* hash = str_hash("1234"); @@ -282,3 +283,4 @@ void str_util_test(void) free(token_2); } } +#endif diff --git a/backend/src/util/str.h b/backend/src/util/str.h index 5ccf646..ae26315 100644 --- a/backend/src/util/str.h +++ b/backend/src/util/str.h @@ -42,4 +42,6 @@ uint64_t str_fast_hash(const char* input); char* str_random(size_t length); -void str_util_test(void); +#ifdef RUN_TESTS +void test_util_str(void); +#endif