taler-merchant-httpd_patch-private-templates-TEMPLATE_ID.c (5279B)
1 /* 2 This file is part of TALER 3 (C) 2022, 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-templates-TEMPLATE_ID.c 22 * @brief implementing PATCH /templates/$ID request handling 23 * @author Priscilla HUANG 24 */ 25 #include "platform.h" 26 #include "taler-merchant-httpd_patch-private-templates-TEMPLATE_ID.h" 27 #include "taler-merchant-httpd_helper.h" 28 #include <taler/taler_json_lib.h> 29 #include "merchant-database/update_template.h" 30 31 32 /** 33 * PATCH configuration of an existing instance, given its configuration. 34 * 35 * @param rh context of the handler 36 * @param connection the MHD connection to handle 37 * @param[in,out] hc context with further information about the request 38 * @return MHD result code 39 */ 40 enum MHD_Result 41 TMH_private_patch_templates_ID (const struct TMH_RequestHandler *rh, 42 struct MHD_Connection *connection, 43 struct TMH_HandlerContext *hc) 44 { 45 struct TMH_MerchantInstance *mi = hc->instance; 46 const char *template_id = hc->infix; 47 struct TALER_MERCHANTDB_TemplateDetails tp = {0}; 48 enum GNUNET_DB_QueryStatus qs; 49 struct GNUNET_JSON_Specification spec[] = { 50 GNUNET_JSON_spec_string ("template_description", 51 (const char **) &tp.template_description), 52 GNUNET_JSON_spec_mark_optional ( 53 GNUNET_JSON_spec_string ("otp_id", 54 (const char **) &tp.otp_id), 55 NULL), 56 GNUNET_JSON_spec_json ("template_contract", 57 &tp.template_contract), 58 GNUNET_JSON_spec_mark_optional ( 59 GNUNET_JSON_spec_json ("editable_defaults", 60 &tp.editable_defaults), 61 NULL), 62 GNUNET_JSON_spec_end () 63 }; 64 65 GNUNET_assert (NULL != mi); 66 GNUNET_assert (NULL != template_id); 67 { 68 enum GNUNET_GenericReturnValue res; 69 70 res = TALER_MHD_parse_json_data (connection, 71 hc->request_body, 72 spec); 73 if (GNUNET_OK != res) 74 return (GNUNET_NO == res) 75 ? MHD_YES 76 : MHD_NO; 77 } 78 79 if (! TALER_MERCHANT_template_contract_valid (tp.template_contract)) 80 { 81 GNUNET_break_op (0); 82 GNUNET_JSON_parse_free (spec); 83 return TALER_MHD_reply_with_error (connection, 84 MHD_HTTP_BAD_REQUEST, 85 TALER_EC_GENERIC_PARAMETER_MALFORMED, 86 "template_contract"); 87 } 88 if (NULL != tp.editable_defaults) 89 { 90 const char *key; 91 json_t *val; 92 93 json_object_foreach (tp.editable_defaults, key, val) 94 { 95 if (NULL != 96 json_object_get (tp.template_contract, 97 key)) 98 { 99 char *msg; 100 enum MHD_Result ret; 101 102 GNUNET_break_op (0); 103 GNUNET_asprintf (&msg, 104 "editable_defaults::%s conflicts with template_contract", 105 key); 106 GNUNET_JSON_parse_free (spec); 107 ret = TALER_MHD_reply_with_error ( 108 connection, 109 MHD_HTTP_BAD_REQUEST, 110 TALER_EC_GENERIC_PARAMETER_MALFORMED, 111 msg); 112 GNUNET_free (msg); 113 return ret; 114 } 115 } 116 } 117 118 qs = TALER_MERCHANTDB_update_template (TMH_db, 119 mi->settings.id, 120 template_id, 121 &tp); 122 { 123 enum MHD_Result ret = MHD_NO; 124 125 switch (qs) 126 { 127 case GNUNET_DB_STATUS_HARD_ERROR: 128 GNUNET_break (0); 129 ret = TALER_MHD_reply_with_error ( 130 connection, 131 MHD_HTTP_INTERNAL_SERVER_ERROR, 132 TALER_EC_GENERIC_DB_STORE_FAILED, 133 NULL); 134 break; 135 case GNUNET_DB_STATUS_SOFT_ERROR: 136 GNUNET_break (0); 137 ret = TALER_MHD_reply_with_error ( 138 connection, 139 MHD_HTTP_INTERNAL_SERVER_ERROR, 140 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 141 "unexpected serialization problem"); 142 break; 143 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 144 ret = TALER_MHD_reply_with_error ( 145 connection, 146 MHD_HTTP_NOT_FOUND, 147 TALER_EC_MERCHANT_GENERIC_TEMPLATE_UNKNOWN, 148 template_id); 149 break; 150 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 151 ret = TALER_MHD_reply_static (connection, 152 MHD_HTTP_NO_CONTENT, 153 NULL, 154 NULL, 155 0); 156 break; 157 } 158 GNUNET_JSON_parse_free (spec); 159 return ret; 160 } 161 } 162 163 164 /* end of taler-merchant-httpd_patch-private-templates-TEMPLATE_ID.c */