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