add description to products in backend

This commit is contained in:
Mikkel Troels Kongsted 2025-03-13 12:49:13 +01:00
parent 7e5c95032e
commit 81b58351fa
3 changed files with 17 additions and 8 deletions

View File

@ -1,5 +1,6 @@
#include "db_sqlite.h"
#include "db.h"
#include "models.h"
#include "str_util.h"
#include <assert.h>
#include <sqlite3.h>
@ -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);
}

View File

@ -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"),

View File

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