donau-httpd_patch-charities-CHARITY_ID.c (6035B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2025 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file donau-httpd_patch-charities-CHARITY_ID.c 18 * @brief Handle request to update a charity entry. 19 * @author Bohdan Potuzhnyi 20 */ 21 #include <donau_config.h> 22 #include <gnunet/gnunet_json_lib.h> 23 #include <gnunet/gnunet_util_lib.h> 24 #include <jansson.h> 25 #include <microhttpd.h> 26 #include <taler/taler_json_lib.h> 27 #include <taler/taler_mhd_lib.h> 28 #include <taler/taler_util.h> 29 #include "donau-httpd_patch-charities-CHARITY_ID.h" 30 #include "donau-httpd_db.h" 31 #include "donau-database/lookup_charity.h" 32 #include "donau-database/update_charity.h" 33 34 35 enum MHD_Result 36 DH_handler_patch_charities (struct DH_RequestContext *rc, 37 const json_t *root, 38 const char *const args[]) 39 { 40 struct DONAU_CharityPublicKeyP charity_pub; 41 unsigned long long charity_id; 42 char dummy; 43 const char *charity_name = NULL; 44 const char *charity_url = NULL; 45 struct TALER_Amount max_per_year; 46 struct GNUNET_JSON_Specification spec[] = { 47 GNUNET_JSON_spec_fixed_auto ("charity_pub", 48 &charity_pub), 49 GNUNET_JSON_spec_string ("charity_name", 50 &charity_name), 51 GNUNET_JSON_spec_string ("charity_url", 52 &charity_url), 53 TALER_JSON_spec_amount ("max_per_year", 54 DH_currency, 55 &max_per_year), 56 GNUNET_JSON_spec_end () 57 }; 58 59 if ( (NULL == args[0]) || 60 (1 != sscanf (args[0], 61 "%llu%c", 62 &charity_id, 63 &dummy)) ) 64 { 65 GNUNET_break_op (0); 66 return TALER_MHD_reply_with_error (rc->connection, 67 MHD_HTTP_BAD_REQUEST, 68 TALER_EC_GENERIC_PARAMETER_MALFORMED, 69 "charity_id"); 70 } 71 72 { 73 enum GNUNET_GenericReturnValue res; 74 75 res = TALER_MHD_parse_json_data (rc->connection, 76 root, 77 spec); 78 if (GNUNET_SYSERR == res) 79 return MHD_NO; /* hard failure */ 80 if (GNUNET_NO == res) 81 { 82 GNUNET_break_op (0); 83 return MHD_YES; /* failure */ 84 } 85 } 86 87 { 88 struct DONAUDB_CharityMetaData meta; 89 enum GNUNET_DB_QueryStatus qs; 90 91 qs = DONAUDB_lookup_charity (DH_context, 92 charity_id, 93 &meta); 94 switch (qs) 95 { 96 case GNUNET_DB_STATUS_HARD_ERROR: 97 case GNUNET_DB_STATUS_SOFT_ERROR: 98 GNUNET_break (0); 99 return TALER_MHD_reply_with_error (rc->connection, 100 MHD_HTTP_INTERNAL_SERVER_ERROR, 101 TALER_EC_GENERIC_DB_FETCH_FAILED, 102 "lookup_charity"); 103 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 104 return TALER_MHD_reply_with_error (rc->connection, 105 MHD_HTTP_NOT_FOUND, 106 TALER_EC_DONAU_CHARITY_NOT_FOUND, 107 args[0]); 108 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 109 break; 110 } 111 112 if (0 < TALER_amount_cmp (&meta.receipts_to_date, 113 &max_per_year)) 114 { 115 GNUNET_break_op (0); 116 GNUNET_free (meta.charity_name); 117 GNUNET_free (meta.charity_url); 118 return TALER_MHD_reply_with_error (rc->connection, 119 MHD_HTTP_BAD_REQUEST, 120 TALER_EC_GENERIC_PARAMETER_MALFORMED, 121 "max_per_year must NOT be SMALLER than receipts_to_date"); 122 } 123 124 qs = DONAUDB_update_charity (DH_context, 125 charity_id, 126 &charity_pub, 127 charity_name, 128 charity_url, 129 &max_per_year); 130 GNUNET_free (meta.charity_name); 131 GNUNET_free (meta.charity_url); 132 133 switch (qs) 134 { 135 case GNUNET_DB_STATUS_HARD_ERROR: 136 case GNUNET_DB_STATUS_SOFT_ERROR: 137 GNUNET_break (0); 138 return TALER_MHD_reply_with_error (rc->connection, 139 MHD_HTTP_INTERNAL_SERVER_ERROR, 140 TALER_EC_GENERIC_DB_STORE_FAILED, 141 "update_charity"); 142 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 143 return TALER_MHD_reply_with_error (rc->connection, 144 MHD_HTTP_NOT_FOUND, 145 TALER_EC_DONAU_CHARITY_NOT_FOUND, 146 args[0]); 147 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 148 return TALER_MHD_reply_static (rc->connection, 149 MHD_HTTP_OK, 150 NULL, 151 NULL, 152 0); 153 } 154 } 155 156 GNUNET_break (0); 157 return TALER_MHD_reply_with_error (rc->connection, 158 MHD_HTTP_INTERNAL_SERVER_ERROR, 159 TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE, 160 "charity_patch"); 161 } 162 163 164 /* end of donau-httpd_patch-charities-CHARITY_ID.c */