donau

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

donau-httpd_history_get.c (3373B)


      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-history.c
     18  * @brief Return history
     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_history.h"
     31 
     32 
     33 /**
     34  * Maximum number of history we return per request.
     35  */
     36 // #define MAX_RECORDS 1024
     37 
     38 /**
     39  * Return history information.
     40  *
     41  * @param cls closure
     42  */
     43 static enum GNUNET_GenericReturnValue
     44 history_cb (
     45   void *cls,
     46   unsigned long long charity_id,
     47   struct TALER_Amount final_amount,
     48   uint64_t donation_year)
     49 {
     50   json_t *history = cls;
     51 
     52   GNUNET_assert (
     53     0 ==
     54     json_array_append (
     55       history,
     56       GNUNET_JSON_PACK (
     57         GNUNET_JSON_pack_int64 ("charity_id",
     58                                 charity_id),
     59         TALER_JSON_pack_amount ("final_amount",
     60                                 &final_amount),
     61         GNUNET_JSON_pack_int64 ("donation_year",
     62                                 donation_year))));
     63 
     64   return GNUNET_OK;
     65 }
     66 
     67 
     68 MHD_RESULT
     69 DH_handler_history_get (
     70   struct DH_RequestContext *rc,
     71   const char *const args[])
     72 {
     73 
     74   if (NULL != args[1])
     75   {
     76     GNUNET_break_op (0);
     77     return TALER_MHD_reply_with_error (rc->connection,
     78                                        MHD_HTTP_BAD_REQUEST,
     79                                        TALER_EC_GENERIC_ENDPOINT_UNKNOWN,
     80                                        args[1]);
     81   }
     82 
     83   {
     84     json_t *history;
     85     enum GNUNET_DB_QueryStatus qs;
     86 
     87     history = json_array ();
     88     GNUNET_assert (NULL != history);
     89     qs = DH_plugin->get_history (DH_plugin->cls,
     90                                  &history_cb,
     91                                  history);
     92     switch (qs)
     93     {
     94     case GNUNET_DB_STATUS_HARD_ERROR:
     95     case GNUNET_DB_STATUS_SOFT_ERROR:
     96       json_decref (history);
     97       GNUNET_break (0);
     98       return TALER_MHD_reply_with_error (rc->connection,
     99                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
    100                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
    101                                          NULL);
    102     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    103       return TALER_MHD_reply_static (
    104         rc->connection,
    105         MHD_HTTP_NO_CONTENT,
    106         NULL,
    107         NULL,
    108         0);
    109     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    110       break;
    111     }
    112     return TALER_MHD_REPLY_JSON_PACK (
    113       rc->connection,
    114       MHD_HTTP_OK,
    115       GNUNET_JSON_pack_array_steal ("history",
    116                                     history));
    117   }
    118 }
    119 
    120 
    121 /* end of donau-httpd_get-history.c */