donau-httpd_post-charities.c (3913B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2024, 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_post-charities.c 18 * @brief Handle request to insert a charity. 19 * @author Johannes Casaburi 20 * @author Christian Grothoff 21 */ 22 #include <donau_config.h> 23 #include <gnunet/gnunet_util_lib.h> 24 #include <gnunet/gnunet_json_lib.h> 25 #include <jansson.h> 26 #include <microhttpd.h> 27 #include <taler/taler_json_lib.h> 28 #include <taler/taler_mhd_lib.h> 29 #include <taler/taler_signatures.h> 30 #include "donau-httpd_post-charities.h" 31 #include "donau-httpd_db.h" 32 #include "donau-database/insert_charity.h" 33 34 35 enum MHD_Result 36 DH_handler_post_charities (struct DH_RequestContext *rc, 37 const json_t *root, 38 const char *const args[]) 39 { 40 struct DONAU_CharityPublicKeyP charity_pub; 41 const char *charity_name; 42 const char *charity_url; 43 struct TALER_Amount max_per_year; 44 struct GNUNET_JSON_Specification spec[] = { 45 GNUNET_JSON_spec_fixed_auto ("charity_pub", 46 &charity_pub), 47 GNUNET_JSON_spec_string ("charity_name", 48 &charity_name), 49 GNUNET_JSON_spec_string ("charity_url", 50 &charity_url), 51 TALER_JSON_spec_amount ("max_per_year", 52 DH_currency, 53 &max_per_year), 54 GNUNET_JSON_spec_end () 55 }; 56 enum GNUNET_DB_QueryStatus qs; 57 uint64_t charity_id; 58 59 (void) args; 60 { 61 enum GNUNET_GenericReturnValue res; 62 63 res = TALER_MHD_parse_json_data (rc->connection, 64 root, 65 spec); 66 if (GNUNET_SYSERR == res) 67 return MHD_NO; /* hard failure */ 68 if (GNUNET_NO == res) 69 { 70 GNUNET_break_op (0); 71 return MHD_YES; /* failure */ 72 } 73 } 74 qs = DONAUDB_insert_charity (DH_context, 75 &charity_pub, 76 charity_name, 77 charity_url, 78 &max_per_year, 79 &charity_id); 80 switch (qs) 81 { 82 case GNUNET_DB_STATUS_SOFT_ERROR: 83 GNUNET_break (0); 84 return TALER_MHD_reply_with_error (rc->connection, 85 MHD_HTTP_INTERNAL_SERVER_ERROR, 86 TALER_EC_GENERIC_DB_STORE_FAILED, 87 "insert_charity"); 88 case GNUNET_DB_STATUS_HARD_ERROR: 89 GNUNET_break (0); 90 return TALER_MHD_reply_with_error (rc->connection, 91 MHD_HTTP_INTERNAL_SERVER_ERROR, 92 TALER_EC_GENERIC_DB_STORE_FAILED, 93 "insert_charity"); 94 case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: 95 return TALER_MHD_reply_with_error (rc->connection, 96 MHD_HTTP_CONFLICT, 97 TALER_EC_DONAU_CHARITY_PUB_EXISTS, 98 NULL); 99 case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: 100 break; 101 } 102 return TALER_MHD_REPLY_JSON_PACK ( 103 rc->connection, 104 MHD_HTTP_CREATED, 105 GNUNET_JSON_pack_uint64 ("charity_id", 106 charity_id)); 107 } 108 109 110 /* end of donau-httpd_post-charities.c */