summaryrefslogtreecommitdiff
path: root/src/cmd_customers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd_customers.c')
-rw-r--r--src/cmd_customers.c32
1 files changed, 12 insertions, 20 deletions
diff --git a/src/cmd_customers.c b/src/cmd_customers.c
index a46cd32..d259a8e 100644
--- a/src/cmd_customers.c
+++ b/src/cmd_customers.c
@@ -129,7 +129,7 @@ static yyjson_mut_val *customer_lookup(struct req *r, int64_t id, int *found)
" WHERE org_id=?1 AND id=?2",
-1, &st, NULL) != SQLITE_OK) {
*found = -1;
- fail(r, "INTERNAL", "database error");
+ db_error(r);
return NULL;
}
sqlite3_bind_int64(st, 1, r->org_id);
@@ -152,7 +152,7 @@ static int customer_name_taken(struct req *r, const char *name, int64_t except_i
"SELECT id FROM customers WHERE org_id=?1 AND name=?2"
" AND id<>?3",
-1, &st, NULL) != SQLITE_OK) {
- fail(r, "INTERNAL", "database error");
+ db_error(r);
return -1;
}
sqlite3_bind_int64(st, 1, r->org_id);
@@ -173,7 +173,7 @@ static yyjson_mut_val *h_customer_list(struct req *r)
"SELECT " CUSTOMER_COLUMNS " FROM customers WHERE org_id=?1"
" AND (?2=0 OR active=1) ORDER BY name COLLATE NOCASE, id",
-1, &st, NULL) != SQLITE_OK)
- return fail(r, "INTERNAL", "database error");
+ return db_error(r);
sqlite3_bind_int64(st, 1, r->org_id);
sqlite3_bind_int(st, 2, active_only);
yyjson_mut_val *items = yyjson_mut_arr(r->rdoc);
@@ -248,7 +248,7 @@ static yyjson_mut_val *h_customer_create(struct req *r)
"vat_nr,email,your_ref,payment_days,notes,created_at)"
" VALUES(?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12)",
-1, &st, NULL) != SQLITE_OK)
- return fail(r, "INTERNAL", "database error");
+ return db_error(r);
sqlite3_bind_int64(st, 1, r->org_id);
sqlite3_bind_text(st, 2, in.name, -1, SQLITE_TRANSIENT);
sqlite3_bind_text(st, 3, address, -1, SQLITE_TRANSIENT);
@@ -266,7 +266,7 @@ static yyjson_mut_val *h_customer_create(struct req *r)
if (rc != SQLITE_DONE) {
if ((rc & 0xff) == SQLITE_CONSTRAINT)
return fail(r, "CONFLICT", "a customer with this name already exists");
- return fail(r, "INTERNAL", sqlite3_errmsg(r->db));
+ return db_sqlite_error(r);
}
int64_t id = db_last_id(r->db);
char *reqjson = audit_args_json(r->args);
@@ -325,7 +325,7 @@ static yyjson_mut_val *h_customer_update(struct req *r)
"created_at,COALESCE(updated_at,'')"
" FROM customers WHERE org_id=?1 AND id=?2",
-1, &st, NULL) != SQLITE_OK)
- return fail(r, "INTERNAL", "database error");
+ return db_error(r);
sqlite3_bind_int64(st, 1, r->org_id);
sqlite3_bind_int64(st, 2, id);
const char *texts[] = { in.name, in.address, in.postal_code, in.city,
@@ -333,11 +333,7 @@ static yyjson_mut_val *h_customer_update(struct req *r)
in.notes };
static const int bind_index[] = { 3, 4, 5, 6, 7, 8, 9, 10, 12 };
for (size_t i = 0; i < sizeof texts / sizeof texts[0]; i++) {
- if (texts[i])
- sqlite3_bind_text(st, bind_index[i], texts[i], -1,
- SQLITE_TRANSIENT);
- else
- sqlite3_bind_null(st, bind_index[i]);
+ bind_text_or_null(st, bind_index[i], texts[i]);
}
sqlite3_bind_int64(st, 11, in.have_payment ? in.payment_days : -1);
sqlite3_bind_int64(st, 13, in.have_active ? in.active : -1);
@@ -366,7 +362,7 @@ static yyjson_mut_val *h_customer_update(struct req *r)
" active=CASE WHEN ?13<0 THEN active ELSE ?13 END,"
" updated_at=?14 WHERE org_id=?1 AND id=?2",
-1, &st, NULL) != SQLITE_OK)
- return fail(r, "INTERNAL", "database error");
+ return db_error(r);
sqlite3_bind_int64(st, 1, r->org_id);
sqlite3_bind_int64(st, 2, id);
const char *texts[] = { in.name, in.address, in.postal_code, in.city,
@@ -374,11 +370,7 @@ static yyjson_mut_val *h_customer_update(struct req *r)
in.notes };
static const int text_index[] = { 3, 4, 5, 6, 7, 8, 9, 10, 12 };
for (size_t i = 0; i < sizeof texts / sizeof texts[0]; i++) {
- if (texts[i])
- sqlite3_bind_text(st, text_index[i], texts[i], -1,
- SQLITE_TRANSIENT);
- else
- sqlite3_bind_null(st, text_index[i]);
+ bind_text_or_null(st, text_index[i], texts[i]);
}
sqlite3_bind_int64(st, 11, in.have_payment ? in.payment_days : -1);
sqlite3_bind_int64(st, 13, in.have_active ? in.active : -1);
@@ -388,7 +380,7 @@ static yyjson_mut_val *h_customer_update(struct req *r)
if (rc != SQLITE_DONE) {
if ((rc & 0xff) == SQLITE_CONSTRAINT)
return fail(r, "CONFLICT", "a customer with this name already exists");
- return fail(r, "INTERNAL", sqlite3_errmsg(r->db));
+ return db_sqlite_error(r);
}
if (sqlite3_changes(r->db) == 0)
return fail(r, "NOT_FOUND", "customer not found");
@@ -436,7 +428,7 @@ static yyjson_mut_val *h_customer_archive(struct req *r)
"UPDATE customers SET active=?3, updated_at=?4"
" WHERE org_id=?1 AND id=?2",
-1, &st, NULL) != SQLITE_OK)
- return fail(r, "INTERNAL", "database error");
+ return db_error(r);
sqlite3_bind_int64(st, 1, r->org_id);
sqlite3_bind_int64(st, 2, id);
sqlite3_bind_int(st, 3, active);
@@ -444,7 +436,7 @@ static yyjson_mut_val *h_customer_archive(struct req *r)
int rc = sqlite3_step(st);
sqlite3_finalize(st);
if (rc != SQLITE_DONE)
- return fail(r, "INTERNAL", sqlite3_errmsg(r->db));
+ return db_sqlite_error(r);
if (sqlite3_changes(r->db) == 0)
return fail(r, "NOT_FOUND", "customer not found");
char *reqjson = audit_args_json(r->args);