donau

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

donau-httpd_charities_get.c (3515B)


      1 /*
      2   This file is part of TALER
      3   Copyright (C) 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_charities_get.c
     18  * @brief Return charities
     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.h"
     29 #include "donaudb_plugin.h"
     30 #include "donau-httpd_charity.h"
     31 
     32 
     33 /**
     34  * Maximum number of charities we return per request.
     35  */
     36 #define MAX_RECORDS 1024
     37 
     38 /**
     39  * Return charities information.
     40  *
     41  * @param cls closure
     42  */
     43 static enum GNUNET_GenericReturnValue
     44 charities_cb (
     45   void *cls,
     46   uint64_t charity_id,
     47   const struct DONAU_CharityPublicKeyP *charity_pub,
     48   const char *charity_name,
     49   const struct TALER_Amount *max_per_year,
     50   uint32_t current_year,
     51   const struct TALER_Amount *receipts_to_date)
     52 {
     53   json_t *charities = cls;
     54 
     55   GNUNET_assert (
     56     0 ==
     57     json_array_append_new (
     58       charities,
     59       GNUNET_JSON_PACK (
     60         GNUNET_JSON_pack_uint64 ("charity_id",
     61                                  charity_id),
     62         GNUNET_JSON_pack_data_auto ("charity_pub",
     63                                     charity_pub),
     64         GNUNET_JSON_pack_string ("charity_name",
     65                                  charity_name),
     66         TALER_JSON_pack_amount ("max_per_year",
     67                                 max_per_year),
     68         GNUNET_JSON_pack_uint64 ("current_year",
     69                                  current_year),
     70         TALER_JSON_pack_amount ("receipts_to_date",
     71                                 receipts_to_date))));
     72   return GNUNET_OK;
     73 }
     74 
     75 
     76 MHD_RESULT
     77 DH_handler_charities_get (
     78   struct DH_RequestContext *rc)
     79 {
     80 
     81   {
     82     json_t *charities;
     83     enum GNUNET_DB_QueryStatus qs;
     84 
     85     charities = json_array ();
     86     GNUNET_assert (NULL != charities);
     87     qs = DH_plugin->get_charities (DH_plugin->cls,
     88                                    &charities_cb,
     89                                    charities);
     90     switch (qs)
     91     {
     92     case GNUNET_DB_STATUS_HARD_ERROR:
     93     case GNUNET_DB_STATUS_SOFT_ERROR:
     94       json_decref (charities);
     95       GNUNET_break (0);
     96       return TALER_MHD_reply_with_error (rc->connection,
     97                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     98                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
     99                                          NULL);
    100     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    101       return TALER_MHD_reply_static (
    102         rc->connection,
    103         MHD_HTTP_NO_CONTENT,
    104         NULL,
    105         NULL,
    106         0);
    107     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    108       break;
    109     }
    110     return TALER_MHD_REPLY_JSON_PACK (
    111       rc->connection,
    112       MHD_HTTP_OK,
    113       GNUNET_JSON_pack_array_steal ("charities",
    114                                     charities));
    115   }
    116 }
    117 
    118 
    119 /* end of donau-httpd_charities_get.c */