gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] 10/19: list, update, delete token families


From: gnunet
Subject: [taler-merchant] 10/19: list, update, delete token families
Date: Fri, 22 Dec 2023 17:25:09 +0100

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository merchant.

commit 0699871850011f41dedda515cc3438ec54791f89
Author: Christian Blättler <blatc2@bfh.ch>
AuthorDate: Mon Nov 27 07:24:10 2023 +0100

    list, update, delete token families
---
 ...hant-httpd_private-delete-token-families-SLUG.c |   2 +
 ...erchant-httpd_private-get-token-families-SLUG.c |   4 +-
 ...ler-merchant-httpd_private-get-token-families.c |  24 ++--
 ...chant-httpd_private-patch-token-families-SLUG.c |   1 -
 ...er-merchant-httpd_private-post-token-families.c |  17 ++-
 src/backenddb/Makefile.am                          |   3 +
 src/backenddb/merchant-0002.sql                    |   3 +
 src/backenddb/pg_delete_token_family.c             |  53 ++++++++
 src/backenddb/pg_delete_token_family.h             |  41 ++++++
 src/backenddb/pg_insert_token_family.c             |  13 +-
 src/backenddb/pg_lookup_token_families.c           | 150 +++++++++++++++++++++
 src/backenddb/pg_lookup_token_families.h           |  43 ++++++
 src/backenddb/pg_lookup_token_family.c             |  44 ++++--
 ...ert_token_family.c => pg_update_token_family.c} |  53 +++-----
 src/backenddb/pg_update_token_family.h             |  44 ++++++
 src/backenddb/plugin_merchantdb_postgres.c         |  10 ++
 src/include/taler_merchantdb_plugin.h              |  35 ++---
 17 files changed, 457 insertions(+), 83 deletions(-)

diff --git 
a/src/backend/taler-merchant-httpd_private-delete-token-families-SLUG.c 
b/src/backend/taler-merchant-httpd_private-delete-token-families-SLUG.c
index 3418b795..de7b6471 100644
--- a/src/backend/taler-merchant-httpd_private-delete-token-families-SLUG.c
+++ b/src/backend/taler-merchant-httpd_private-delete-token-families-SLUG.c
@@ -20,6 +20,7 @@
  */
 #include "platform.h"
 #include "taler-merchant-httpd_private-delete-token-families-SLUG.h"
+#include <gnunet/gnunet_db_lib.h>
 #include <taler/taler_json_lib.h>
 
 
@@ -58,6 +59,7 @@ TMH_private_delete_token_families_SLUG (const struct 
TMH_RequestHandler *rh,
                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
                                        
TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
                                        "delete_token_family (soft)");
+  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     return TALER_MHD_reply_static (connection,
                                    MHD_HTTP_NO_CONTENT,
diff --git a/src/backend/taler-merchant-httpd_private-get-token-families-SLUG.c 
b/src/backend/taler-merchant-httpd_private-get-token-families-SLUG.c
index d31884da..b7c8ab4d 100644
--- a/src/backend/taler-merchant-httpd_private-get-token-families-SLUG.c
+++ b/src/backend/taler-merchant-httpd_private-get-token-families-SLUG.c
@@ -60,7 +60,6 @@ TMH_private_get_tokenfamilies_SLUG (const struct 
TMH_RequestHandler *rh,
                                        
TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
                                        hc->infix);
   }
-  // TODO: Why is this block here? Limiting variable lifetime?
   {
     char *kind = NULL;
     if (TALER_MERCHANTDB_TFK_Subscription == details.kind)
@@ -92,10 +91,11 @@ TMH_private_get_tokenfamilies_SLUG (const struct 
TMH_RequestHandler *rh,
       GNUNET_JSON_pack_timestamp ("valid_before", details.valid_before),
       GNUNET_JSON_pack_time_rel ("duration", details.duration),
       GNUNET_JSON_pack_string ("kind", kind)
-      );
+    );
 
     GNUNET_free (details.name);
     GNUNET_free (details.description);
+    GNUNET_free (kind);
     return result;
   }
 }
diff --git a/src/backend/taler-merchant-httpd_private-get-token-families.c 
b/src/backend/taler-merchant-httpd_private-get-token-families.c
index 031edd90..003e2966 100644
--- a/src/backend/taler-merchant-httpd_private-get-token-families.c
+++ b/src/backend/taler-merchant-httpd_private-get-token-families.c
@@ -29,26 +29,28 @@
  * @param cls a `json_t *` JSON array to build
  * @param slug slug of the token family
  * @param name name of the token family
- * @param start_time start time of the token family's validity period
- * @param expiration end time of the token family's validity period
+ * @param valid_after start time of the token family's validity period
+ * @param valid_before end time of the token family's validity period
  */
 static void
 add_token_family (void *cls,
                   const char *slug,
                   const char *name,
-                  struct GNUNET_TIME_Timestamp start_time,
-                  struct GNUNET_TIME_Timestamp expiration)
+                  struct GNUNET_TIME_Timestamp valid_after,
+                  struct GNUNET_TIME_Timestamp valid_before,
+                  const char *kind)
 {
   json_t *pa = cls;
 
   GNUNET_assert (0 ==
-                 json_array_append_new (
-                   pa,
-                   GNUNET_JSON_PACK (
-                     GNUNET_JSON_pack_string ("slug", slug),
-                     GNUNET_JSON_pack_string ("name", name),
-                     GNUNET_JSON_pack_timestamp ("start_time", start_time),
-                     GNUNET_JSON_pack_timestamp ("expiration", expiration))));
+                  json_array_append_new (
+                    pa,
+                    GNUNET_JSON_PACK (
+                      GNUNET_JSON_pack_string ("slug", slug),
+                      GNUNET_JSON_pack_string ("name", name),
+                      GNUNET_JSON_pack_timestamp ("valid_after", valid_after),
+                      GNUNET_JSON_pack_timestamp ("valid_before", 
valid_before),
+                      GNUNET_JSON_pack_string ("kind", kind))));
 }
 
 
diff --git 
a/src/backend/taler-merchant-httpd_private-patch-token-families-SLUG.c 
b/src/backend/taler-merchant-httpd_private-patch-token-families-SLUG.c
index a5fef59d..8d34b2ff 100644
--- a/src/backend/taler-merchant-httpd_private-patch-token-families-SLUG.c
+++ b/src/backend/taler-merchant-httpd_private-patch-token-families-SLUG.c
@@ -49,7 +49,6 @@ TMH_private_patch_token_family_SLUG (const struct 
TMH_RequestHandler *rh,
   struct TMH_MerchantInstance *mi = hc->instance;
   const char *slug = hc->infix;
   struct TALER_MERCHANTDB_TokenFamilyDetails details = {0};
-  int64_t total_stock;
   enum GNUNET_DB_QueryStatus qs;
   struct GNUNET_JSON_Specification spec[] = {
     GNUNET_JSON_spec_string ("name",
diff --git a/src/backend/taler-merchant-httpd_private-post-token-families.c 
b/src/backend/taler-merchant-httpd_private-post-token-families.c
index 6ff25942..6bf048f9 100644
--- a/src/backend/taler-merchant-httpd_private-post-token-families.c
+++ b/src/backend/taler-merchant-httpd_private-post-token-families.c
@@ -73,6 +73,7 @@ TMH_private_post_token_families (const struct 
TMH_RequestHandler *rh,
 {
   struct TMH_MerchantInstance *mi = hc->instance;
   struct TALER_MERCHANTDB_TokenFamilyDetails details = { 0 };
+  const char *kind = NULL;
   enum GNUNET_DB_QueryStatus qs;
   struct GNUNET_JSON_Specification spec[] = {
     GNUNET_JSON_spec_string ("slug",
@@ -85,7 +86,7 @@ TMH_private_post_token_families (const struct 
TMH_RequestHandler *rh,
       GNUNET_JSON_spec_json ("description_i18n",
                              &details.description_i18n),
       NULL),
-    GNUNET_JSON_spec_uint32("kind", &details.kind),
+    GNUNET_JSON_spec_string("kind", &kind),
     GNUNET_JSON_spec_timestamp ("valid_after",
                                 &details.valid_after),
     GNUNET_JSON_spec_timestamp ("valid_before",
@@ -111,6 +112,20 @@ TMH_private_post_token_families (const struct 
TMH_RequestHandler *rh,
     }
   }
 
+
+  if (strcmp(kind, "discount") == 0)
+    details.kind = TALER_MERCHANTDB_TFK_Discount;
+  else if (strcmp(kind, "subscription") == 0)
+    details.kind = TALER_MERCHANTDB_TFK_Subscription;
+  else
+  {
+    GNUNET_break (0);
+    return TALER_MHD_reply_with_error (connection,
+                                       MHD_HTTP_BAD_REQUEST,
+                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
+                                       "kind");
+  }
+
   if (NULL == details.description_i18n)
     details.description_i18n = json_object ();
 
diff --git a/src/backenddb/Makefile.am b/src/backenddb/Makefile.am
index f0e2bc9c..a63a56b1 100644
--- a/src/backenddb/Makefile.am
+++ b/src/backenddb/Makefile.am
@@ -168,6 +168,9 @@ libtaler_plugin_merchantdb_postgres_la_SOURCES = \
   pg_lookup_pending_webhooks.h pg_lookup_pending_webhooks.c \
   pg_insert_token_family.h pg_insert_token_family.c \
   pg_lookup_token_family.h pg_lookup_token_family.c \
+  pg_lookup_token_families.h pg_lookup_token_families.c \
+  pg_delete_token_family.h pg_delete_token_family.c \
+  pg_update_token_family.h pg_update_token_family.c \
   plugin_merchantdb_postgres.c \
   pg_helper.h pg_helper.c
 libtaler_plugin_merchantdb_postgres_la_LIBADD = \
diff --git a/src/backenddb/merchant-0002.sql b/src/backenddb/merchant-0002.sql
index b92a1fcf..33324c8e 100644
--- a/src/backenddb/merchant-0002.sql
+++ b/src/backenddb/merchant-0002.sql
@@ -47,6 +47,7 @@ CREATE INDEX IF NOT EXISTS 
merchant_contract_terms_by_merchant_and_session
 
 CREATE TABLE IF NOT EXISTS merchant_token_families
   (token_family_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
+  ,merchant_serial BIGINT NOT NULL REFERENCES merchant_instances 
(merchant_serial) ON DELETE CASCADE
   ,slug TEXT NOT NULL UNIQUE
   ,name TEXT NOT NULL
   ,description TEXT
@@ -60,6 +61,8 @@ CREATE TABLE IF NOT EXISTS merchant_token_families
   );
 COMMENT ON TABLE merchant_token_families
  IS 'Token families configured by the merchant.';
+COMMENT ON COLUMN merchant_token_families.merchant_serial
+ IS 'Instance where the token family is configured.';
 COMMENT ON COLUMN merchant_token_families.slug
  IS 'Unique slug for the token family.';
 COMMENT ON COLUMN merchant_token_families.name
diff --git a/src/backenddb/pg_delete_token_family.c 
b/src/backenddb/pg_delete_token_family.c
new file mode 100644
index 00000000..46a4c01f
--- /dev/null
+++ b/src/backenddb/pg_delete_token_family.c
@@ -0,0 +1,53 @@
+/*
+   This file is part of TALER
+   Copyright (C) 2023 Taler Systems SA
+
+   TALER is free software; you can redistribute it and/or modify it under the
+   terms of the GNU 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along with
+   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_delete_token_family.c
+ * @brief Implementation of the delete_token_family function for Postgres
+ * @author Christian Blättler
+ */
+#include "platform.h"
+#include <taler/taler_error_codes.h>
+#include <taler/taler_dbevents.h>
+#include <taler/taler_pq_lib.h>
+#include "pg_delete_token_family.h"
+#include "pg_helper.h"
+
+enum GNUNET_DB_QueryStatus
+TMH_PG_delete_token_family (void *cls,
+                            const char *instance_id,
+                            const char *token_family_slug)
+{
+  struct PostgresClosure *pg = cls;
+  struct GNUNET_PQ_QueryParam params[] = {
+    GNUNET_PQ_query_param_string (instance_id),
+    GNUNET_PQ_query_param_string (token_family_slug),
+    GNUNET_PQ_query_param_end
+  };
+
+  check_connection (pg);
+  PREPARE (pg,
+           "delete_token_family",
+           "DELETE"
+           " FROM merchant_token_families"
+           " WHERE merchant_token_families.merchant_serial="
+           "     (SELECT merchant_serial "
+           "        FROM merchant_instances"
+           "        WHERE merchant_id=$1)"
+           "   AND slug=$2");
+  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
+                                             "delete_token_family",
+                                             params);
+}
\ No newline at end of file
diff --git a/src/backenddb/pg_delete_token_family.h 
b/src/backenddb/pg_delete_token_family.h
new file mode 100644
index 00000000..ed380998
--- /dev/null
+++ b/src/backenddb/pg_delete_token_family.h
@@ -0,0 +1,41 @@
+/*
+   This file is part of TALER
+   Copyright (C) 2023 Taler Systems SA
+
+   TALER is free software; you can redistribute it and/or modify it under the
+   terms of the GNU 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along with
+   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_delete_token_family.h
+ * @brief implementation of the delete_token_family function for Postgres
+ * @author Christian Blättler
+ */
+#ifndef PG_DELETE_TOKEN_FAMILY_H
+#define PG_DELETE_TOKEN_FAMILY_H
+
+#include <taler/taler_util.h>
+#include <taler/taler_json_lib.h>
+#include "taler_merchantdb_plugin.h"
+
+/**
+  * Delete information about a token family.
+  *
+  * @param cls closure
+  * @param instance_id instance to delete token family of
+  * @param token_family_slug slug of token family to delete
+  * @return database result code
+  */
+enum GNUNET_DB_QueryStatus
+TMH_PG_delete_token_family (void *cls,
+                            const char *instance_id,
+                            const char *token_family_slug);
+
+#endif
diff --git a/src/backenddb/pg_insert_token_family.c 
b/src/backenddb/pg_insert_token_family.c
index 88cd8649..bf7159b8 100644
--- a/src/backenddb/pg_insert_token_family.c
+++ b/src/backenddb/pg_insert_token_family.c
@@ -41,13 +41,14 @@ TMH_PG_insert_token_family (void *cls,
       break;
     case TALER_MERCHANTDB_TFK_Subscription:
       kind = "subscription";
+      break;
     default:
       GNUNET_break (0);
       return GNUNET_DB_STATUS_HARD_ERROR;
   }
 
   struct GNUNET_PQ_QueryParam params[] = {
-    // GNUNET_PQ_query_param_string (instance_id),
+    GNUNET_PQ_query_param_string (instance_id),
     GNUNET_PQ_query_param_string (token_family_slug),
     GNUNET_PQ_query_param_string (details->name),
     GNUNET_PQ_query_param_string (details->description),
@@ -63,7 +64,8 @@ TMH_PG_insert_token_family (void *cls,
   PREPARE (pg,
            "insert_token_family",
            "INSERT INTO merchant_token_families"
-           "(slug"
+           "(merchant_serial"
+           ",slug"
            ",name"
            ",description"
            ",description_i18n"
@@ -71,10 +73,9 @@ TMH_PG_insert_token_family (void *cls,
            ",valid_before"
            ",duration"
            ",kind)"
-           "VALUES"
-           "($1, $2, $3, $4, $5, $6, $7, $8)");
-          //  " FROM merchant_instances"
-          //  " WHERE merchant_id=$1");
+           " SELECT merchant_serial, $2, $3, $4, $5, $6, $7, $8, $9"
+           " FROM merchant_instances"
+           " WHERE merchant_id=$1");
   return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                              "insert_token_family",
                                              params);
diff --git a/src/backenddb/pg_lookup_token_families.c 
b/src/backenddb/pg_lookup_token_families.c
new file mode 100644
index 00000000..0ebe3b53
--- /dev/null
+++ b/src/backenddb/pg_lookup_token_families.c
@@ -0,0 +1,150 @@
+/*
+   This file is part of TALER
+   Copyright (C) 2023 Taler Systems SA
+
+   TALER is free software; you can redistribute it and/or modify it under the
+   terms of the GNU 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along with
+   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_lookup_token_families.c
+ * @brief Implementation of the lookup_token_families function for Postgres
+ * @author Christian Blättler
+ */
+#include "platform.h"
+#include <taler/taler_error_codes.h>
+#include <taler/taler_dbevents.h>
+#include <taler/taler_pq_lib.h>
+#include "pg_lookup_token_families.h"
+#include "pg_helper.h"
+#include "taler_merchantdb_plugin.h"
+
+
+/**
+ * Context used for TMH_PG_lookup_token_families().
+ */
+struct LookupTokenFamiliesContext
+{
+  /**
+   * Function to call with the results.
+   */
+  TALER_MERCHANTDB_TokenFamiliesCallback cb;
+
+  /**
+   * Closure for @a cb.
+   */
+  void *cb_cls;
+
+  /**
+   * Did database result extraction fail?
+   */
+  bool extract_failed;
+};
+
+
+/**
+ * Function to be called with the results of a SELECT statement
+ * that has returned @a num_results results about token families.
+ *
+ * @param[in,out] cls of type `struct LookupTokenFamiliesContext *`
+ * @param result the postgres result
+ * @param num_results the number of results in @a result
+ */
+static void
+lookup_token_families_cb (void *cls,
+                          PGresult *result,
+                          unsigned int num_results)
+{
+  struct LookupTokenFamiliesContext *tflc = cls;
+
+  for (unsigned int i = 0; i < num_results; i++)
+  {
+    char *slug;
+    char *name;
+    char *kind;
+    struct GNUNET_TIME_Timestamp valid_after;
+    struct GNUNET_TIME_Timestamp valid_before;
+    struct GNUNET_PQ_ResultSpec rs[] = {
+      GNUNET_PQ_result_spec_string ("slug",
+                                    &slug),
+      GNUNET_PQ_result_spec_string ("name",
+                                    &name),
+      GNUNET_PQ_result_spec_timestamp ("valid_after",
+                                       &valid_after),
+      GNUNET_PQ_result_spec_timestamp ("valid_before",
+                                       &valid_before),
+      GNUNET_PQ_result_spec_string ("kind",
+                                       &kind),
+      GNUNET_PQ_result_spec_end
+    };
+
+    if (GNUNET_OK !=
+        GNUNET_PQ_extract_result (result,
+                                  rs,
+                                  i))
+    {
+      GNUNET_break (0);
+      tflc->extract_failed = true;
+      return;
+    }
+
+    tflc->cb (tflc->cb_cls,
+              slug,
+              name,
+              valid_after,
+              valid_before,
+              kind);
+    GNUNET_PQ_cleanup_result (rs);
+  }
+}
+
+
+enum GNUNET_DB_QueryStatus
+TMH_PG_lookup_token_families (void *cls,
+                              const char *instance_id,
+                              TALER_MERCHANTDB_TokenFamiliesCallback cb,
+                              void *cb_cls)
+{
+  struct PostgresClosure *pg = cls;
+  struct LookupTokenFamiliesContext context = {
+    .cb = cb,
+    .cb_cls = cb_cls,
+    /* Can be overwritten by the lookup_token_families_cb */
+    .extract_failed = false,
+  };
+  struct GNUNET_PQ_QueryParam params[] = {
+    GNUNET_PQ_query_param_string (instance_id),
+    GNUNET_PQ_query_param_end
+  };
+  enum GNUNET_DB_QueryStatus qs;
+
+  check_connection (pg);
+  PREPARE (pg,
+           "lookup_token_families",
+           "SELECT"
+           " slug"
+           ",name"
+           ",valid_after"
+           ",valid_before"
+           ",kind"
+           " FROM merchant_token_families"
+           " JOIN merchant_instances"
+           "   USING (merchant_serial)"
+           " WHERE merchant_instances.merchant_id=$1");
+  qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
+                                             "lookup_token_families",
+                                             params,
+                                             &lookup_token_families_cb,
+                                             &context);
+  /* If there was an error inside lookup_token_families_cb, return a hard 
error. */
+  if (context.extract_failed)
+    return GNUNET_DB_STATUS_HARD_ERROR;
+  return qs;
+}
diff --git a/src/backenddb/pg_lookup_token_families.h 
b/src/backenddb/pg_lookup_token_families.h
new file mode 100644
index 00000000..0c9f80fe
--- /dev/null
+++ b/src/backenddb/pg_lookup_token_families.h
@@ -0,0 +1,43 @@
+/*
+   This file is part of TALER
+   Copyright (C) 2023 Taler Systems SA
+
+   TALER is free software; you can redistribute it and/or modify it under the
+   terms of the GNU 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along with
+   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_lookup_token_families.h
+ * @brief implementation of the lookup_token_families function for Postgres
+ * @author Christian Grothoff
+ */
+#ifndef PG_LOOKUP_TOKEN_FAMILIES_H
+#define PG_LOOKUP_TOKEN_FAMILIES_H
+
+#include <taler/taler_util.h>
+#include <taler/taler_json_lib.h>
+#include "taler_merchantdb_plugin.h"
+
+/**
+ * Lookup all of the token families the given instance has configured.
+ *
+ * @param cls closure
+ * @param instance_id instance to lookup token families for
+ * @param cb function to call on all token families found
+ * @param cb_cls closure for @a cb
+ * @return database result code
+ */
+enum GNUNET_DB_QueryStatus
+TMH_PG_lookup_token_families (void *cls,
+                              const char *instance_id,
+                              TALER_MERCHANTDB_TokenFamiliesCallback cb,
+                              void *cb_cls);
+
+#endif
diff --git a/src/backenddb/pg_lookup_token_family.c 
b/src/backenddb/pg_lookup_token_family.c
index 7a871692..848b79a9 100644
--- a/src/backenddb/pg_lookup_token_family.c
+++ b/src/backenddb/pg_lookup_token_family.c
@@ -33,7 +33,7 @@ TMH_PG_lookup_token_family (void *cls,
 {
   struct PostgresClosure *pg = cls;
   struct GNUNET_PQ_QueryParam params[] = {
-    // GNUNET_PQ_query_param_string (instance_id),
+    GNUNET_PQ_query_param_string (instance_id),
     GNUNET_PQ_query_param_string (token_family_slug),
     GNUNET_PQ_query_param_end
   };
@@ -52,6 +52,8 @@ TMH_PG_lookup_token_family (void *cls,
   }
   else
   {
+    char *kind;
+
     struct GNUNET_PQ_ResultSpec rs[] = {
       GNUNET_PQ_result_spec_string ("slug",
                                     &details->slug),
@@ -67,11 +69,12 @@ TMH_PG_lookup_token_family (void *cls,
                                        &details->valid_before),
       GNUNET_PQ_result_spec_relative_time ("duration",
                                            &details->duration),
-      // TODO: How to convert kind string into enum 
TALER_MERCHANTDB_TokenFamilyKind
+      GNUNET_PQ_result_spec_string ("kind",
+                                    &kind),
       GNUNET_PQ_result_spec_uint64 ("issued",
-                                     &details->issued),
+                                    &details->issued),
       GNUNET_PQ_result_spec_uint64 ("redeemed",
-                                     &details->redeemed),
+                                    &details->redeemed),
       GNUNET_PQ_result_spec_end
     };
 
@@ -86,16 +89,33 @@ TMH_PG_lookup_token_family (void *cls,
              ",valid_after"
              ",valid_before"
              ",duration"
+             ",kind"
              ",issued"
              ",redeemed"
              " FROM merchant_token_families"
-            //  " JOIN merchant_instances"
-            //  "   USING (merchant_serial)"
-            //  " WHERE merchant_instances.merchant_id=$1"
-             "   WHERE merchant_token_families.slug=$1");
-    return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
-                                                     "lookup_token_family",
-                                                     params,
-                                                     rs);
+             " JOIN merchant_instances"
+             "   USING (merchant_serial)"
+             " WHERE merchant_instances.merchant_id=$1"
+             "   AND merchant_token_families.slug=$2");
+    enum GNUNET_DB_QueryStatus qs;
+    qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
+                                                   "lookup_token_family",
+                                                   params,
+                                                   rs);
+
+    if (GNUNET_DB_STATUS_SUCCESS_ONE_RESULT == qs)
+    {
+      if (strcmp(kind, "discount") == 0)
+        details->kind = TALER_MERCHANTDB_TFK_Discount;
+      else if (strcmp(kind, "subscription") == 0)
+        details->kind = TALER_MERCHANTDB_TFK_Subscription;
+      else
+      {
+        GNUNET_break (0);
+        return GNUNET_DB_STATUS_HARD_ERROR;
+      }
+    }
+
+    return qs;
   }
 }
diff --git a/src/backenddb/pg_insert_token_family.c 
b/src/backenddb/pg_update_token_family.c
similarity index 63%
copy from src/backenddb/pg_insert_token_family.c
copy to src/backenddb/pg_update_token_family.c
index 88cd8649..7864c60b 100644
--- a/src/backenddb/pg_insert_token_family.c
+++ b/src/backenddb/pg_update_token_family.c
@@ -14,40 +14,27 @@
    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
  */
 /**
- * @file backenddb/pg_insert_token_family.c
- * @brief Implementation of the insert_token_family function for Postgres
+ * @file backenddb/pg_update_token_family.c
+ * @brief Implementation of the update_token_family function for Postgres
  * @author Christian Blättler
  */
 #include "platform.h"
 #include <taler/taler_error_codes.h>
 #include <taler/taler_dbevents.h>
 #include <taler/taler_pq_lib.h>
-#include "pg_insert_token_family.h"
+#include "pg_update_token_family.h"
 #include "pg_helper.h"
 
 enum GNUNET_DB_QueryStatus
-TMH_PG_insert_token_family (void *cls,
+TMH_PG_update_token_family (void *cls,
                             const char *instance_id,
                             const char *token_family_slug,
                             const struct TALER_MERCHANTDB_TokenFamilyDetails 
*details)
 {
   struct PostgresClosure *pg = cls;
 
-  const char *kind;
-  switch (details->kind)
-  {
-    case TALER_MERCHANTDB_TFK_Discount:
-      kind = "discount";
-      break;
-    case TALER_MERCHANTDB_TFK_Subscription:
-      kind = "subscription";
-    default:
-      GNUNET_break (0);
-      return GNUNET_DB_STATUS_HARD_ERROR;
-  }
-
   struct GNUNET_PQ_QueryParam params[] = {
-    // GNUNET_PQ_query_param_string (instance_id),
+    GNUNET_PQ_query_param_string (instance_id),
     GNUNET_PQ_query_param_string (token_family_slug),
     GNUNET_PQ_query_param_string (details->name),
     GNUNET_PQ_query_param_string (details->description),
@@ -55,27 +42,25 @@ TMH_PG_insert_token_family (void *cls,
     GNUNET_PQ_query_param_timestamp (&details->valid_after),
     GNUNET_PQ_query_param_timestamp (&details->valid_before),
     GNUNET_PQ_query_param_relative_time (&details->duration),
-    GNUNET_PQ_query_param_string (kind),
     GNUNET_PQ_query_param_end
   };
 
   check_connection (pg);
   PREPARE (pg,
-           "insert_token_family",
-           "INSERT INTO merchant_token_families"
-           "(slug"
-           ",name"
-           ",description"
-           ",description_i18n"
-           ",valid_after"
-           ",valid_before"
-           ",duration"
-           ",kind)"
-           "VALUES"
-           "($1, $2, $3, $4, $5, $6, $7, $8)");
-          //  " FROM merchant_instances"
-          //  " WHERE merchant_id=$1");
+           "update_token_family",
+           "UPDATE merchant_token_families SET"
+           " name=$3"
+           ",description=$4"
+           ",description_i18n=$5"
+           ",valid_after=$6"
+           ",valid_before=$7"
+           ",duration=$8"
+           " WHERE merchant_serial="
+           "   (SELECT merchant_serial"
+           "      FROM merchant_instances"
+           "      WHERE merchant_id=$1)"
+           "   AND slug=$2");
   return GNUNET_PQ_eval_prepared_non_select (pg->conn,
-                                             "insert_token_family",
+                                             "update_token_family",
                                              params);
 }
diff --git a/src/backenddb/pg_update_token_family.h 
b/src/backenddb/pg_update_token_family.h
new file mode 100644
index 00000000..84ce65ec
--- /dev/null
+++ b/src/backenddb/pg_update_token_family.h
@@ -0,0 +1,44 @@
+/*
+   This file is part of TALER
+   Copyright (C) 2023 Taler Systems SA
+
+   TALER is free software; you can redistribute it and/or modify it under the
+   terms of the GNU 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 General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along with
+   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
+ */
+/**
+ * @file backenddb/pg_update_token_family.h
+ * @brief implementation of the update_token_family function for Postgres
+ * @author Christian Blättler
+ */
+#ifndef PG_UPDATE_TOKEN_FAMILY_H
+#define PG_UPDATE_TOKEN_FAMILY_H
+
+#include <taler/taler_util.h>
+#include <taler/taler_json_lib.h>
+#include "taler_merchantdb_plugin.h"
+
+/**
+  * Update details about a particular token family.
+  *
+  * @param cls closure
+  * @param instance_id instance to update token family for
+  * @param token_family_slug slug of token family to update
+  * @param details set to the updated token family on success, can be NULL
+  *        (in that case we only want to check if the token family exists)
+  * @return database result code
+  */
+enum GNUNET_DB_QueryStatus
+TMH_PG_update_token_family (void *cls,
+                            const char *instance_id,
+                            const char *token_family_slug,
+                            const struct TALER_MERCHANTDB_TokenFamilyDetails 
*details);
+
+#endif
diff --git a/src/backenddb/plugin_merchantdb_postgres.c 
b/src/backenddb/plugin_merchantdb_postgres.c
index 90586561..38dd636e 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -149,6 +149,9 @@
 #include "pg_insert_pickup.h"
 #include "pg_insert_token_family.h"
 #include "pg_lookup_token_family.h"
+#include "pg_lookup_token_families.h"
+#include "pg_delete_token_family.h"
+#include "pg_update_token_family.h"
 
 
 /**
@@ -607,6 +610,13 @@ libtaler_plugin_merchantdb_postgres_init (void *cls)
     = &TMH_PG_insert_token_family;
   plugin->lookup_token_family
     = &TMH_PG_lookup_token_family;
+  plugin->lookup_token_families
+    = &TMH_PG_lookup_token_families;
+  plugin->delete_token_family
+    = &TMH_PG_delete_token_family;
+  plugin->update_token_family
+    = &TMH_PG_update_token_family;
+
 
 
   return plugin;
diff --git a/src/include/taler_merchantdb_plugin.h 
b/src/include/taler_merchantdb_plugin.h
index c838a175..8df171fe 100644
--- a/src/include/taler_merchantdb_plugin.h
+++ b/src/include/taler_merchantdb_plugin.h
@@ -255,22 +255,6 @@ typedef void
 (*TALER_MERCHANTDB_ProductsCallback)(void *cls,
                                      const char *product_id);
 
-/**
- * Typically called by `lookup_token_families`.
- *
- * @param cls a `json_t *` JSON array to build
- * @param slug slug of the token family
- * @param name name of the token family
- * @param start_time start time of the token family's validity period
- * @param expiration end time of the token family's validity period
- */
-typedef void
-(*TALER_MERCHANTDB_TokenFamiliesCallback)(void *cls,
-                                          const char *slug,
-                                          const char *name,
-                                          struct GNUNET_TIME_Timestamp 
start_time,
-                                          struct GNUNET_TIME_Timestamp 
expiration);
-
 
 /**
  * Details about a product.
@@ -1106,6 +1090,25 @@ enum TALER_MERCHANTDB_TokenFamilyKind
 
 };
 
+
+/**
+ * Typically called by `lookup_token_families`.
+ *
+ * @param cls a `json_t *` JSON array to build
+ * @param slug slug of the token family
+ * @param name name of the token family
+ * @param start_time start time of the token family's validity period
+ * @param expiration end time of the token family's validity period
+ */
+typedef void
+(*TALER_MERCHANTDB_TokenFamiliesCallback)(void *cls,
+                                          const char *slug,
+                                          const char *name,
+                                          struct GNUNET_TIME_Timestamp 
start_time,
+                                          struct GNUNET_TIME_Timestamp 
expiration,
+                                          const char *kind);
+
+
 /**
  * Details about a token family.
  */

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]