Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2023, 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-token-families-SLUG.c
18 : * @brief implement GET /tokenfamilies/$SLUG/
19 : * @author Christian Blättler
20 : */
21 : #include "platform.h"
22 : #include "taler-merchant-httpd_private-get-token-families-SLUG.h"
23 : #include <gnunet/gnunet_json_lib.h>
24 : #include <taler/taler_json_lib.h>
25 :
26 :
27 : /**
28 : * Handle a GET "/tokenfamilies/$SLUG" request.
29 : *
30 : * @param rh context of the handler
31 : * @param connection the MHD connection to handle
32 : * @param[in,out] hc context with further information about the request
33 : * @return MHD result code
34 : */
35 : MHD_RESULT
36 0 : TMH_private_get_tokenfamilies_SLUG (const struct TMH_RequestHandler *rh,
37 : struct MHD_Connection *connection,
38 : struct TMH_HandlerContext *hc)
39 : {
40 0 : struct TMH_MerchantInstance *mi = hc->instance;
41 0 : struct TALER_MERCHANTDB_TokenFamilyDetails details = { 0 };
42 : enum GNUNET_DB_QueryStatus status;
43 :
44 0 : GNUNET_assert (NULL != mi);
45 0 : status = TMH_db->lookup_token_family (TMH_db->cls,
46 0 : mi->settings.id,
47 0 : hc->infix,
48 : &details);
49 0 : if (0 > status)
50 : {
51 0 : GNUNET_break (0);
52 0 : return TALER_MHD_reply_with_error (connection,
53 : MHD_HTTP_INTERNAL_SERVER_ERROR,
54 : TALER_EC_GENERIC_DB_FETCH_FAILED,
55 : "lookup_token_family");
56 : }
57 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == status)
58 : {
59 0 : return TALER_MHD_reply_with_error (connection,
60 : MHD_HTTP_NOT_FOUND,
61 : TALER_EC_MERCHANT_GENERIC_TOKEN_FAMILY_UNKNOWN,
62 0 : hc->infix);
63 : }
64 : {
65 0 : char *kind = NULL;
66 : MHD_RESULT result;
67 :
68 0 : if (TALER_MERCHANTDB_TFK_Subscription == details.kind)
69 : {
70 0 : kind = GNUNET_strdup ("subscription");
71 : }
72 0 : else if (TALER_MERCHANTDB_TFK_Discount == details.kind)
73 : {
74 0 : kind = GNUNET_strdup ("discount");
75 : }
76 : else
77 : {
78 0 : GNUNET_break (0);
79 0 : return TALER_MHD_reply_with_error (connection,
80 : MHD_HTTP_INTERNAL_SERVER_ERROR,
81 : TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
82 : "invalid_token_family_kind");
83 : }
84 :
85 0 : result = TALER_MHD_REPLY_JSON_PACK (
86 : connection,
87 : MHD_HTTP_OK,
88 : GNUNET_JSON_pack_string ("slug",
89 : details.slug),
90 : GNUNET_JSON_pack_string ("name",
91 : details.name),
92 : GNUNET_JSON_pack_string ("description",
93 : details.description),
94 : GNUNET_JSON_pack_object_steal ("description_i18n",
95 : details.description_i18n),
96 : GNUNET_JSON_pack_allow_null (
97 : GNUNET_JSON_pack_object_steal ("extra_data",
98 : details.extra_data)),
99 : GNUNET_JSON_pack_timestamp ("valid_after",
100 : details.valid_after),
101 : GNUNET_JSON_pack_timestamp ("valid_before",
102 : details.valid_before),
103 : GNUNET_JSON_pack_time_rel ("duration",
104 : details.duration),
105 : GNUNET_JSON_pack_time_rel ("validity_granularity",
106 : details.validity_granularity),
107 : GNUNET_JSON_pack_time_rel ("start_offset",
108 : details.start_offset),
109 : GNUNET_JSON_pack_string ("kind",
110 : kind),
111 : GNUNET_JSON_pack_int64 ("issued",
112 : details.issued),
113 : GNUNET_JSON_pack_int64 ("used",
114 : details.used)
115 : );
116 :
117 0 : GNUNET_free (details.name);
118 0 : GNUNET_free (details.description);
119 0 : GNUNET_free (details.cipher_spec);
120 0 : GNUNET_free (kind);
121 0 : return result;
122 : }
123 : }
124 :
125 :
126 : /* end of taler-merchant-httpd_private-get-token-families-SLUG.c */
|