donau

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

donau-httpd_get-history.c (3573B)


      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 "donau-httpd_get-history.h"
     30 #include "donau-database/get_history.h"
     31 
     32 
     33 /**
     34  * Return history information.
     35  *
     36  * @param cls closure, JSON array to append history records to
     37  * @param charity_id unique identifier of the charity
     38  * @param final_amount aggregated donation amount for the year
     39  * @param donation_year year of the donations
     40  * @return #GNUNET_OK to continue iteration
     41  */
     42 static enum GNUNET_GenericReturnValue
     43 history_cb (
     44   void *cls,
     45   unsigned long long charity_id,
     46   struct TALER_Amount final_amount,
     47   uint64_t donation_year)
     48 {
     49   json_t *history = cls;
     50 
     51   GNUNET_assert (
     52     0 ==
     53     json_array_append_new (
     54       history,
     55       GNUNET_JSON_PACK (
     56         GNUNET_JSON_pack_int64 ("charity_id",
     57                                 charity_id),
     58         TALER_JSON_pack_amount ("final_amount",
     59                                 &final_amount),
     60         GNUNET_JSON_pack_int64 ("donation_year",
     61                                 donation_year))));
     62 
     63   return GNUNET_OK;
     64 }
     65 
     66 
     67 enum MHD_Result
     68 DH_handler_get_history (
     69   struct DH_RequestContext *rc,
     70   const char *const args[])
     71 {
     72 
     73   if (NULL != args[1])
     74   {
     75     GNUNET_break_op (0);
     76     return TALER_MHD_reply_with_error (rc->connection,
     77                                        MHD_HTTP_BAD_REQUEST,
     78                                        TALER_EC_GENERIC_ENDPOINT_UNKNOWN,
     79                                        args[1]);
     80   }
     81 
     82   {
     83     json_t *history;
     84     enum GNUNET_DB_QueryStatus qs;
     85 
     86     history = json_array ();
     87     GNUNET_assert (NULL != history);
     88     qs = DONAUDB_get_history (DH_context,
     89                               &history_cb,
     90                               history);
     91     switch (qs)
     92     {
     93     case GNUNET_DB_STATUS_HARD_ERROR:
     94     case GNUNET_DB_STATUS_SOFT_ERROR:
     95       json_decref (history);
     96       GNUNET_break (0);
     97       return TALER_MHD_reply_with_error (rc->connection,
     98                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
     99                                          TALER_EC_GENERIC_DB_FETCH_FAILED,
    100                                          NULL);
    101     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    102       json_decref (history);
    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 */