donau-httpd_charity_get.c (4023B)
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_charity_get.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 <pthread.h> 26 #include <taler/taler_json_lib.h> 27 #include <taler/taler_mhd_lib.h> 28 #include <taler/taler_signatures.h> 29 #include "donaudb_plugin.h" 30 #include "donau-httpd_charity.h" 31 32 33 /** 34 * Maximum number of records we return per request. 35 */ 36 #define MAX_RECORDS 1024 37 38 39 MHD_RESULT 40 DH_handler_charity_get ( 41 struct DH_RequestContext *rc, 42 const struct DONAU_CharitySignatureP *charity_sig, 43 const char *charity_id_s) 44 { 45 unsigned long long charity_id; 46 char dummy; 47 48 if ( (NULL == charity_id_s) || 49 (1 != sscanf (charity_id_s, 50 "%llu%c", 51 &charity_id, 52 &dummy)) ) 53 { 54 GNUNET_break_op (0); 55 return TALER_MHD_reply_with_error (rc->connection, 56 MHD_HTTP_BAD_REQUEST, 57 TALER_EC_GENERIC_PARAMETER_MALFORMED, 58 "charity_id"); 59 } 60 61 { 62 struct DONAUDB_CharityMetaData meta; 63 enum GNUNET_DB_QueryStatus qs; 64 MHD_RESULT result; 65 66 qs = DH_plugin->lookup_charity (DH_plugin->cls, 67 (uint64_t) charity_id, 68 &meta); 69 switch (qs) 70 { 71 case GNUNET_DB_STATUS_HARD_ERROR: 72 case GNUNET_DB_STATUS_SOFT_ERROR: 73 GNUNET_break (0); 74 return TALER_MHD_reply_with_error (rc->connection, 75 MHD_HTTP_INTERNAL_SERVER_ERROR, 76 TALER_EC_GENERIC_DB_FETCH_FAILED, 77 NULL); 78 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 79 return TALER_MHD_reply_with_error ( 80 rc->connection, 81 MHD_HTTP_NOT_FOUND, 82 TALER_EC_DONAU_CHARITY_NOT_FOUND, 83 charity_id_s); 84 break; 85 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 86 break; 87 } 88 89 if (GNUNET_OK != 90 DONAU_charity_get_info_verify (&meta.charity_pub, 91 charity_sig)) 92 { 93 GNUNET_break_op (0); 94 return TALER_MHD_reply_with_error (rc->connection, 95 MHD_HTTP_FORBIDDEN, 96 TALER_EC_GENERIC_FORBIDDEN, 97 DONAU_HTTP_HEADER_CHARITY_SIGNATURE); 98 } 99 result = TALER_MHD_REPLY_JSON_PACK ( 100 rc->connection, 101 MHD_HTTP_OK, 102 GNUNET_JSON_pack_data_auto ("charity_pub", 103 &meta.charity_pub), 104 GNUNET_JSON_pack_string ("url", 105 meta.charity_url), 106 GNUNET_JSON_pack_string ("name", 107 meta.charity_name), 108 TALER_JSON_pack_amount ("max_per_year", 109 &meta.max_per_year), 110 TALER_JSON_pack_amount ("receipts_to_date", 111 &meta.receipts_to_date), 112 GNUNET_JSON_pack_uint64 ("current_year", 113 meta.current_year)); 114 115 GNUNET_free (meta.charity_url); 116 GNUNET_free (meta.charity_name); 117 return result; 118 } 119 } 120 121 122 /* end of donau-httpd_charity_get.c */