donau

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

donau-httpd_get-charities.c (3970B)


      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_get-charities.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 "donau-httpd_get-charities.h"
     30 #include "donau-database/get_charities.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, JSON array to append charity records to
     42  * @param charity_id unique identifier of the charity
     43  * @param charity_pub public key of the charity
     44  * @param charity_name human-readable name of the charity
     45  * @param max_per_year maximum donation amount per year
     46  * @param current_year current donation year
     47  * @param receipts_to_date total receipts issued so far this year
     48  * @return #GNUNET_OK to continue iteration
     49  */
     50 static enum GNUNET_GenericReturnValue
     51 charities_cb (
     52   void *cls,
     53   uint64_t charity_id,
     54   const struct DONAU_CharityPublicKeyP *charity_pub,
     55   const char *charity_name,
     56   const struct TALER_Amount *max_per_year,
     57   uint32_t current_year,
     58   const struct TALER_Amount *receipts_to_date)
     59 {
     60   json_t *charities = cls;
     61 
     62   GNUNET_assert (
     63     0 ==
     64     json_array_append_new (
     65       charities,
     66       GNUNET_JSON_PACK (
     67         GNUNET_JSON_pack_uint64 ("charity_id",
     68                                  charity_id),
     69         GNUNET_JSON_pack_data_auto ("charity_pub",
     70                                     charity_pub),
     71         GNUNET_JSON_pack_string ("charity_name",
     72                                  charity_name),
     73         TALER_JSON_pack_amount ("max_per_year",
     74                                 max_per_year),
     75         GNUNET_JSON_pack_uint64 ("current_year",
     76                                  current_year),
     77         TALER_JSON_pack_amount ("receipts_to_date",
     78                                 receipts_to_date))));
     79   return GNUNET_OK;
     80 }
     81 
     82 
     83 enum MHD_Result
     84 DH_handler_get_charities (
     85   struct DH_RequestContext *rc)
     86 {
     87 
     88   {
     89     json_t *charities;
     90     enum GNUNET_DB_QueryStatus qs;
     91 
     92     charities = json_array ();
     93     GNUNET_assert (NULL != charities);
     94     qs = DONAUDB_get_charities (DH_context,
     95                                 &charities_cb,
     96                                 charities);
     97     switch (qs)
     98     {
     99     case GNUNET_DB_STATUS_HARD_ERROR:
    100     case GNUNET_DB_STATUS_SOFT_ERROR:
    101       json_decref (charities);
    102       GNUNET_break (0);
    103       return TALER_MHD_reply_with_error (rc->connection,
    104                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
    105                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
    106                                          NULL);
    107     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    108       json_decref (charities);
    109       return TALER_MHD_reply_static (
    110         rc->connection,
    111         MHD_HTTP_NO_CONTENT,
    112         NULL,
    113         NULL,
    114         0);
    115     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    116       break;
    117     }
    118     return TALER_MHD_REPLY_JSON_PACK (
    119       rc->connection,
    120       MHD_HTTP_OK,
    121       GNUNET_JSON_pack_array_steal ("charities",
    122                                     charities));
    123   }
    124 }
    125 
    126 
    127 /* end of donau-httpd_get-charities.c */