mirror of
https://github.com/Mercantec-GHC/h4-projekt-gruppe-0-sm.git
synced 2025-04-27 16:24:07 +02:00
fix products, modify user
This commit is contained in:
parent
f58dfae118
commit
f8438c591b
@ -1,6 +1,7 @@
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
email TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
balance_dkk_cent INT NOT NULL
|
||||
@ -48,7 +49,7 @@ CREATE TABLE IF NOT EXISTS cart_items (
|
||||
|
||||
|
||||
|
||||
INSERT OR REPLACE INTO users VALUES(1,'test@email.com','d980840fcb82970ab86656feebdccdd288be0e9b05f14e712b59529a2868fee3d980840fcb82970ab86656feebdccdd288be0e9b05f14e712b59529a2868fee3',10000);
|
||||
INSERT OR REPLACE INTO users VALUES(1,'User','test@email.com','d980840fcb82970ab86656feebdccdd288be0e9b05f14e712b59529a2868fee3d980840fcb82970ab86656feebdccdd288be0e9b05f14e712b59529a2868fee3',10000);
|
||||
|
||||
INSERT OR REPLACE INTO products VALUES(1,'Letmælk',1195,'Mælk fra ko',NULL,NULL);
|
||||
INSERT OR REPLACE INTO products VALUES(2,'Smør',2000,'Smør fra mejeri',NULL,NULL);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "db.h"
|
||||
#include "http_server.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -28,3 +29,5 @@ typedef struct {
|
||||
RESPOND(HTTP_CTX, STATUS, "text/html", __VA_ARGS__)
|
||||
#define RESPOND_JSON(HTTP_CTX, STATUS, ...) \
|
||||
RESPOND(HTTP_CTX, STATUS, "application/json", __VA_ARGS__)
|
||||
|
||||
void route_get_products_all(HttpCtx* ctx);
|
||||
|
@ -1,4 +1,3 @@
|
||||
#include "products.h"
|
||||
#include "../controllers.h"
|
||||
#include "../http_server.h"
|
||||
#include "../models_json.h"
|
||||
|
@ -1,6 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "../controllers.h"
|
||||
#include "../http_server.h"
|
||||
|
||||
void route_get_products_all(HttpCtx* ctx);
|
@ -1,6 +1,7 @@
|
||||
#include "db_sqlite.h"
|
||||
#include "db.h"
|
||||
#include "str_util.h"
|
||||
#include <assert.h>
|
||||
#include <sqlite3.h>
|
||||
#include <stdio.h>
|
||||
|
||||
@ -66,19 +67,22 @@ static inline void disconnect(sqlite3* connection)
|
||||
|
||||
DbRes db_user_insert(Db* db, User* user)
|
||||
{
|
||||
static_assert(sizeof(User) == 40, "model has changed");
|
||||
|
||||
sqlite3* connection;
|
||||
CONNECT;
|
||||
DbRes res;
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
sqlite3_prepare_v2(connection,
|
||||
"INSERT INTO users (email, password_hash, balance_dkk_cent) "
|
||||
"INSERT INTO users (name, email, password_hash, balance_dkk_cent) "
|
||||
"VALUES (?, ?, ?)",
|
||||
-1, &stmt, NULL);
|
||||
|
||||
sqlite3_bind_text(stmt, 1, user->email, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, user->password_hash, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(stmt, 3, user->balance_dkk_cent);
|
||||
sqlite3_bind_text(stmt, 1, user->name, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, user->email, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, user->password_hash, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int64(stmt, 4, user->balance_dkk_cent);
|
||||
|
||||
int step_res = sqlite3_step(stmt);
|
||||
if (step_res != SQLITE_DONE) {
|
||||
@ -97,13 +101,15 @@ l0_return:
|
||||
|
||||
DbRes db_user_from_id(Db* db, User* user, int64_t id)
|
||||
{
|
||||
static_assert(sizeof(User) == 40, "model has changed");
|
||||
|
||||
sqlite3* connection;
|
||||
CONNECT;
|
||||
DbRes res;
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
sqlite3_prepare_v2(connection,
|
||||
"SELECT id, email, password_hash, balance_dkk_cent"
|
||||
"SELECT id, name, email, password_hash, balance_dkk_cent"
|
||||
"FROM users WHERE id = ?",
|
||||
-1, &stmt, NULL);
|
||||
sqlite3_bind_int64(stmt, 1, id);
|
||||
@ -119,9 +125,10 @@ DbRes db_user_from_id(Db* db, User* user, int64_t id)
|
||||
}
|
||||
*user = (User) {
|
||||
.id = GET_INT(0),
|
||||
.email = GET_STR(1),
|
||||
.password_hash = GET_STR(2),
|
||||
.balance_dkk_cent = GET_INT(3),
|
||||
.name = GET_STR(1),
|
||||
.email = GET_STR(2),
|
||||
.password_hash = GET_STR(3),
|
||||
.balance_dkk_cent = GET_INT(4),
|
||||
};
|
||||
|
||||
res = DbRes_Ok;
|
||||
|
@ -274,7 +274,7 @@ static inline void worker_handle_request(Worker* worker, Client* client)
|
||||
MAX_HEADER_BUFFER_SIZE);
|
||||
goto l0_return;
|
||||
}
|
||||
// puts((char*)buffer);
|
||||
puts((char*)buffer);
|
||||
|
||||
Req req;
|
||||
size_t body_idx;
|
||||
@ -356,8 +356,10 @@ static inline int parse_header(
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (path_str.len >= MAX_PATH_LEN + 1)
|
||||
if (path_str.len >= MAX_PATH_LEN + 1) {
|
||||
fprintf(stderr, "error: path too long\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char* path = calloc(MAX_PATH_LEN + 1, sizeof(char));
|
||||
strncpy(path, path_str.ptr, path_str.len);
|
||||
@ -378,6 +380,7 @@ static inline int parse_header(
|
||||
key_len += 1;
|
||||
}
|
||||
if (key_len == 0 || key_len > MAX_HEADER_KEY_LEN) {
|
||||
fprintf(stderr, "error: header key too long\n");
|
||||
return -1;
|
||||
}
|
||||
size_t value_begin = key_len + 1;
|
||||
@ -386,6 +389,7 @@ static inline int parse_header(
|
||||
}
|
||||
size_t value_len = line.len - value_begin;
|
||||
if (value_len == 0 || value_len > MAX_HEADER_VALUE_LEN) {
|
||||
fprintf(stderr, "error: header value too long, %ld\n", value_len);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ static inline void worker_handle_request(Worker* worker, Client* req);
|
||||
#define MAX_PATH_LEN 128 - 1
|
||||
#define MAX_HEADERS_LEN 32
|
||||
#define MAX_HEADER_KEY_LEN 32 - 1
|
||||
#define MAX_HEADER_VALUE_LEN 128 - 1
|
||||
#define MAX_HEADER_VALUE_LEN 512 - 1
|
||||
|
||||
typedef enum {
|
||||
Method_GET,
|
||||
|
@ -1,5 +1,4 @@
|
||||
#include "controllers.h"
|
||||
#include "controllers/products.h"
|
||||
#include "db_sqlite.h"
|
||||
#include "http_server.h"
|
||||
#include "json.h"
|
||||
|
@ -1,11 +1,13 @@
|
||||
#include "models.h"
|
||||
#include "json.h"
|
||||
#include "str_util.h"
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
void user_free(User* m)
|
||||
{
|
||||
free(m->name);
|
||||
free(m->email);
|
||||
free(m->password_hash);
|
||||
}
|
||||
@ -38,16 +40,19 @@ void cart_item_free(CartItem* m)
|
||||
|
||||
char* user_to_json_string(const User* m)
|
||||
{
|
||||
static_assert(sizeof(User) == 40, "model has changed");
|
||||
|
||||
String string;
|
||||
string_construct(&string);
|
||||
string_pushf(&string,
|
||||
"{"
|
||||
"\"id\":%ld,"
|
||||
"\"name\":\"%s\","
|
||||
"\"email\":\"%s\","
|
||||
"\"password_hash\":\"%s\","
|
||||
"\"balance_dkk_cent\":%ld"
|
||||
"}",
|
||||
m->id, m->email, m->password_hash, m->balance_dkk_cent);
|
||||
m->id, m->name, m->email, m->password_hash, m->balance_dkk_cent);
|
||||
|
||||
char* result = string_copy(&string);
|
||||
string_destroy(&string);
|
||||
@ -56,6 +61,8 @@ char* user_to_json_string(const User* m)
|
||||
|
||||
char* coord_to_json_string(const Coord* m)
|
||||
{
|
||||
static_assert(sizeof(Coord) == 24, "model has changed");
|
||||
|
||||
String string;
|
||||
string_construct(&string);
|
||||
string_pushf(&string,
|
||||
@ -73,6 +80,8 @@ char* coord_to_json_string(const Coord* m)
|
||||
|
||||
char* product_to_json_string(const Product* m)
|
||||
{
|
||||
static_assert(sizeof(Product) == 40, "model has changed");
|
||||
|
||||
String string;
|
||||
string_construct(&string);
|
||||
string_pushf(&string,
|
||||
@ -92,6 +101,8 @@ char* product_to_json_string(const Product* m)
|
||||
|
||||
char* product_price_to_json_string(const ProductPrice* m)
|
||||
{
|
||||
static_assert(sizeof(ProductPrice) == 24, "model has changed");
|
||||
|
||||
String string;
|
||||
string_construct(&string);
|
||||
string_pushf(&string,
|
||||
@ -109,6 +120,8 @@ char* product_price_to_json_string(const ProductPrice* m)
|
||||
|
||||
char* cart_to_json_string(const Cart* m)
|
||||
{
|
||||
static_assert(sizeof(Cart) == 16, "model has changed");
|
||||
|
||||
String string;
|
||||
string_construct(&string);
|
||||
string_pushf(&string,
|
||||
@ -125,6 +138,8 @@ char* cart_to_json_string(const Cart* m)
|
||||
|
||||
char* cart_item_to_json_string(const CartItem* m)
|
||||
{
|
||||
static_assert(sizeof(CartItem) == 24, "model has changed");
|
||||
|
||||
String string;
|
||||
string_construct(&string);
|
||||
string_pushf(&string,
|
||||
@ -167,8 +182,11 @@ static inline bool obj_conforms(
|
||||
|
||||
int user_from_json(User* m, const JsonValue* json)
|
||||
{
|
||||
static_assert(sizeof(User) == 40, "model has changed");
|
||||
|
||||
ObjField fields[] = {
|
||||
{ "id", JsonType_Number },
|
||||
{ "name", JsonType_String },
|
||||
{ "email", JsonType_String },
|
||||
{ "password_hash", JsonType_String },
|
||||
{ "balance_dkk_cent", JsonType_Number },
|
||||
@ -177,6 +195,7 @@ int user_from_json(User* m, const JsonValue* json)
|
||||
return -1;
|
||||
*m = (User) {
|
||||
.id = GET_INT("id"),
|
||||
.name = GET_STR("name"),
|
||||
.email = GET_STR("email"),
|
||||
.password_hash = GET_STR("password_hash"),
|
||||
.balance_dkk_cent = GET_INT("balance_dkk_cent"),
|
||||
@ -186,6 +205,8 @@ int user_from_json(User* m, const JsonValue* json)
|
||||
|
||||
int coord_from_json(Coord* m, const JsonValue* json)
|
||||
{
|
||||
static_assert(sizeof(Coord) == 24, "model has changed");
|
||||
|
||||
ObjField fields[] = {
|
||||
{ "id", JsonType_Number },
|
||||
{ "x", JsonType_Number },
|
||||
@ -203,6 +224,8 @@ 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");
|
||||
|
||||
ObjField fields[] = {
|
||||
{ "id", JsonType_Number },
|
||||
{ "name", JsonType_String },
|
||||
@ -224,6 +247,8 @@ int product_from_json(Product* m, const JsonValue* json)
|
||||
|
||||
int product_price_from_json(ProductPrice* m, const JsonValue* json)
|
||||
{
|
||||
static_assert(sizeof(ProductPrice) == 24, "model has changed");
|
||||
|
||||
ObjField fields[] = {
|
||||
{ "id", JsonType_Number },
|
||||
{ "product_id", JsonType_Number },
|
||||
@ -241,6 +266,8 @@ int product_price_from_json(ProductPrice* m, const JsonValue* json)
|
||||
|
||||
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 },
|
||||
@ -256,6 +283,8 @@ int cart_from_json(Cart* m, const JsonValue* json)
|
||||
|
||||
int cart_item_from_json(CartItem* m, const JsonValue* json)
|
||||
{
|
||||
static_assert(sizeof(CartItem) == 24, "model has changed");
|
||||
|
||||
ObjField fields[] = {
|
||||
{ "id", JsonType_Number },
|
||||
{ "cart_id", JsonType_Number },
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
typedef struct {
|
||||
int64_t id;
|
||||
char* name;
|
||||
char* email;
|
||||
char* password_hash;
|
||||
int64_t balance_dkk_cent;
|
||||
|
@ -46,7 +46,9 @@ void string_push_str(String* string, const char* str)
|
||||
for (size_t i = 0; i < strlen(str); ++i) {
|
||||
string_push(string, str[i]);
|
||||
}
|
||||
string->data[string->size] = '\0';
|
||||
|
||||
string_push(string, '\0');
|
||||
string->size -= 1;
|
||||
}
|
||||
|
||||
void string_push_fmt_va(String* string, const char* fmt, ...)
|
||||
|
Loading…
x
Reference in New Issue
Block a user