Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2019, 2020, 2021, 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-pos.c
18 : * @brief implement GET /private/pos
19 : * @author Christian Grothoff
20 : */
21 : #include "platform.h"
22 : #include "taler-merchant-httpd_private-get-pos.h"
23 : #include <taler/taler_json_lib.h>
24 :
25 : /**
26 : * Closure for add_product().
27 : */
28 : struct Context
29 : {
30 : /**
31 : * JSON array of products we are building.
32 : */
33 : json_t *pa;
34 :
35 : /**
36 : * JSON array of categories we are building.
37 : */
38 : json_t *ca;
39 :
40 : };
41 :
42 :
43 : /**
44 : * Add category to the @e ca array.
45 : *
46 : * @param cls a `struct Context` with JSON arrays to build
47 : * @param category_id ID of the category
48 : * @param category_name name of the category
49 : * @param category_name_i18n translations of the @a category_name
50 : * @param product_count number of products in the category
51 : */
52 : static void
53 0 : add_category (
54 : void *cls,
55 : uint64_t category_id,
56 : const char *category_name,
57 : const json_t *category_name_i18n,
58 : uint64_t product_count)
59 : {
60 0 : struct Context *ctx = cls;
61 :
62 : (void) product_count;
63 0 : GNUNET_assert (
64 : 0 ==
65 : json_array_append_new (
66 : ctx->ca,
67 : GNUNET_JSON_PACK (
68 : GNUNET_JSON_pack_uint64 ("id",
69 : category_id),
70 : GNUNET_JSON_pack_object_incref ("name_i18n",
71 : (json_t *) category_name_i18n),
72 : GNUNET_JSON_pack_string ("name",
73 : category_name))));
74 0 : }
75 :
76 :
77 : /**
78 : * Add product details to our JSON array.
79 : *
80 : * @param cls a `struct Context` with JSON arrays to build
81 : * @param product_serial row ID of the product
82 : * @param product_id ID of the product
83 : * @param pd full product details
84 : * @param num_categories length of @a categories array
85 : * @param categories array of categories the
86 : * product is in
87 : */
88 : static void
89 0 : add_product (void *cls,
90 : uint64_t product_serial,
91 : const char *product_id,
92 : const struct TALER_MERCHANTDB_ProductDetails *pd,
93 : size_t num_categories,
94 : const uint64_t *categories)
95 : {
96 0 : struct Context *ctx = cls;
97 0 : json_t *pa = ctx->pa;
98 : json_t *cata;
99 :
100 0 : cata = json_array ();
101 0 : GNUNET_assert (NULL != cata);
102 0 : for (size_t i = 0; i<num_categories; i++)
103 0 : GNUNET_assert (
104 : 0 == json_array_append_new (
105 : cata,
106 : json_integer (categories[i])));
107 0 : if (0 == num_categories)
108 : {
109 : // If there is no category, we return the default category
110 0 : GNUNET_assert (
111 : 0 == json_array_append_new (
112 : cata,
113 : json_integer (0)));
114 : }
115 0 : GNUNET_assert (
116 : 0 ==
117 : json_array_append_new (
118 : pa,
119 : GNUNET_JSON_PACK (
120 : GNUNET_JSON_pack_string ("description",
121 : pd->description),
122 : GNUNET_JSON_pack_object_incref ("description_i18n",
123 : (json_t *) pd->description_i18n),
124 : GNUNET_JSON_pack_string ("unit",
125 : pd->unit),
126 : TALER_JSON_pack_amount ("price",
127 : &pd->price),
128 : GNUNET_JSON_pack_allow_null (
129 : GNUNET_JSON_pack_string ("image",
130 : pd->image)),
131 : GNUNET_JSON_pack_array_steal ("categories",
132 : cata),
133 : GNUNET_JSON_pack_allow_null (
134 : GNUNET_JSON_pack_array_incref ("taxes",
135 : (json_t *) pd->taxes)),
136 : (INT64_MAX == pd->total_stock)
137 : ? GNUNET_JSON_pack_int64 ("total_stock",
138 : pd->total_stock)
139 : : GNUNET_JSON_pack_allow_null (
140 : GNUNET_JSON_pack_string ("total_stock",
141 : NULL)),
142 : GNUNET_JSON_pack_uint64 ("minimum_age",
143 : pd->minimum_age),
144 : GNUNET_JSON_pack_uint64 ("product_serial",
145 : product_serial),
146 : GNUNET_JSON_pack_string ("product_id",
147 : product_id))));
148 0 : }
149 :
150 :
151 : MHD_RESULT
152 0 : TMH_private_get_pos (const struct TMH_RequestHandler *rh,
153 : struct MHD_Connection *connection,
154 : struct TMH_HandlerContext *hc)
155 : {
156 : struct Context ctx;
157 : enum GNUNET_DB_QueryStatus qs;
158 :
159 0 : ctx.pa = json_array ();
160 0 : GNUNET_assert (NULL != ctx.pa);
161 0 : ctx.ca = json_array ();
162 0 : GNUNET_assert (NULL != ctx.ca);
163 0 : GNUNET_assert (
164 : 0 == json_array_append_new (
165 : ctx.ca,
166 : GNUNET_JSON_PACK (
167 : GNUNET_JSON_pack_uint64 ("id",
168 : 0),
169 : GNUNET_JSON_pack_string ("name",
170 : "default"))));
171 0 : qs = TMH_db->lookup_categories (TMH_db->cls,
172 0 : hc->instance->settings.id,
173 : &add_category,
174 : &ctx);
175 0 : if (0 > qs)
176 : {
177 0 : GNUNET_break (0);
178 0 : json_decref (ctx.pa);
179 0 : json_decref (ctx.ca);
180 0 : return TALER_MHD_reply_with_error (connection,
181 : MHD_HTTP_INTERNAL_SERVER_ERROR,
182 : TALER_EC_GENERIC_DB_FETCH_FAILED,
183 : NULL);
184 : }
185 0 : qs = TMH_db->lookup_all_products (TMH_db->cls,
186 0 : hc->instance->settings.id,
187 : &add_product,
188 : &ctx);
189 0 : if (0 > qs)
190 : {
191 0 : GNUNET_break (0);
192 0 : json_decref (ctx.pa);
193 0 : json_decref (ctx.ca);
194 0 : return TALER_MHD_reply_with_error (connection,
195 : MHD_HTTP_INTERNAL_SERVER_ERROR,
196 : TALER_EC_GENERIC_DB_FETCH_FAILED,
197 : NULL);
198 : }
199 0 : return TALER_MHD_REPLY_JSON_PACK (
200 : connection,
201 : MHD_HTTP_OK,
202 : GNUNET_JSON_pack_array_steal ("categories",
203 : ctx.ca),
204 : GNUNET_JSON_pack_array_steal ("products",
205 : ctx.pa));
206 : }
207 :
208 :
209 : /* end of taler-merchant-httpd_private-get-pos.c */
|