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 src/backend/taler-merchant-httpd_get-private-categories-CATEGORY_ID.c
18 : * @brief implement GET /private/categories/$ID
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler-merchant-httpd_get-private-categories-CATEGORY_ID.h"
23 : #include <taler/taler_json_lib.h>
24 : #include "merchant-database/get_category.h"
25 :
26 :
27 : /**
28 : * Handle a GET "/private/categories/$ID" request.
29 : *
30 : * @param rh context of the handler
31 : * @param connection the MHD connection to handle
32 : * @param[in,out] hc context with further information about the request
33 : * @return MHD result code
34 : */
35 : enum MHD_Result
36 0 : TMH_private_get_categories_ID (
37 : const struct TMH_RequestHandler *rh,
38 : struct MHD_Connection *connection,
39 : struct TMH_HandlerContext *hc)
40 : {
41 0 : struct TMH_MerchantInstance *mi = hc->instance;
42 : enum GNUNET_DB_QueryStatus qs;
43 : unsigned long long cnum;
44 : char dummy;
45 : struct TALER_MERCHANTDB_CategoryDetails cd;
46 0 : size_t num_products = 0;
47 0 : char *products = NULL;
48 :
49 0 : GNUNET_assert (NULL != mi);
50 0 : GNUNET_assert (NULL != hc->infix);
51 0 : if (1 != sscanf (hc->infix,
52 : "%llu%c",
53 : &cnum,
54 : &dummy))
55 : {
56 0 : GNUNET_break_op (0);
57 0 : return TALER_MHD_reply_with_error (
58 : connection,
59 : MHD_HTTP_BAD_REQUEST,
60 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
61 : "category_id must be a number");
62 : }
63 :
64 0 : qs = TALER_MERCHANTDB_get_category (TMH_db,
65 0 : mi->settings.id,
66 : cnum,
67 : &cd,
68 : &num_products,
69 : &products);
70 0 : if (0 > qs)
71 : {
72 0 : GNUNET_break (0);
73 0 : return TALER_MHD_reply_with_error (connection,
74 : MHD_HTTP_INTERNAL_SERVER_ERROR,
75 : TALER_EC_GENERIC_DB_FETCH_FAILED,
76 : "get_category");
77 : }
78 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
79 : {
80 0 : return TALER_MHD_reply_with_error (connection,
81 : MHD_HTTP_NOT_FOUND,
82 : TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN,
83 0 : hc->infix);
84 : }
85 : {
86 : enum MHD_Result ret;
87 : json_t *jproducts;
88 0 : const char *pos = products;
89 :
90 0 : jproducts = json_array ();
91 0 : GNUNET_assert (NULL != jproducts);
92 0 : for (unsigned int i = 0; i<num_products; i++)
93 : {
94 0 : const char *product_id = pos;
95 : json_t *jprod;
96 :
97 0 : pos = pos + strlen (product_id) + 1;
98 0 : jprod = GNUNET_JSON_PACK (
99 : GNUNET_JSON_pack_string ("product_id",
100 : product_id));
101 0 : GNUNET_assert (0 ==
102 : json_array_append_new (jproducts,
103 : jprod));
104 : }
105 0 : GNUNET_free (products);
106 0 : ret = TALER_MHD_REPLY_JSON_PACK (
107 : connection,
108 : MHD_HTTP_OK,
109 : GNUNET_JSON_pack_string ("name",
110 : cd.category_name),
111 : GNUNET_JSON_pack_allow_null (
112 : GNUNET_JSON_pack_object_incref ("name_i18n",
113 : cd.category_name_i18n)),
114 : GNUNET_JSON_pack_array_steal ("products",
115 : jproducts));
116 0 : TALER_MERCHANTDB_category_details_free (&cd);
117 0 : return ret;
118 : }
119 : }
120 :
121 :
122 : /* end of taler-merchant-httpd_get-private-categories-CATEGORY_ID.c */
|