diff --git a/backend/src/db_sqlite.c b/backend/src/db_sqlite.c index d81c4a3..3fd621a 100644 --- a/backend/src/db_sqlite.c +++ b/backend/src/db_sqlite.c @@ -1,5 +1,6 @@ #include "db_sqlite.h" #include "db.h" +#include "models.h" #include "str_util.h" #include #include @@ -117,6 +118,7 @@ DbRes db_user_from_id(Db* db, User* user, int64_t id) int step_res = sqlite3_step(stmt); if (step_res == SQLITE_DONE) { res = DbRes_NotFound; + puts("didn't find user"); goto l0_return; } else if (step_res != SQLITE_ROW) { fprintf(stderr, "error: %s\n", sqlite3_errmsg(connection)); @@ -221,6 +223,7 @@ l0_return: DbRes db_product_all(Db* db, ProductVec* vec) { + static_assert(sizeof(Product) == 48, "model has changed"); sqlite3* connection; CONNECT; DbRes res; @@ -228,7 +231,7 @@ DbRes db_product_all(Db* db, ProductVec* vec) sqlite3_stmt* stmt; sqlite_res = sqlite3_prepare_v2(connection, - "SELECT id, name, price_dkk_cent, coord, barcode FROM products", -1, + "SELECT id, name, description, price_dkk_cent, coord, barcode FROM products", -1, &stmt, NULL); if (sqlite_res != SQLITE_OK) { fprintf(stderr, "error: %s\n", sqlite3_errmsg(connection)); @@ -240,9 +243,10 @@ DbRes db_product_all(Db* db, ProductVec* vec) Product product = { .id = GET_INT(0), .name = GET_STR(1), - .price_dkk_cent = GET_INT(2), - .coord_id = GET_INT(3), - .barcode = GET_STR(4), + .description = GET_STR(2), + .price_dkk_cent = GET_INT(3), + .coord_id = GET_INT(4), + .barcode = GET_STR(5), }; product_vec_push(vec, product); } diff --git a/backend/src/models.c b/backend/src/models.c index 148959c..9d5e1b4 100644 --- a/backend/src/models.c +++ b/backend/src/models.c @@ -24,9 +24,10 @@ void coord_destroy(Coord* m) void product_destroy(Product* m) { - static_assert(sizeof(Product) == 40, "model has changed"); + static_assert(sizeof(Product) == 48, "model has changed"); free(m->name); + free(m->description); free(m->barcode); } @@ -110,7 +111,7 @@ char* coord_to_json_string(const Coord* m) char* product_to_json_string(const Product* m) { - static_assert(sizeof(Product) == 40, "model has changed"); + static_assert(sizeof(Product) == 48, "model has changed"); String string; string_construct(&string); @@ -118,11 +119,12 @@ char* product_to_json_string(const Product* m) "{" "\"id\":%ld," "\"name\":\"%s\"," + "\"description\":\"%s\"," "\"price_dkk_cent\":%ld," "\"coord_id\":%ld," "\"barcode\":\"%s\"" "}", - m->id, m->name, m->price_dkk_cent, m->coord_id, m->barcode); + m->id, m->name, m->description, m->price_dkk_cent, m->coord_id, m->barcode); char* result = string_copy(&string); string_destroy(&string); @@ -291,11 +293,12 @@ int coord_from_json(Coord* m, const JsonValue* json) int product_from_json(Product* m, const JsonValue* json) { - static_assert(sizeof(Product) == 40, "model has changed"); + static_assert(sizeof(Product) == 48, "model has changed"); ObjField fields[] = { { "id", JsonType_Number }, { "name", JsonType_String }, + { "description", JsonType_String}, { "price_dkk_cent", JsonType_Number }, { "coord_id", JsonType_Number }, { "barcode", JsonType_String }, @@ -305,6 +308,7 @@ int product_from_json(Product* m, const JsonValue* json) *m = (Product) { .id = GET_INT("id"), .name = GET_STR("name"), + .description = GET_STR("description"), .price_dkk_cent = GET_INT("price_dkk_cent"), .coord_id = GET_INT("coord_id"), .barcode = GET_STR("y"), diff --git a/backend/src/models.h b/backend/src/models.h index e75df51..fd59deb 100644 --- a/backend/src/models.h +++ b/backend/src/models.h @@ -19,6 +19,7 @@ typedef struct { typedef struct { int64_t id; char* name; + char* description; int64_t price_dkk_cent; int64_t coord_id; char* barcode;