taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.c (5804B)
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 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-tokenfamilies-TOKEN_FAMILY_SLUG.c 22 * @brief implementing PATCH /tokenfamilies/$SLUG request handling 23 * @author Christian Blättler 24 */ 25 #include "platform.h" 26 #include "taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.h" 27 #include "taler-merchant-httpd_helper.h" 28 #include <taler/taler_json_lib.h> 29 #include "merchant-database/update_token_family.h" 30 31 32 /** 33 * How often do we retry the simple INSERT database transaction? 34 */ 35 #define MAX_RETRIES 3 36 37 38 /** 39 * Handle a PATCH "/tokenfamilies/$slug" request. 40 * 41 * @param rh context of the handler 42 * @param connection the MHD connection to handle 43 * @param[in,out] hc context with further information about the request 44 * @return MHD result code 45 */ 46 enum MHD_Result 47 TMH_private_patch_token_family_SLUG (const struct TMH_RequestHandler *rh, 48 struct MHD_Connection *connection, 49 struct TMH_HandlerContext *hc) 50 { 51 struct TMH_MerchantInstance *mi = hc->instance; 52 const char *slug = hc->infix; 53 struct TALER_MERCHANTDB_TokenFamilyDetails details = {0}; 54 struct GNUNET_JSON_Specification spec[] = { 55 GNUNET_JSON_spec_string ("name", 56 (const char **) &details.name), 57 GNUNET_JSON_spec_string ("description", 58 (const char **) &details.description), 59 GNUNET_JSON_spec_mark_optional ( 60 GNUNET_JSON_spec_json ("description_i18n", 61 &details.description_i18n), 62 NULL), 63 GNUNET_JSON_spec_mark_optional ( 64 GNUNET_JSON_spec_json ("extra_data", 65 &details.extra_data), 66 NULL), 67 GNUNET_JSON_spec_timestamp ("valid_after", 68 &details.valid_after), 69 GNUNET_JSON_spec_timestamp ("valid_before", 70 &details.valid_before), 71 GNUNET_JSON_spec_end () 72 }; 73 74 GNUNET_assert (NULL != mi); 75 GNUNET_assert (NULL != slug); 76 { 77 enum GNUNET_GenericReturnValue res; 78 79 res = TALER_MHD_parse_json_data (connection, 80 hc->request_body, 81 spec); 82 if (GNUNET_OK != res) 83 return (GNUNET_NO == res) 84 ? MHD_YES 85 : MHD_NO; 86 } 87 88 /* Ensure that valid_after is before valid_before */ 89 if (GNUNET_TIME_timestamp_cmp (details.valid_after, 90 >=, 91 details.valid_before)) 92 { 93 GNUNET_break_op (0); 94 GNUNET_JSON_parse_free (spec); 95 return TALER_MHD_reply_with_error (connection, 96 MHD_HTTP_BAD_REQUEST, 97 TALER_EC_GENERIC_PARAMETER_MALFORMED, 98 "valid_after >= valid_before"); 99 } 100 101 if (NULL == details.description_i18n) 102 { 103 details.description_i18n = json_object (); 104 GNUNET_assert (NULL != details.description_i18n); 105 } 106 if (! TALER_JSON_check_i18n (details.description_i18n)) 107 { 108 GNUNET_break_op (0); 109 GNUNET_JSON_parse_free (spec); 110 return TALER_MHD_reply_with_error (connection, 111 MHD_HTTP_BAD_REQUEST, 112 TALER_EC_GENERIC_PARAMETER_MALFORMED, 113 "description_i18n"); 114 } 115 116 { 117 enum GNUNET_DB_QueryStatus qs; 118 enum MHD_Result ret = MHD_NO; 119 120 qs = TALER_MERCHANTDB_update_token_family (TMH_db, 121 mi->settings.id, 122 slug, 123 &details); 124 switch (qs) 125 { 126 case GNUNET_DB_STATUS_HARD_ERROR: 127 GNUNET_break (0); 128 ret = TALER_MHD_reply_with_error (connection, 129 MHD_HTTP_INTERNAL_SERVER_ERROR, 130 TALER_EC_GENERIC_DB_STORE_FAILED, 131 NULL); 132 break; 133 case GNUNET_DB_STATUS_SOFT_ERROR: 134 GNUNET_break (0); 135 ret = TALER_MHD_reply_with_error (connection, 136 MHD_HTTP_INTERNAL_SERVER_ERROR, 137 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 138 "unexpected serialization problem"); 139 break; 140 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 141 ret = TALER_MHD_reply_with_error (connection, 142 MHD_HTTP_NOT_FOUND, 143 TALER_EC_MERCHANT_PATCH_TOKEN_FAMILY_NOT_FOUND, 144 slug); 145 break; 146 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 147 ret = TALER_MHD_reply_static (connection, 148 MHD_HTTP_NO_CONTENT, 149 NULL, 150 NULL, 151 0); 152 break; 153 } 154 GNUNET_JSON_parse_free (spec); 155 return ret; 156 } 157 } 158 159 160 /* end of taler-merchant-httpd_patch-private-tokenfamilies-TOKEN_FAMILY_SLUG.c */