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