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