Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2025 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 Affero General Public License for more
12 : details.
13 :
14 : You should have received a copy of the GNU Affero General Public License
15 : along with TALER; see the file COPYING. If not, see
16 : <http://www.gnu.org/licenses/>
17 : */
18 : /**
19 : * @file merchant/backend/taler-merchant-httpd_private-get-groups.c
20 : * @brief implementation of GET /private/groups
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include "taler-merchant-httpd_private-get-groups.h"
25 : #include <taler/taler_json_lib.h>
26 :
27 : /**
28 : * Sensible bound on the number of results to return
29 : */
30 : #define MAX_DELTA 1024
31 :
32 :
33 : /**
34 : * Callback for listing product groups.
35 : *
36 : * @param cls closure with a `json_t *`
37 : * @param product_group_id unique identifier of the group
38 : * @param group_name name of the group
39 : * @param group_description human-readable description
40 : */
41 : static void
42 0 : add_group (void *cls,
43 : uint64_t product_group_id,
44 : const char *group_name,
45 : const char *group_description)
46 : {
47 0 : json_t *groups = cls;
48 : json_t *entry;
49 :
50 0 : entry = GNUNET_JSON_PACK (
51 : GNUNET_JSON_pack_uint64 ("group_serial",
52 : product_group_id),
53 : GNUNET_JSON_pack_string ("group_name",
54 : group_name),
55 : GNUNET_JSON_pack_string ("description",
56 : group_description));
57 0 : GNUNET_assert (NULL != entry);
58 0 : GNUNET_assert (0 ==
59 : json_array_append_new (groups,
60 : entry));
61 0 : }
62 :
63 :
64 : MHD_RESULT
65 0 : TMH_private_get_groups (const struct TMH_RequestHandler *rh,
66 : struct MHD_Connection *connection,
67 : struct TMH_HandlerContext *hc)
68 : {
69 0 : int64_t limit = -20;
70 : uint64_t offset;
71 : json_t *groups;
72 :
73 : (void) rh;
74 0 : TALER_MHD_parse_request_snumber (connection,
75 : "limit",
76 : &limit);
77 0 : if ( (-MAX_DELTA > limit) ||
78 0 : (limit > MAX_DELTA) )
79 : {
80 0 : GNUNET_break_op (0);
81 0 : return TALER_MHD_reply_with_error (connection,
82 : MHD_HTTP_BAD_REQUEST,
83 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
84 : "limit");
85 : }
86 0 : if (limit > 0)
87 0 : offset = 0;
88 : else
89 0 : offset = INT64_MAX;
90 0 : TALER_MHD_parse_request_number (connection,
91 : "offset",
92 : &offset);
93 :
94 0 : groups = json_array ();
95 0 : GNUNET_assert (NULL != groups);
96 :
97 : {
98 : enum GNUNET_DB_QueryStatus qs;
99 :
100 0 : qs = TMH_db->select_product_groups (TMH_db->cls,
101 0 : hc->instance->settings.id,
102 : limit,
103 : offset,
104 : &add_group,
105 : groups);
106 0 : if (qs < 0)
107 : {
108 0 : GNUNET_break (0);
109 0 : json_decref (groups);
110 0 : return TALER_MHD_reply_with_error (connection,
111 : MHD_HTTP_INTERNAL_SERVER_ERROR,
112 : TALER_EC_GENERIC_DB_FETCH_FAILED,
113 : "select_product_groups");
114 : }
115 : }
116 :
117 0 : return TALER_MHD_REPLY_JSON_PACK (
118 : connection,
119 : MHD_HTTP_OK,
120 : GNUNET_JSON_pack_array_steal ("groups",
121 : groups));
122 : }
|