mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
total dkk cent on receipts
This commit is contained in:
parent
a34ed6f653
commit
f0c0ad5766
@ -35,7 +35,8 @@ CREATE TABLE IF NOT EXISTS product_prices (
|
|||||||
CREATE TABLE IF NOT EXISTS receipts (
|
CREATE TABLE IF NOT EXISTS receipts (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
user INTEGER NOT NULL,
|
user INTEGER NOT NULL,
|
||||||
datetime INTEGER NOT NULL,
|
total_dkk_cent INTEGER NOT NULL,
|
||||||
|
timestamp INTEGER NOT NULL,
|
||||||
|
|
||||||
FOREIGN KEY(user) REFERENCES users(id)
|
FOREIGN KEY(user) REFERENCES users(id)
|
||||||
);
|
);
|
||||||
|
@ -73,6 +73,7 @@ void route_post_carts_purchase(HttpCtx* ctx)
|
|||||||
.id = 0,
|
.id = 0,
|
||||||
.user_id = session->user_id,
|
.user_id = session->user_id,
|
||||||
.timestamp = NULL,
|
.timestamp = NULL,
|
||||||
|
.total_dkk_cent = total_dkk_cent,
|
||||||
.products = (ReceiptProductVec) { 0 },
|
.products = (ReceiptProductVec) { 0 },
|
||||||
};
|
};
|
||||||
receipt_product_vec_construct(&receipt.products);
|
receipt_product_vec_construct(&receipt.products);
|
||||||
|
@ -49,6 +49,7 @@ void route_get_receipts_one(HttpCtx* ctx)
|
|||||||
|
|
||||||
ReceiptsOneRes res = {
|
ReceiptsOneRes res = {
|
||||||
.receipt_id = receipt.id,
|
.receipt_id = receipt.id,
|
||||||
|
.total_dkk_cent = receipt.total_dkk_cent,
|
||||||
.timestamp = str_dup(receipt.timestamp),
|
.timestamp = str_dup(receipt.timestamp),
|
||||||
.products = (ReceiptsOneResProductVec) { 0 },
|
.products = (ReceiptsOneResProductVec) { 0 },
|
||||||
};
|
};
|
||||||
|
@ -517,7 +517,7 @@ l0_return:
|
|||||||
|
|
||||||
DbRes db_receipt_insert(Db* db, const Receipt* receipt, int64_t* id)
|
DbRes db_receipt_insert(Db* db, const Receipt* receipt, int64_t* id)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(Receipt) == 48, "model has changed");
|
static_assert(sizeof(Receipt) == 56, "model has changed");
|
||||||
static_assert(sizeof(ReceiptProduct) == 32, "model has changed");
|
static_assert(sizeof(ReceiptProduct) == 32, "model has changed");
|
||||||
|
|
||||||
sqlite3* connection;
|
sqlite3* connection;
|
||||||
@ -526,8 +526,8 @@ DbRes db_receipt_insert(Db* db, const Receipt* receipt, int64_t* id)
|
|||||||
|
|
||||||
sqlite3_stmt* stmt;
|
sqlite3_stmt* stmt;
|
||||||
int prepare_res = sqlite3_prepare_v2(connection,
|
int prepare_res = sqlite3_prepare_v2(connection,
|
||||||
"INSERT INTO receipts (user, datetime) "
|
"INSERT INTO receipts (user, total_dkk_cent, timestamp) "
|
||||||
"VALUES (?, unixepoch('now'))",
|
"VALUES (?, ?, unixepoch('now'))",
|
||||||
-1,
|
-1,
|
||||||
&stmt,
|
&stmt,
|
||||||
NULL);
|
NULL);
|
||||||
@ -539,10 +539,11 @@ DbRes db_receipt_insert(Db* db, const Receipt* receipt, int64_t* id)
|
|||||||
}
|
}
|
||||||
|
|
||||||
sqlite3_bind_int64(stmt, 1, receipt->user_id);
|
sqlite3_bind_int64(stmt, 1, receipt->user_id);
|
||||||
|
sqlite3_bind_int64(stmt, 2, receipt->total_dkk_cent);
|
||||||
|
|
||||||
int step_res = sqlite3_step(stmt);
|
int step_res = sqlite3_step(stmt);
|
||||||
if (step_res != SQLITE_DONE) {
|
if (step_res != SQLITE_DONE) {
|
||||||
fprintf(stderr, "error: %s\n", sqlite3_errmsg(connection));
|
REPORT_SQLITE3_ERROR();
|
||||||
res = DbRes_Error;
|
res = DbRes_Error;
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
}
|
}
|
||||||
@ -574,7 +575,7 @@ DbRes db_receipt_insert(Db* db, const Receipt* receipt, int64_t* id)
|
|||||||
|
|
||||||
int step_res = sqlite3_step(stmt);
|
int step_res = sqlite3_step(stmt);
|
||||||
if (step_res != SQLITE_DONE) {
|
if (step_res != SQLITE_DONE) {
|
||||||
fprintf(stderr, "error: %s\n", sqlite3_errmsg(connection));
|
REPORT_SQLITE3_ERROR();
|
||||||
res = DbRes_Error;
|
res = DbRes_Error;
|
||||||
goto l0_return;
|
goto l0_return;
|
||||||
}
|
}
|
||||||
@ -591,7 +592,7 @@ l0_return:
|
|||||||
DbRes db_receipt_with_id_and_user_id(
|
DbRes db_receipt_with_id_and_user_id(
|
||||||
Db* db, Receipt* receipt, int64_t id, int64_t user_id)
|
Db* db, Receipt* receipt, int64_t id, int64_t user_id)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(Receipt) == 48, "model has changed");
|
static_assert(sizeof(Receipt) == 56, "model has changed");
|
||||||
|
|
||||||
sqlite3* connection;
|
sqlite3* connection;
|
||||||
CONNECT;
|
CONNECT;
|
||||||
@ -599,7 +600,9 @@ DbRes db_receipt_with_id_and_user_id(
|
|||||||
sqlite3_stmt* stmt = NULL;
|
sqlite3_stmt* stmt = NULL;
|
||||||
|
|
||||||
int prepare_res = sqlite3_prepare_v2(connection,
|
int prepare_res = sqlite3_prepare_v2(connection,
|
||||||
"SELECT id, user, datetime(datetime, 'unixepoch') FROM receipts"
|
"SELECT id, user, total_dkk_cent, datetime(timestamp, 'unixepoch') "
|
||||||
|
"FROM "
|
||||||
|
"receipts"
|
||||||
" WHERE id = ? AND user = ?",
|
" WHERE id = ? AND user = ?",
|
||||||
-1,
|
-1,
|
||||||
&stmt,
|
&stmt,
|
||||||
@ -627,7 +630,8 @@ DbRes db_receipt_with_id_and_user_id(
|
|||||||
*receipt = (Receipt) {
|
*receipt = (Receipt) {
|
||||||
.id = GET_INT(0),
|
.id = GET_INT(0),
|
||||||
.user_id = GET_INT(1),
|
.user_id = GET_INT(1),
|
||||||
.timestamp = GET_STR(2),
|
.total_dkk_cent = GET_INT(2),
|
||||||
|
.timestamp = GET_STR(3),
|
||||||
.products = (ReceiptProductVec) { 0 },
|
.products = (ReceiptProductVec) { 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -674,7 +678,8 @@ l0_return:
|
|||||||
DbRes db_receipt_all_headers_with_user_id(
|
DbRes db_receipt_all_headers_with_user_id(
|
||||||
Db* db, ReceiptHeaderVec* receipts, int64_t user_id)
|
Db* db, ReceiptHeaderVec* receipts, int64_t user_id)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(Receipt) == 48, "model has changed");
|
static_assert(sizeof(Receipt) == 56, "model has changed");
|
||||||
|
static_assert(sizeof(ReceiptHeader) == 32, "model has changed");
|
||||||
|
|
||||||
sqlite3* connection;
|
sqlite3* connection;
|
||||||
CONNECT;
|
CONNECT;
|
||||||
@ -683,8 +688,10 @@ DbRes db_receipt_all_headers_with_user_id(
|
|||||||
|
|
||||||
sqlite3_stmt* stmt;
|
sqlite3_stmt* stmt;
|
||||||
sqlite_res = sqlite3_prepare_v2(connection,
|
sqlite_res = sqlite3_prepare_v2(connection,
|
||||||
"SELECT id, user, datetime(datetime, 'unixepoch') FROM receipts WHERE "
|
"SELECT id, user, total_dkk_cent, datetime(timestamp, 'unixepoch')"
|
||||||
"user = ?",
|
" FROM"
|
||||||
|
" receipts WHERE"
|
||||||
|
" user = ?",
|
||||||
-1,
|
-1,
|
||||||
&stmt,
|
&stmt,
|
||||||
NULL);
|
NULL);
|
||||||
@ -700,7 +707,8 @@ DbRes db_receipt_all_headers_with_user_id(
|
|||||||
ReceiptHeader receipt = {
|
ReceiptHeader receipt = {
|
||||||
.id = GET_INT(0),
|
.id = GET_INT(0),
|
||||||
.user_id = GET_INT(1),
|
.user_id = GET_INT(1),
|
||||||
.timestamp = GET_STR(2),
|
.total_dkk_cent = GET_INT(2),
|
||||||
|
.timestamp = GET_STR(3),
|
||||||
};
|
};
|
||||||
receipt_header_vec_push(receipts, receipt);
|
receipt_header_vec_push(receipts, receipt);
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,7 @@ int main(void)
|
|||||||
server, "/product_editor/index.html", route_get_product_editor_html);
|
server, "/product_editor/index.html", route_get_product_editor_html);
|
||||||
http_server_get(server,
|
http_server_get(server,
|
||||||
"/product_editor/product_editor.js",
|
"/product_editor/product_editor.js",
|
||||||
route_get_product_editor_js);
|
route_get_product_editor_html);
|
||||||
|
|
||||||
http_server_post(server, "/api/carts/purchase", route_post_carts_purchase);
|
http_server_post(server, "/api/carts/purchase", route_post_carts_purchase);
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ void receipt_product_destroy(ReceiptProduct* m)
|
|||||||
|
|
||||||
void receipt_destroy(Receipt* m)
|
void receipt_destroy(Receipt* m)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(Receipt) == 48, "model has changed");
|
static_assert(sizeof(Receipt) == 56, "model has changed");
|
||||||
|
|
||||||
free(m->timestamp);
|
free(m->timestamp);
|
||||||
for (size_t i = 0; i < m->products.size; ++i)
|
for (size_t i = 0; i < m->products.size; ++i)
|
||||||
@ -58,7 +58,7 @@ void receipt_destroy(Receipt* m)
|
|||||||
|
|
||||||
void receipt_header_destroy(ReceiptHeader* m)
|
void receipt_header_destroy(ReceiptHeader* m)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(ReceiptHeader) == 24, "model has changed");
|
static_assert(sizeof(ReceiptHeader) == 32, "model has changed");
|
||||||
|
|
||||||
free(m->timestamp);
|
free(m->timestamp);
|
||||||
}
|
}
|
||||||
@ -96,7 +96,7 @@ void receipts_one_res_product_destroy(ReceiptsOneResProduct* m)
|
|||||||
|
|
||||||
void receipts_one_res_destroy(ReceiptsOneRes* m)
|
void receipts_one_res_destroy(ReceiptsOneRes* m)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(ReceiptsOneRes) == 40, "model has changed");
|
static_assert(sizeof(ReceiptsOneRes) == 48, "model has changed");
|
||||||
|
|
||||||
free(m->timestamp);
|
free(m->timestamp);
|
||||||
for (size_t i = 0; i < m->products.size; ++i)
|
for (size_t i = 0; i < m->products.size; ++i)
|
||||||
@ -200,7 +200,7 @@ char* product_price_to_json_string(const ProductPrice* m)
|
|||||||
|
|
||||||
char* receipt_to_json_string(const Receipt* m)
|
char* receipt_to_json_string(const Receipt* m)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(Receipt) == 48, "model has changed");
|
static_assert(sizeof(Receipt) == 56, "model has changed");
|
||||||
|
|
||||||
String string;
|
String string;
|
||||||
string_construct(&string);
|
string_construct(&string);
|
||||||
@ -208,10 +208,12 @@ char* receipt_to_json_string(const Receipt* m)
|
|||||||
"{"
|
"{"
|
||||||
"\"id\":%ld,"
|
"\"id\":%ld,"
|
||||||
"\"user_id\":%ld,"
|
"\"user_id\":%ld,"
|
||||||
|
"\"total_dkk_cent\":%ld,"
|
||||||
"\"timestamp\":\"%s\","
|
"\"timestamp\":\"%s\","
|
||||||
"\"products\":[",
|
"\"products\":[",
|
||||||
m->id,
|
m->id,
|
||||||
m->user_id,
|
m->user_id,
|
||||||
|
m->total_dkk_cent,
|
||||||
m->timestamp);
|
m->timestamp);
|
||||||
|
|
||||||
for (size_t i = 0; i < m->products.size; ++i) {
|
for (size_t i = 0; i < m->products.size; ++i) {
|
||||||
@ -238,7 +240,7 @@ char* receipt_to_json_string(const Receipt* m)
|
|||||||
|
|
||||||
char* receipt_header_to_json_string(const ReceiptHeader* m)
|
char* receipt_header_to_json_string(const ReceiptHeader* m)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(ReceiptHeader) == 24, "model has changed");
|
static_assert(sizeof(ReceiptHeader) == 32, "model has changed");
|
||||||
|
|
||||||
String string;
|
String string;
|
||||||
string_construct(&string);
|
string_construct(&string);
|
||||||
@ -246,9 +248,12 @@ char* receipt_header_to_json_string(const ReceiptHeader* m)
|
|||||||
"{"
|
"{"
|
||||||
"\"id\":%ld,"
|
"\"id\":%ld,"
|
||||||
"\"user_id\":%ld,"
|
"\"user_id\":%ld,"
|
||||||
"\"timestamp\":\"%s\"}",
|
"\"total_dkk_cent\":%ld,"
|
||||||
|
"\"timestamp\":\"%s\""
|
||||||
|
"}",
|
||||||
m->id,
|
m->id,
|
||||||
m->user_id,
|
m->user_id,
|
||||||
|
m->total_dkk_cent,
|
||||||
m->timestamp);
|
m->timestamp);
|
||||||
char* result = string_copy(&string);
|
char* result = string_copy(&string);
|
||||||
string_destroy(&string);
|
string_destroy(&string);
|
||||||
@ -327,16 +332,18 @@ char* receipts_one_res_product_to_json_string(const ReceiptsOneResProduct* m)
|
|||||||
|
|
||||||
char* receipts_one_res_to_json_string(const ReceiptsOneRes* m)
|
char* receipts_one_res_to_json_string(const ReceiptsOneRes* m)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(ReceiptsOneRes) == 40, "model has changed");
|
static_assert(sizeof(ReceiptsOneRes) == 48, "model has changed");
|
||||||
|
|
||||||
String string;
|
String string;
|
||||||
string_construct(&string);
|
string_construct(&string);
|
||||||
string_pushf(&string,
|
string_pushf(&string,
|
||||||
"{"
|
"{"
|
||||||
"\"receipt_id\":%ld,"
|
"\"receipt_id\":%ld,"
|
||||||
|
"\"total_dkk_cent\":%ld,"
|
||||||
"\"timestamp\":\"%s\","
|
"\"timestamp\":\"%s\","
|
||||||
"\"products\":[",
|
"\"products\":[",
|
||||||
m->receipt_id,
|
m->receipt_id,
|
||||||
|
m->total_dkk_cent,
|
||||||
m->timestamp);
|
m->timestamp);
|
||||||
|
|
||||||
for (size_t i = 0; i < m->products.size; ++i) {
|
for (size_t i = 0; i < m->products.size; ++i) {
|
||||||
@ -475,13 +482,13 @@ int product_price_from_json(ProductPrice* m, const JsonValue* json)
|
|||||||
|
|
||||||
int receipt_from_json(Receipt* m, const JsonValue* json)
|
int receipt_from_json(Receipt* m, const JsonValue* json)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(Receipt) == 48, "model has changed");
|
static_assert(sizeof(Receipt) == 56, "model has changed");
|
||||||
|
|
||||||
PANIC("not implemented");
|
PANIC("not implemented");
|
||||||
}
|
}
|
||||||
int receipt_header_from_json(ReceiptHeader* m, const JsonValue* json)
|
int receipt_header_from_json(ReceiptHeader* m, const JsonValue* json)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(ReceiptHeader) == 24, "model has changed");
|
static_assert(sizeof(ReceiptHeader) == 32, "model has changed");
|
||||||
|
|
||||||
PANIC("not implemented");
|
PANIC("not implemented");
|
||||||
}
|
}
|
||||||
@ -571,7 +578,7 @@ int receipts_one_res_product_from_json(
|
|||||||
|
|
||||||
int receipts_one_res_from_json(ReceiptsOneRes* m, const JsonValue* json)
|
int receipts_one_res_from_json(ReceiptsOneRes* m, const JsonValue* json)
|
||||||
{
|
{
|
||||||
static_assert(sizeof(ReceiptsOneRes) == 40, "model has changed");
|
static_assert(sizeof(ReceiptsOneRes) == 48, "model has changed");
|
||||||
|
|
||||||
PANIC("not implemented");
|
PANIC("not implemented");
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,7 @@ DECLARE_VEC_TYPE(ReceiptProduct, ReceiptProductVec, receipt_product_vec, )
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
int64_t id;
|
int64_t id;
|
||||||
int64_t user_id;
|
int64_t user_id;
|
||||||
|
int64_t total_dkk_cent;
|
||||||
char* timestamp;
|
char* timestamp;
|
||||||
ReceiptProductVec products;
|
ReceiptProductVec products;
|
||||||
} Receipt;
|
} Receipt;
|
||||||
@ -55,6 +56,7 @@ DECLARE_VEC_TYPE(Receipt, ReceiptVec, receipt_vec, )
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
int64_t id;
|
int64_t id;
|
||||||
int64_t user_id;
|
int64_t user_id;
|
||||||
|
int64_t total_dkk_cent;
|
||||||
char* timestamp;
|
char* timestamp;
|
||||||
} ReceiptHeader;
|
} ReceiptHeader;
|
||||||
|
|
||||||
@ -113,6 +115,7 @@ DECLARE_VEC_TYPE(ReceiptsOneResProduct,
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int64_t receipt_id;
|
int64_t receipt_id;
|
||||||
|
int64_t total_dkk_cent;
|
||||||
char* timestamp;
|
char* timestamp;
|
||||||
ReceiptsOneResProductVec products;
|
ReceiptsOneResProductVec products;
|
||||||
} ReceiptsOneRes;
|
} ReceiptsOneRes;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user