donau

Donation authority for GNU Taler (experimental)
Log | Files | Refs | Submodules | README | LICENSE

donau-httpd_charity_insert.c (3985B)


      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_charity_insert.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 <pthread.h>
     28 #include <taler/taler_json_lib.h>
     29 #include <taler/taler_mhd_lib.h>
     30 #include <taler/taler_signatures.h>
     31 #include "donaudb_plugin.h"
     32 #include "donau-httpd_charity.h"
     33 #include "donau-httpd_db.h"
     34 
     35 
     36 MHD_RESULT
     37 DH_handler_charity_post (struct DH_RequestContext *rc,
     38                          const json_t *root,
     39                          const char *const args[])
     40 {
     41   struct DONAU_CharityPublicKeyP charity_pub;
     42   const char *charity_name;
     43   const char *charity_url;
     44   struct TALER_Amount max_per_year;
     45   struct GNUNET_JSON_Specification spec[] = {
     46     GNUNET_JSON_spec_fixed_auto ("charity_pub",
     47                                  &charity_pub),
     48     GNUNET_JSON_spec_string ("charity_name",
     49                              &charity_name),
     50     GNUNET_JSON_spec_string ("charity_url",
     51                              &charity_url),
     52     TALER_JSON_spec_amount ("max_per_year",
     53                             DH_currency,
     54                             &max_per_year),
     55     GNUNET_JSON_spec_end ()
     56   };
     57   enum GNUNET_DB_QueryStatus qs;
     58   uint64_t charity_id;
     59 
     60   (void) args;
     61   {
     62     enum GNUNET_GenericReturnValue res;
     63 
     64     res = TALER_MHD_parse_json_data (rc->connection,
     65                                      root,
     66                                      spec);
     67     if (GNUNET_SYSERR == res)
     68       return MHD_NO; /* hard failure */
     69     if (GNUNET_NO == res)
     70     {
     71       GNUNET_break_op (0);
     72       return MHD_YES; /* failure */
     73     }
     74   }
     75   qs = DH_plugin->insert_charity (DH_plugin->cls,
     76                                   &charity_pub,
     77                                   charity_name,
     78                                   charity_url,
     79                                   &max_per_year,
     80                                   &charity_id);
     81   switch (qs)
     82   {
     83   case GNUNET_DB_STATUS_SOFT_ERROR:
     84     GNUNET_break (0);
     85     return TALER_MHD_reply_with_error (rc->connection,
     86                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     87                                        TALER_EC_GENERIC_DB_STORE_FAILED,
     88                                        "insert_charity");
     89   case GNUNET_DB_STATUS_HARD_ERROR:
     90     GNUNET_break (0);
     91     return TALER_MHD_reply_with_error (rc->connection,
     92                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     93                                        TALER_EC_GENERIC_DB_STORE_FAILED,
     94                                        "insert_charity");
     95   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
     96     GNUNET_break (0);
     97     return TALER_MHD_reply_with_error (rc->connection,
     98                                        MHD_HTTP_CONFLICT,
     99                                        TALER_EC_DONAU_CHARITY_PUB_EXISTS,
    100                                        NULL);
    101     return GNUNET_DB_STATUS_HARD_ERROR;
    102   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    103     break;
    104   }
    105   return TALER_MHD_REPLY_JSON_PACK (
    106     rc->connection,
    107     MHD_HTTP_CREATED,
    108     GNUNET_JSON_pack_uint64 ("charity_id",
    109                              charity_id));
    110 }
    111 
    112 
    113 /* end of donau-httpd_charity_insert.c */