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 : /**
21 : * @file src/backend/taler-merchant-httpd_patch-private-categories-CATEGORY_ID.c
22 : * @brief implementing PATCH /categories/$ID request handling
23 : * @author Christian Grothoff
24 : */
25 : #include "platform.h"
26 : #include "taler-merchant-httpd_patch-private-categories-CATEGORY_ID.h"
27 : #include "taler-merchant-httpd_helper.h"
28 : #include <taler/taler_json_lib.h>
29 : #include "merchant-database/update_category.h"
30 :
31 :
32 : enum MHD_Result
33 0 : TMH_private_patch_categories_ID (const struct TMH_RequestHandler *rh,
34 : struct MHD_Connection *connection,
35 : struct TMH_HandlerContext *hc)
36 : {
37 0 : struct TMH_MerchantInstance *mi = hc->instance;
38 : unsigned long long cnum;
39 : char dummy;
40 : const char *category_name;
41 0 : json_t *oempty = NULL;
42 0 : const json_t *category_name_i18n = NULL;
43 : struct GNUNET_JSON_Specification spec[] = {
44 0 : GNUNET_JSON_spec_string ("name",
45 : &category_name),
46 0 : GNUNET_JSON_spec_mark_optional (
47 : GNUNET_JSON_spec_object_const ("name_i18n",
48 : &category_name_i18n),
49 : NULL),
50 0 : GNUNET_JSON_spec_end ()
51 : };
52 : enum GNUNET_DB_QueryStatus qs;
53 0 : bool conflict = false;
54 :
55 0 : GNUNET_assert (NULL != mi);
56 0 : GNUNET_assert (NULL != hc->infix);
57 0 : if (1 != sscanf (hc->infix,
58 : "%llu%c",
59 : &cnum,
60 : &dummy))
61 : {
62 0 : GNUNET_break_op (0);
63 0 : return TALER_MHD_reply_with_error (connection,
64 : MHD_HTTP_BAD_REQUEST,
65 : TALER_EC_GENERIC_PARAMETER_MALFORMED,
66 : "category_id must be a number");
67 : }
68 :
69 : {
70 : enum GNUNET_GenericReturnValue res;
71 :
72 0 : res = TALER_MHD_parse_json_data (connection,
73 0 : hc->request_body,
74 : spec);
75 0 : if (GNUNET_OK != res)
76 : return (GNUNET_NO == res)
77 : ? MHD_YES
78 0 : : MHD_NO;
79 : }
80 0 : if (NULL == category_name_i18n)
81 : {
82 : /* NULL is not allowed in the DB, substitute with empty object */
83 0 : oempty = json_object ();
84 0 : category_name_i18n = oempty;
85 : }
86 :
87 0 : qs = TALER_MERCHANTDB_update_category (TMH_db,
88 0 : mi->settings.id,
89 : cnum,
90 : category_name,
91 : category_name_i18n,
92 : &conflict);
93 0 : json_decref (oempty);
94 0 : if (conflict)
95 : {
96 : enum MHD_Result ret;
97 :
98 0 : ret = TALER_MHD_reply_with_error (
99 : connection,
100 : MHD_HTTP_CONFLICT,
101 : TALER_EC_MERCHANT_PRIVATE_POST_CATEGORIES_CONFLICT_CATEGORY_EXISTS,
102 : category_name);
103 0 : GNUNET_JSON_parse_free (spec);
104 0 : return ret;
105 : }
106 : {
107 0 : enum MHD_Result ret = MHD_NO;
108 :
109 0 : switch (qs)
110 : {
111 0 : case GNUNET_DB_STATUS_HARD_ERROR:
112 0 : GNUNET_break (0);
113 0 : ret = TALER_MHD_reply_with_error (connection,
114 : MHD_HTTP_INTERNAL_SERVER_ERROR,
115 : TALER_EC_GENERIC_DB_STORE_FAILED,
116 : "update_category");
117 0 : break;
118 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
119 0 : GNUNET_break (0);
120 0 : ret = TALER_MHD_reply_with_error (connection,
121 : MHD_HTTP_INTERNAL_SERVER_ERROR,
122 : TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
123 : "unexpected serialization problem");
124 0 : break;
125 0 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
126 0 : ret = TALER_MHD_reply_with_error (connection,
127 : MHD_HTTP_NOT_FOUND,
128 : TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN,
129 : category_name);
130 0 : break;
131 0 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
132 0 : ret = TALER_MHD_reply_static (connection,
133 : MHD_HTTP_NO_CONTENT,
134 : NULL,
135 : NULL,
136 : 0);
137 0 : break;
138 : }
139 0 : GNUNET_JSON_parse_free (spec);
140 0 : return ret;
141 : }
142 : }
143 :
144 :
145 : /* end of taler-merchant-httpd_patch-private-categories-CATEGORY_ID.c */
|