donau-httpd_get-charity-CHARITY_ID.c (4128B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2023-2024 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_get-charity-CHARITY_ID.c 18 * @brief Return summary information about a charity 19 * @author Johannes Casaburi 20 */ 21 #include <donau_config.h> 22 #include <gnunet/gnunet_util_lib.h> 23 #include <jansson.h> 24 #include <microhttpd.h> 25 #include <taler/taler_json_lib.h> 26 #include <taler/taler_mhd_lib.h> 27 #include <taler/taler_signatures.h> 28 #include "donau-httpd_get-charity-CHARITY_ID.h" 29 #include "donau-database/lookup_charity.h" 30 31 32 /** 33 * Maximum number of records we return per request. 34 */ 35 #define MAX_RECORDS 1024 36 37 38 enum MHD_Result 39 DH_handler_get_charity ( 40 struct DH_RequestContext *rc, 41 const struct DONAU_CharitySignatureP *charity_sig, 42 const char *charity_id_s) 43 { 44 unsigned long long charity_id; 45 char dummy; 46 47 if ( (NULL == charity_id_s) || 48 (1 != sscanf (charity_id_s, 49 "%llu%c", 50 &charity_id, 51 &dummy)) ) 52 { 53 GNUNET_break_op (0); 54 return TALER_MHD_reply_with_error (rc->connection, 55 MHD_HTTP_BAD_REQUEST, 56 TALER_EC_GENERIC_PARAMETER_MALFORMED, 57 "charity_id"); 58 } 59 60 { 61 struct DONAUDB_CharityMetaData meta; 62 enum GNUNET_DB_QueryStatus qs; 63 enum MHD_Result result; 64 65 qs = DONAUDB_lookup_charity (DH_context, 66 (uint64_t) charity_id, 67 &meta); 68 switch (qs) 69 { 70 case GNUNET_DB_STATUS_HARD_ERROR: 71 case GNUNET_DB_STATUS_SOFT_ERROR: 72 GNUNET_break (0); 73 return TALER_MHD_reply_with_error (rc->connection, 74 MHD_HTTP_INTERNAL_SERVER_ERROR, 75 TALER_EC_GENERIC_DB_FETCH_FAILED, 76 NULL); 77 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 78 return TALER_MHD_reply_with_error ( 79 rc->connection, 80 MHD_HTTP_NOT_FOUND, 81 TALER_EC_DONAU_CHARITY_NOT_FOUND, 82 charity_id_s); 83 break; 84 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 85 break; 86 } 87 88 if (GNUNET_OK != 89 DONAU_charity_get_info_verify (&meta.charity_pub, 90 charity_sig)) 91 { 92 GNUNET_break_op (0); 93 GNUNET_free (meta.charity_url); 94 GNUNET_free (meta.charity_name); 95 return TALER_MHD_reply_with_error (rc->connection, 96 MHD_HTTP_FORBIDDEN, 97 TALER_EC_GENERIC_FORBIDDEN, 98 DONAU_HTTP_HEADER_CHARITY_SIGNATURE); 99 } 100 result = TALER_MHD_REPLY_JSON_PACK ( 101 rc->connection, 102 MHD_HTTP_OK, 103 GNUNET_JSON_pack_data_auto ("charity_pub", 104 &meta.charity_pub), 105 GNUNET_JSON_pack_string ("url", 106 meta.charity_url), 107 GNUNET_JSON_pack_string ("name", 108 meta.charity_name), 109 TALER_JSON_pack_amount ("max_per_year", 110 &meta.max_per_year), 111 TALER_JSON_pack_amount ("receipts_to_date", 112 &meta.receipts_to_date), 113 GNUNET_JSON_pack_uint64 ("current_year", 114 meta.current_year)); 115 116 GNUNET_free (meta.charity_url); 117 GNUNET_free (meta.charity_name); 118 return result; 119 } 120 } 121 122 123 /* end of donau-httpd_get-charity-CHARITY_ID.c */