Line data Source code
1 : /* 2 : This file is part of TALER 3 : (C) 2022-2024 Taler Systems SA 4 : 5 : TALER is free software; you can redistribute it and/or modify it under the 6 : terms of the GNU Affero General Public License as published by the Free Software 7 : Foundation; either version 3, or (at your option) any later version. 8 : 9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 : 13 : You should have received a copy of the GNU General Public License along with 14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 : */ 16 : /** 17 : * @file taler-merchant-httpd_private-get-categories-ID.c 18 : * @brief implement GET /private/categories/$ID 19 : * @author Christian Grothoff 20 : */ 21 : #include "platform.h" 22 : #include "taler-merchant-httpd_private-get-categories-ID.h" 23 : #include <taler/taler_json_lib.h> 24 : 25 : 26 : /** 27 : * Handle a GET "/private/categories/$ID" request. 28 : * 29 : * @param rh context of the handler 30 : * @param connection the MHD connection to handle 31 : * @param[in,out] hc context with further information about the request 32 : * @return MHD result code 33 : */ 34 : MHD_RESULT 35 0 : TMH_private_get_categories_ID ( 36 : const struct TMH_RequestHandler *rh, 37 : struct MHD_Connection *connection, 38 : struct TMH_HandlerContext *hc) 39 : { 40 0 : struct TMH_MerchantInstance *mi = hc->instance; 41 : enum GNUNET_DB_QueryStatus qs; 42 : unsigned long long cnum; 43 : char dummy; 44 : struct TALER_MERCHANTDB_CategoryDetails cd; 45 0 : size_t num_products = 0; 46 0 : char *products = NULL; 47 : 48 0 : GNUNET_assert (NULL != mi); 49 0 : GNUNET_assert (NULL != hc->infix); 50 0 : if (1 != sscanf (hc->infix, 51 : "%llu%c", 52 : &cnum, 53 : &dummy)) 54 : { 55 0 : GNUNET_break_op (0); 56 0 : return TALER_MHD_reply_with_error ( 57 : connection, 58 : MHD_HTTP_BAD_REQUEST, 59 : TALER_EC_GENERIC_PARAMETER_MALFORMED, 60 : "category_id must be a number"); 61 : } 62 : 63 0 : qs = TMH_db->select_category (TMH_db->cls, 64 0 : mi->settings.id, 65 : cnum, 66 : &cd, 67 : &num_products, 68 : &products); 69 0 : if (0 > qs) 70 : { 71 0 : GNUNET_break (0); 72 0 : return TALER_MHD_reply_with_error (connection, 73 : MHD_HTTP_INTERNAL_SERVER_ERROR, 74 : TALER_EC_GENERIC_DB_FETCH_FAILED, 75 : "select_category"); 76 : } 77 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 78 : { 79 0 : return TALER_MHD_reply_with_error (connection, 80 : MHD_HTTP_NOT_FOUND, 81 : TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN, 82 0 : hc->infix); 83 : } 84 : { 85 : MHD_RESULT ret; 86 : json_t *jproducts; 87 0 : const char *pos = products; 88 : 89 0 : jproducts = json_array (); 90 0 : GNUNET_assert (NULL != jproducts); 91 0 : for (unsigned int i = 0; i<num_products; i++) 92 : { 93 0 : const char *product_id = pos; 94 : json_t *jprod; 95 : 96 0 : pos = pos + strlen (product_id) + 1; 97 0 : jprod = GNUNET_JSON_PACK ( 98 : GNUNET_JSON_pack_string ("product_id", 99 : product_id)); 100 0 : GNUNET_assert (0 == 101 : json_array_append_new (jproducts, 102 : jprod)); 103 : } 104 0 : GNUNET_free (products); 105 0 : ret = TALER_MHD_REPLY_JSON_PACK ( 106 : connection, 107 : MHD_HTTP_OK, 108 : GNUNET_JSON_pack_string ("name", 109 : cd.category_name), 110 : GNUNET_JSON_pack_allow_null ( 111 : GNUNET_JSON_pack_object_incref ("name_i18n", 112 : cd.category_name_i18n)), 113 : GNUNET_JSON_pack_array_steal ("products", 114 : jproducts)); 115 0 : TALER_MERCHANTDB_category_details_free (&cd); 116 0 : return ret; 117 : } 118 : } 119 : 120 : 121 : /* end of taler-merchant-httpd_private-get-categories-ID.c */