Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2020-2021 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU 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-tips.c
18 : * @brief implementation of a GET /private/tips handler
19 : * @author Jonathan Buchanan
20 : */
21 : #include "platform.h"
22 : #include "taler-merchant-httpd_private-get-tips.h"
23 : #include <taler/taler_json_lib.h>
24 :
25 : /**
26 : * Add tip details to our JSON array.
27 : *
28 : * @param[in,out] cls a `json_t *` JSON array to build
29 : * @param row_id row number of the tip
30 : * @param tip_id ID of the tip
31 : * @param amount the amount of the tip
32 : */
33 : static void
34 0 : add_tip (void *cls,
35 : uint64_t row_id,
36 : struct TALER_TipIdentifierP tip_id,
37 : struct TALER_Amount amount)
38 : {
39 0 : json_t *pa = cls;
40 :
41 0 : GNUNET_assert (0 ==
42 : json_array_append_new (
43 : pa,
44 : GNUNET_JSON_PACK (
45 : GNUNET_JSON_pack_uint64 ("row_id",
46 : row_id),
47 : GNUNET_JSON_pack_data_auto ("tip_id",
48 : &tip_id),
49 : TALER_JSON_pack_amount ("tip_amount",
50 : &amount))));
51 0 : }
52 :
53 :
54 : MHD_RESULT
55 0 : TMH_private_get_tips (const struct TMH_RequestHandler *rh,
56 : struct MHD_Connection *connection,
57 : struct TMH_HandlerContext *hc)
58 : {
59 : json_t *pa;
60 : enum GNUNET_DB_QueryStatus qs;
61 : enum TALER_EXCHANGE_YesNoAll expired;
62 : uint64_t offset;
63 : int64_t limit;
64 :
65 : (void) rh;
66 0 : if (! (TALER_arg_to_yna (connection,
67 : "expired",
68 : TALER_EXCHANGE_YNA_NO,
69 : &expired)) )
70 0 : return TALER_MHD_reply_with_error (connection,
71 : MHD_HTTP_BAD_REQUEST,
72 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
73 : "expired");
74 : {
75 : const char *limit_str;
76 :
77 0 : limit_str = MHD_lookup_connection_value (connection,
78 : MHD_GET_ARGUMENT_KIND,
79 : "limit");
80 0 : if (NULL == limit_str)
81 : {
82 0 : limit = -20;
83 : }
84 : else
85 : {
86 : char dummy[2];
87 : long long ll;
88 :
89 0 : if (1 !=
90 0 : sscanf (limit_str,
91 : "%lld%1s",
92 : &ll,
93 : dummy))
94 0 : return TALER_MHD_reply_with_error (connection,
95 : MHD_HTTP_BAD_REQUEST,
96 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
97 : "limit");
98 0 : limit = (uint64_t) ll;
99 : }
100 : }
101 : {
102 : const char *offset_str;
103 :
104 0 : offset_str = MHD_lookup_connection_value (connection,
105 : MHD_GET_ARGUMENT_KIND,
106 : "offset");
107 0 : if (NULL == offset_str)
108 : {
109 0 : if (limit > 0)
110 0 : offset = 0;
111 : else
112 0 : offset = INT64_MAX;
113 : }
114 : else
115 : {
116 : char dummy[2];
117 : unsigned long long ull;
118 :
119 0 : if (1 !=
120 0 : sscanf (offset_str,
121 : "%llu%1s",
122 : &ull,
123 : dummy))
124 0 : return TALER_MHD_reply_with_error (connection,
125 : MHD_HTTP_BAD_REQUEST,
126 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
127 : "offset");
128 0 : offset = (uint64_t) ull;
129 : }
130 : }
131 :
132 0 : pa = json_array ();
133 0 : GNUNET_assert (NULL != pa);
134 0 : qs = TMH_db->lookup_tips (TMH_db->cls,
135 0 : hc->instance->settings.id,
136 : expired,
137 : limit,
138 : offset,
139 : &add_tip,
140 : pa);
141 :
142 0 : if (0 > qs)
143 : {
144 0 : GNUNET_break (0);
145 0 : json_decref (pa);
146 0 : return TALER_MHD_reply_with_error (connection,
147 : MHD_HTTP_INTERNAL_SERVER_ERROR,
148 : TALER_EC_GENERIC_DB_FETCH_FAILED,
149 : "tips");
150 : }
151 :
152 0 : return TALER_MHD_REPLY_JSON_PACK (
153 : connection,
154 : MHD_HTTP_OK,
155 : GNUNET_JSON_pack_array_steal ("tips",
156 : pa));
157 : }
|