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 ("product_name",
121 : pd->product_name),
122 : GNUNET_JSON_pack_string ("description",
123 : pd->description),
124 : GNUNET_JSON_pack_object_incref ("description_i18n",
125 : (json_t *) pd->description_i18n),
126 : GNUNET_JSON_pack_string ("unit",
127 : pd->unit),
128 : TALER_JSON_pack_amount ("price",
129 : &pd->price),
130 : GNUNET_JSON_pack_allow_null (
131 : GNUNET_JSON_pack_string ("image",
132 : pd->image)),
133 : GNUNET_JSON_pack_array_steal ("categories",
134 : cata),
135 : GNUNET_JSON_pack_allow_null (
136 : GNUNET_JSON_pack_array_incref ("taxes",
137 : (json_t *) pd->taxes)),
138 : (INT64_MAX == pd->total_stock)
139 : ? GNUNET_JSON_pack_int64 ("total_stock",
140 : pd->total_stock)
141 : : GNUNET_JSON_pack_allow_null (
142 : GNUNET_JSON_pack_string ("total_stock",
143 : NULL)),
144 : GNUNET_JSON_pack_uint64 ("minimum_age",
145 : pd->minimum_age),
146 : GNUNET_JSON_pack_uint64 ("product_serial",
147 : product_serial),
148 : GNUNET_JSON_pack_string ("product_id",
149 : product_id))));
150 0 : }
151 :
152 :
153 : MHD_RESULT
154 0 : TMH_private_get_pos (const struct TMH_RequestHandler *rh,
155 : struct MHD_Connection *connection,
156 : struct TMH_HandlerContext *hc)
157 : {
158 : struct Context ctx;
159 : enum GNUNET_DB_QueryStatus qs;
160 :
161 0 : ctx.pa = json_array ();
162 0 : GNUNET_assert (NULL != ctx.pa);
163 0 : ctx.ca = json_array ();
164 0 : GNUNET_assert (NULL != ctx.ca);
165 0 : GNUNET_assert (
166 : 0 == json_array_append_new (
167 : ctx.ca,
168 : GNUNET_JSON_PACK (
169 : GNUNET_JSON_pack_uint64 ("id",
170 : 0),
171 : GNUNET_JSON_pack_string ("name",
172 : "default"))));
173 0 : qs = TMH_db->lookup_categories (TMH_db->cls,
174 0 : hc->instance->settings.id,
175 : &add_category,
176 : &ctx);
177 0 : if (0 > qs)
178 : {
179 0 : GNUNET_break (0);
180 0 : json_decref (ctx.pa);
181 0 : json_decref (ctx.ca);
182 0 : return TALER_MHD_reply_with_error (connection,
183 : MHD_HTTP_INTERNAL_SERVER_ERROR,
184 : TALER_EC_GENERIC_DB_FETCH_FAILED,
185 : NULL);
186 : }
187 0 : qs = TMH_db->lookup_all_products (TMH_db->cls,
188 0 : hc->instance->settings.id,
189 : &add_product,
190 : &ctx);
191 0 : if (0 > qs)
192 : {
193 0 : GNUNET_break (0);
194 0 : json_decref (ctx.pa);
195 0 : json_decref (ctx.ca);
196 0 : return TALER_MHD_reply_with_error (connection,
197 : MHD_HTTP_INTERNAL_SERVER_ERROR,
198 : TALER_EC_GENERIC_DB_FETCH_FAILED,
199 : NULL);
200 : }
201 0 : return TALER_MHD_REPLY_JSON_PACK (
202 : connection,
203 : MHD_HTTP_OK,
204 : GNUNET_JSON_pack_array_steal ("categories",
205 : ctx.ca),
206 : GNUNET_JSON_pack_array_steal ("products",
207 : ctx.pa));
208 : }
209 :
210 :
211 : /* end of taler-merchant-httpd_private-get-pos.c */
|