Line data Source code
1 : /*
2 : This file is part of TALER
3 : (C) 2024 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify
6 : it under the terms of the GNU Affero General Public License as
7 : published by the Free Software Foundation; either version 3,
8 : or (at your option) any later version.
9 :
10 : TALER is distributed in the hope that it will be useful, but
11 : WITHOUT ANY WARRANTY; without even the implied warranty of
12 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 : GNU General Public License for more details.
14 :
15 : You should have received a copy of the GNU General Public
16 : License along with TALER; see the file COPYING. If not,
17 : see <http://www.gnu.org/licenses/>
18 : */
19 : /**
20 : * @file src/backend/taler-merchant-httpd_post-private-categories.c
21 : * @brief implementing POST /private/categories request handling
22 : * @author Christian Grothoff
23 : */
24 : #include "platform.h"
25 : #include "taler-merchant-httpd_post-private-categories.h"
26 : #include "taler-merchant-httpd_helper.h"
27 : #include <taler/taler_json_lib.h>
28 : #include "merchant-database/insert_category.h"
29 : #include "merchant-database/get_category_by_name.h"
30 : #include "merchant-database/start.h"
31 :
32 : /**
33 : * How often do we retry the simple INSERT database transaction?
34 : */
35 : #define MAX_RETRIES 3
36 :
37 :
38 : enum MHD_Result
39 6 : TMH_private_post_categories (const struct TMH_RequestHandler *rh,
40 : struct MHD_Connection *connection,
41 : struct TMH_HandlerContext *hc)
42 : {
43 6 : struct TMH_MerchantInstance *mi = hc->instance;
44 : const char *category_name;
45 6 : const json_t *category_name_i18n = NULL;
46 6 : json_t *oempty = NULL;
47 : uint64_t category_id;
48 : struct GNUNET_JSON_Specification spec[] = {
49 6 : GNUNET_JSON_spec_string ("name",
50 : &category_name),
51 6 : GNUNET_JSON_spec_mark_optional (
52 : GNUNET_JSON_spec_object_const ("name_i18n",
53 : &category_name_i18n),
54 : NULL),
55 6 : GNUNET_JSON_spec_end ()
56 : };
57 : enum GNUNET_DB_QueryStatus qs;
58 :
59 6 : GNUNET_assert (NULL != mi);
60 : {
61 : enum GNUNET_GenericReturnValue res;
62 :
63 6 : res = TALER_MHD_parse_json_data (connection,
64 6 : hc->request_body,
65 : spec);
66 6 : if (GNUNET_OK != res)
67 : {
68 0 : GNUNET_break_op (0);
69 : return (GNUNET_NO == res)
70 : ? MHD_YES
71 0 : : MHD_NO;
72 : }
73 : }
74 6 : if (NULL == category_name_i18n)
75 : {
76 : /* NULL is not allowed in the DB, substitute with empty object */
77 0 : oempty = json_object ();
78 0 : category_name_i18n = oempty;
79 : }
80 : /* finally, interact with DB until no serialization error */
81 6 : for (unsigned int i = 0; i<MAX_RETRIES; i++)
82 : {
83 : json_t *xcategory_name_i18n;
84 :
85 6 : if (GNUNET_OK !=
86 6 : TALER_MERCHANTDB_start (TMH_db,
87 : "POST /categories"))
88 : {
89 0 : GNUNET_break (0);
90 0 : GNUNET_JSON_parse_free (spec);
91 0 : json_decref (oempty);
92 0 : return TALER_MHD_reply_with_error (connection,
93 : MHD_HTTP_INTERNAL_SERVER_ERROR,
94 : TALER_EC_GENERIC_DB_START_FAILED,
95 : NULL);
96 : }
97 6 : qs = TALER_MERCHANTDB_get_category_by_name (TMH_db,
98 6 : mi->settings.id,
99 : category_name,
100 : &xcategory_name_i18n,
101 : &category_id);
102 6 : switch (qs)
103 : {
104 0 : case GNUNET_DB_STATUS_HARD_ERROR:
105 : /* Clean up and fail hard */
106 0 : GNUNET_break (0);
107 0 : TALER_MERCHANTDB_rollback (TMH_db);
108 0 : GNUNET_JSON_parse_free (spec);
109 0 : json_decref (oempty);
110 0 : return TALER_MHD_reply_with_error (connection,
111 : MHD_HTTP_INTERNAL_SERVER_ERROR,
112 : TALER_EC_GENERIC_DB_FETCH_FAILED,
113 : NULL);
114 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
115 : /* restart transaction */
116 0 : goto retry;
117 6 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
118 : /* Good, we can proceed! */
119 6 : break;
120 0 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
121 : /* idempotency check: is etp == tp? */
122 : {
123 : bool eq;
124 :
125 0 : eq = (1 == json_equal (xcategory_name_i18n,
126 : category_name_i18n));
127 0 : json_decref (xcategory_name_i18n);
128 0 : TALER_MERCHANTDB_rollback (TMH_db);
129 0 : GNUNET_JSON_parse_free (spec);
130 0 : json_decref (oempty);
131 : return eq
132 0 : ? TALER_MHD_REPLY_JSON_PACK (connection,
133 : MHD_HTTP_OK,
134 : GNUNET_JSON_pack_uint64 ("category_id",
135 : category_id))
136 0 : : TALER_MHD_reply_with_error (connection,
137 : MHD_HTTP_CONFLICT,
138 : TALER_EC_MERCHANT_PRIVATE_POST_CATEGORIES_CONFLICT_CATEGORY_EXISTS,
139 : category_name);
140 : }
141 : } /* end switch (qs) */
142 :
143 6 : qs = TALER_MERCHANTDB_insert_category (TMH_db,
144 6 : mi->settings.id,
145 : category_name,
146 : category_name_i18n,
147 : &category_id);
148 6 : if (GNUNET_DB_STATUS_HARD_ERROR == qs)
149 : {
150 0 : TALER_MERCHANTDB_rollback (TMH_db);
151 6 : break;
152 : }
153 6 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
154 : {
155 : /* A concurrent request created a category with the same name
156 : between our lookup above and this INSERT. */
157 0 : TALER_MERCHANTDB_rollback (TMH_db);
158 0 : GNUNET_JSON_parse_free (spec);
159 0 : json_decref (oempty);
160 0 : return TALER_MHD_reply_with_error (
161 : connection,
162 : MHD_HTTP_CONFLICT,
163 : TALER_EC_MERCHANT_PRIVATE_POST_CATEGORIES_CONFLICT_CATEGORY_EXISTS,
164 : category_name);
165 : }
166 6 : if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
167 : {
168 6 : qs = TALER_MERCHANTDB_commit (TMH_db);
169 6 : if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
170 6 : break;
171 : }
172 0 : retry:
173 0 : GNUNET_assert (GNUNET_DB_STATUS_SOFT_ERROR == qs);
174 0 : TALER_MERCHANTDB_rollback (TMH_db);
175 : } /* for RETRIES loop */
176 6 : json_decref (oempty);
177 6 : GNUNET_JSON_parse_free (spec);
178 6 : if (qs < 0)
179 : {
180 0 : GNUNET_break (0);
181 0 : return TALER_MHD_reply_with_error (
182 : connection,
183 : MHD_HTTP_INTERNAL_SERVER_ERROR,
184 : (GNUNET_DB_STATUS_SOFT_ERROR == qs)
185 : ? TALER_EC_GENERIC_DB_SOFT_FAILURE
186 : : TALER_EC_GENERIC_DB_COMMIT_FAILED,
187 : NULL);
188 : }
189 6 : return TALER_MHD_REPLY_JSON_PACK (
190 : connection,
191 : MHD_HTTP_OK,
192 : GNUNET_JSON_pack_uint64 ("category_id",
193 : category_id));
194 : }
195 :
196 :
197 : /* end of taler-merchant-httpd_post-private-categories.c */
|