commit d094616fc290711cb1316bb924262c179b840412
parent 66e613f504317e14ca226739118bae9d08361c34
Author: Casaburi Johannes <johannes.casaburi@students.bfh.ch>
Date: Sat, 6 Jan 2024 16:23:30 +0100
added get charities
Diffstat:
4 files changed, 200 insertions(+), 27 deletions(-)
diff --git a/src/donau/donau-httpd_get-charities.c b/src/donau/donau-httpd_get-charities.c
@@ -0,0 +1,116 @@
+/*
+ This file is part of TALER
+ Copyright (C) 2024 Taler Systems SA
+
+ TALER is free software; you can redistribute it and/or modify it under the
+ terms of the GNU Affero General Public License as published by the Free Software
+ Foundation; either version 3, or (at your option) any later version.
+
+ TALER is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+ A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with
+ TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
+*/
+/**
+ * @file donau-httpd_get-charities.c
+ * @brief Return charities
+ * @author Johannes Casaburi
+ */
+#include "platform.h"
+#include <gnunet/gnunet_util_lib.h>
+#include <jansson.h>
+#include <microhttpd.h>
+#include <pthread.h>
+#include "taler/taler_json_lib.h"
+#include "taler/taler_mhd_lib.h"
+#include "taler/taler_signatures.h"
+#include "donau-httpd.h"
+#include "donaudb_plugin.h"
+#include "donau-httpd_charity.h"
+// #include "taler-exchange-httpd_metrics.h"
+
+
+/**
+ * Maximum number of charities we return per request.
+ */
+#define MAX_RECORDS 1024
+
+/**
+ * Return charities information.
+ *
+ * @param cls closure
+ */
+static void
+charities_cb (void *cls)
+{
+ json_t *charities = cls;
+
+ GNUNET_assert (
+ 0 ==
+ json_array_append (
+ charities,
+ GNUNET_JSON_PACK (
+ GNUNET_JSON_pack_string ("url",
+ charity_url),
+ GNUNET_JSON_pack_string ("name",
+ charity_name)
+ )));
+}
+
+
+MHD_RESULT
+DH_handler_charities_get (
+ struct TEH_RequestContext *rc,
+ const char *const args[])
+{
+
+ if (NULL != args[1])
+ {
+ GNUNET_break_op (0);
+ return TALER_MHD_reply_with_error (rc->connection,
+ MHD_HTTP_BAD_REQUEST,
+ TALER_EC_GENERIC_ENDPOINT_UNKNOWN,
+ args[1]);
+ }
+
+ {
+ json_t *charities;
+ enum GNUNET_DB_QueryStatus qs;
+
+ charities = json_array ();
+ GNUNET_assert (NULL != charities);
+ qs = DH_plugin->get_charities (DH_plugin->cls,
+ &charities_cb,
+ charities);
+ switch (qs)
+ {
+ case GNUNET_DB_STATUS_HARD_ERROR:
+ case GNUNET_DB_STATUS_SOFT_ERROR:
+ json_decref (charities);
+ GNUNET_break (0);
+ return TALER_MHD_reply_with_error (rc->connection,
+ MHD_HTTP_INTERNAL_SERVER_ERROR,
+ TALER_EC_GENERIC_DB_FETCH_FAILED,
+ NULL);
+ case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
+ return TALER_MHD_reply_static (
+ rc->connection,
+ MHD_HTTP_NO_CONTENT,
+ NULL,
+ NULL,
+ 0);
+ case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
+ break;
+ }
+ return TALER_MHD_REPLY_JSON_PACK (
+ rc->connection,
+ MHD_HTTP_OK,
+ GNUNET_JSON_pack_array_steal ("charities",
+ charities));
+ }
+}
+
+
+/* end of donau-httpd_get-charities.c */
diff --git a/src/donau/donau-httpd_get-charity.c b/src/donau/donau-httpd_get-charity.c
@@ -29,7 +29,7 @@
#include "donau-httpd.h"
#include "donaudb_plugin.h"
#include "donau-httpd_charity.h"
-#include "donau-httpd_metrics.h"
+// #include "donau-httpd_metrics.h"
/**
@@ -71,10 +71,10 @@ DH_handler_charity_get (
MHD_RESULT result;
GNUNET_assert (NULL != charity_info);
- qs = DH_plugin->select_charity_info (DH_plugin->cls,
- charity_id,
- &charity_url,
- &charity_name);
+ qs = DH_plugin->get_charity (DH_plugin->cls,
+ charity_id,
+ &charity_url,
+ &charity_name);
switch (qs)
{
case GNUNET_DB_STATUS_HARD_ERROR:
diff --git a/src/donau/donau-httpd_post-charity.c b/src/donau/donau-httpd_post-charity.c
@@ -14,8 +14,8 @@
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
*/
/**
- * @file taler-exchange-httpd_aml-decision.c
- * @brief Handle request about an AML decision.
+ * @file donau-httpd_post-charity.c
+ * @brief Handle request to insert a charity.
* @author Johannes Casaburi
*/
#include "platform.h"
@@ -24,9 +24,9 @@
#include <jansson.h>
#include <microhttpd.h>
#include <pthread.h>
-#include "taler_json_lib.h"
-#include "taler_mhd_lib.h"
-#include "taler_signatures.h"
+#include "taler/taler_json_lib.h"
+#include "taler/taler_mhd_lib.h"
+#include "taler/taler_signatures.h"
#include "taler/taler-exchange-httpd_responses.h"
@@ -49,7 +49,7 @@ struct InsertCharityContext
/**
- * Function implementing AML decision database transaction.
+ * Function implementing insert charity transaction.
*
* Runs the transaction logic; IF it returns a non-error code, the
* transaction logic MUST NOT queue a MHD response. IF it returns an hard
@@ -71,9 +71,9 @@ insert_charity (void *cls,
struct InsertCharityContext *icc = cls;
enum GNUNET_DB_QueryStatus qs;
- qs = TEH_plugin->insert_charity (DH_plugin->cls,
- icc->charity_name,
- icc->charity_url);
+ qs = DH_plugin->insert_charity (DH_plugin->cls,
+ icc->charity_name,
+ icc->charity_url);
if (qs <= 0)
{
if (GNUNET_DB_STATUS_SOFT_ERROR != qs)
@@ -145,4 +145,4 @@ DH_handler_charity_post (
}
-/* end of taler-exchange-httpd_aml-decision.c */
+/* end of donau-httpd_post-charity.c */
diff --git a/src/include/donaudb_plugin.h b/src/include/donaudb_plugin.h
@@ -124,6 +124,19 @@ typedef void
/**
+ * Return charities.
+ *
+ * @param cls closure
+ * @param charity_url
+ * @param charity_name
+ */
+typedef void
+(*DONAUDB_GetCharitiesCallback)(
+ void *cls,
+ const char *charity_name,
+ const char *charity_url);
+
+/**
* @brief The plugin API, returned from the plugin's "init" function.
* The argument given to "init" is simply a configuration handle.
*/
@@ -149,7 +162,7 @@ struct DONAUDB_Plugin
* @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
*/
enum GNUNET_GenericReturnValue
- (*drop_tables)(void *cls);
+ (*drop_tables)(void *cls);
/**
* Create the necessary tables if they are not present
@@ -162,9 +175,9 @@ struct DONAUDB_Plugin
* @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
*/
enum GNUNET_GenericReturnValue
- (*create_tables)(void *cls,
- bool support_partitions,
- uint32_t num_partitions);
+ (*create_tables)(void *cls,
+ bool support_partitions,
+ uint32_t num_partitions);
/**
@@ -176,8 +189,8 @@ struct DONAUDB_Plugin
* @return #GNUNET_OK on success
*/
enum GNUNET_GenericReturnValue
- (*start)(void *cls,
- const char *name);
+ (*start)(void *cls,
+ const char *name);
/**
@@ -189,8 +202,8 @@ struct DONAUDB_Plugin
* @return #GNUNET_OK on success
*/
enum GNUNET_GenericReturnValue
- (*start_read_committed)(void *cls,
- const char *name);
+ (*start_read_committed)(void *cls,
+ const char *name);
/**
* Start a READ ONLY serializable transaction.
@@ -201,8 +214,8 @@ struct DONAUDB_Plugin
* @return #GNUNET_OK on success
*/
enum GNUNET_GenericReturnValue
- (*start_read_only)(void *cls,
- const char *name);
+ (*start_read_only)(void *cls,
+ const char *name);
/**
@@ -212,7 +225,7 @@ struct DONAUDB_Plugin
* @return transaction status
*/
enum GNUNET_DB_QueryStatus
- (*commit)(void *cls);
+ (*commit)(void *cls);
/**
@@ -226,7 +239,7 @@ struct DONAUDB_Plugin
* #GNUNET_SYSERR on hard errors
*/
enum GNUNET_GenericReturnValue
- (*preflight)(void *cls);
+ (*preflight)(void *cls);
/**
@@ -281,6 +294,50 @@ struct DONAUDB_Plugin
const void *extra,
size_t extra_size);
+ /**
+ * Get charity.
+ *
+ * @param cls closure
+ * @param charity_id
+ * @param charity_url
+ * @param charity_name
+ * @return database transaction status
+ */
+ enum GNUNET_DB_QueryStatus
+ (*get_charity)(
+ void *cls,
+ unsigned long long charity_id,
+ const char *charity_url,
+ const char *charity_name);
+
+/**
+ * Get charities.
+ *
+ * @param cls closure
+ * @param cb callback to invoke on each match
+ * @param cb_cls closure for @a cb
+ * @return database transaction status
+ */
+ enum GNUNET_DB_QueryStatus
+ (*get_charities)(
+ void *cls,
+ DONAUDB_GetCharitiesCallback cb,
+ void *cb_cls);
+
+ /**
+ * Insert Charity
+ *
+ * @param cls closure
+ * @param charity_name
+ * @param charity_url
+ * @return database transaction status
+ */
+ enum GNUNET_DB_QueryStatus
+ (*insert_charity)(
+ void *cls,
+ const char *charity_name,
+ const char *charity_url);
+
};