diff options
author | Sebastian <sebasjm@gmail.com> | 2024-08-01 12:16:03 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2024-08-01 12:16:03 -0300 |
commit | 13885d5a8d3d759b9183819a5d8138253c18cb20 (patch) | |
tree | a1734a20880715de65b0f6162db747337fdfcd37 | |
parent | 873b48b5aabe28acbf0aeb274cd946fc1207b894 (diff) |
fix get category details
-rw-r--r-- | src/backend/taler-merchant-httpd_private-get-categories-ID.c | 35 | ||||
-rw-r--r-- | src/backenddb/merchantdb_helper.c | 25 | ||||
-rw-r--r-- | src/backenddb/pg_select_category.c | 73 | ||||
-rw-r--r-- | src/backenddb/pg_select_category.h | 7 | ||||
-rw-r--r-- | src/include/taler_merchantdb_plugin.h | 7 |
5 files changed, 116 insertions, 31 deletions
diff --git a/src/backend/taler-merchant-httpd_private-get-categories-ID.c b/src/backend/taler-merchant-httpd_private-get-categories-ID.c index 02ef3495..56877832 100644 --- a/src/backend/taler-merchant-httpd_private-get-categories-ID.c +++ b/src/backend/taler-merchant-httpd_private-get-categories-ID.c @@ -42,6 +42,8 @@ TMH_private_get_categories_ID ( unsigned long long cnum; char dummy; struct TALER_MERCHANTDB_CategoryDetails cd; + size_t num_products = 0; + const char* *products = NULL; GNUNET_assert (NULL != mi); GNUNET_assert (NULL != hc->infix); @@ -60,7 +62,9 @@ TMH_private_get_categories_ID ( qs = TMH_db->select_category (TMH_db->cls, mi->settings.id, cnum, - &cd); + &cd, + &num_products, + &products); if (0 > qs) { GNUNET_break (0); @@ -78,25 +82,28 @@ TMH_private_get_categories_ID ( } { MHD_RESULT ret; - json_t *products; + json_t *jproducts; - products = json_array (); - GNUNET_assert (NULL != products); - for (unsigned int i = 0; i<cd.num_products; i++) + jproducts = json_array (); + GNUNET_assert (NULL != jproducts); + for (unsigned int i = 0; i<num_products; i++) { - const struct TALER_MERCHANTDB_ProductSummary *product - = &cd.products[i]; + const char* product_id = products[i]; json_t *jprod; jprod = GNUNET_JSON_PACK ( GNUNET_JSON_pack_string ("product_id", - product->product_id), - GNUNET_JSON_pack_string ("description", - product->description), - GNUNET_JSON_pack_object_steal ("description_i18n", - product->description_i18n)); + product_id) + // FIXME: also return product details + // GNUNET_JSON_pack_string ("product_id", + // product->product_id), + // GNUNET_JSON_pack_string ("description", + // product->description), + // GNUNET_JSON_pack_object_steal ("description_i18n", + // product->description_i18n) + ); GNUNET_assert (0 == - json_array_append_new (products, + json_array_append_new (jproducts, jprod)); } @@ -109,7 +116,7 @@ TMH_private_get_categories_ID ( GNUNET_JSON_pack_object_incref ("name_i18n", cd.category_name_i18n)), GNUNET_JSON_pack_array_steal ("products", - products)); + jproducts)); TALER_MERCHANTDB_category_details_free (&cd); return ret; } diff --git a/src/backenddb/merchantdb_helper.c b/src/backenddb/merchantdb_helper.c index 72b1c9a7..4397fb36 100644 --- a/src/backenddb/merchantdb_helper.c +++ b/src/backenddb/merchantdb_helper.c @@ -88,18 +88,19 @@ TALER_MERCHANTDB_category_details_free ( { GNUNET_free (cd->category_name); json_decref (cd->category_name_i18n); - for (unsigned int i = 0; i<cd->num_products; i++) - { - struct TALER_MERCHANTDB_ProductSummary *ps - = &cd->products[i]; - - GNUNET_free (ps->product_id); - GNUNET_free (ps->description); - json_decref (ps->description_i18n); - } - GNUNET_array_grow (cd->products, - cd->num_products, - 0); + // FIXME: also return product details + // for (unsigned int i = 0; i<cd->num_products; i++) + // { + // const char* *ps + // = &cd->products[i]; + + // GNUNET_free (ps); + // // GNUNET_free (ps->description); + // // json_decref (ps->description_i18n); + // } + // GNUNET_array_grow (cd->products, + // cd->num_products, + // 0); } diff --git a/src/backenddb/pg_select_category.c b/src/backenddb/pg_select_category.c index c20d7bb7..74e69a0b 100644 --- a/src/backenddb/pg_select_category.c +++ b/src/backenddb/pg_select_category.c @@ -30,8 +30,75 @@ enum GNUNET_DB_QueryStatus TMH_PG_select_category (void *cls, const char *instance_id, uint64_t category_id, - struct TALER_MERCHANTDB_CategoryDetails *cd) + struct TALER_MERCHANTDB_CategoryDetails *cd, + size_t *num_products, + const char* **products) { - GNUNET_break (0); // FIXME - return GNUNET_DB_STATUS_HARD_ERROR; + struct PostgresClosure *pg = cls; + struct GNUNET_PQ_QueryParam params[] = { + GNUNET_PQ_query_param_string (instance_id), + GNUNET_PQ_query_param_uint64 (&category_id), + GNUNET_PQ_query_param_end + }; + + if (NULL == cd) + { + struct GNUNET_PQ_ResultSpec rs_null[] = { + GNUNET_PQ_result_spec_end + }; + + check_connection (pg); + return GNUNET_PQ_eval_prepared_singleton_select (pg->conn, + "select_category", + params, + rs_null); + } + else + { + struct GNUNET_PQ_ResultSpec rs[] = { + GNUNET_PQ_result_spec_string ("category_name", + &cd->category_name), + TALER_PQ_result_spec_json ("category_name_i18n", + &cd->category_name_i18n), + GNUNET_PQ_result_spec_array_string (pg->conn, + "products", + num_products, + products), + GNUNET_PQ_result_spec_end + }; + + PREPARE (pg, + "select_category", + "SELECT" + " category_name" + ",category_name_i18n" + ",t.product_array AS products" + " FROM merchant_categories mc" + " JOIN merchant_instances inst" + " USING (merchant_serial)" + ",LATERAL (" + " SELECT ARRAY (" + " SELECT " + " mi.product_id AS product_id" + // FIXME: also return product details (or NOT) + // " ,mi.description AS description" + // " ,mi.description_i18n AS description_i18n" + " FROM merchant_product_categories mpc" + " JOIN merchant_inventory mi" + " USING (product_serial)" + " WHERE mpc.category_serial = mc.category_serial" + " ) AS product_array" + " ) t" + " WHERE inst.merchant_id=$1" + " AND mc.category_serial=$2"); + + + check_connection (pg); + return GNUNET_PQ_eval_prepared_singleton_select ( + pg->conn, + "select_category", + params, + rs); + + } } diff --git a/src/backenddb/pg_select_category.h b/src/backenddb/pg_select_category.h index 9eeb14aa..3b6e458b 100644 --- a/src/backenddb/pg_select_category.h +++ b/src/backenddb/pg_select_category.h @@ -34,12 +34,17 @@ * @param category_id category to update * @param[out] cd set to the category details on success, can be NULL * (in that case we only want to check if the category exists) + * @param[out] num_products set to length of @a products array + * @param[out] products set to array of products the + * category is in, caller must free() it. * @return database result code */ enum GNUNET_DB_QueryStatus TMH_PG_select_category (void *cls, const char *instance_id, uint64_t category_id, - struct TALER_MERCHANTDB_CategoryDetails *cd); + struct TALER_MERCHANTDB_CategoryDetails *cd, + size_t *num_products, + const char* **products); #endif diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h index 96a3d001..7b380537 100644 --- a/src/include/taler_merchantdb_plugin.h +++ b/src/include/taler_merchantdb_plugin.h @@ -3266,13 +3266,18 @@ struct TALER_MERCHANTDB_Plugin * @param category_id category to update * @param[out] cd set to the category details on success, can be NULL * (in that case we only want to check if the category exists) + * @param[out] num_products set to length of @a products array + * @param[out] products set to array of products the + * category is in, caller must free() it. * @return database result code */ enum GNUNET_DB_QueryStatus (*select_category)(void *cls, const char *instance_id, uint64_t category_id, - struct TALER_MERCHANTDB_CategoryDetails *cd); + struct TALER_MERCHANTDB_CategoryDetails *cd, + size_t *num_products, + const char* **products); /** |