merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

lookup_template.c (3946B)


      1 /*
      2    This file is part of TALER
      3    Copyright (C) 2022, 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 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 General Public License for more details.
     12 
     13    You should have received a copy of the GNU General Public License along with
     14    TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 /**
     17  * @file src/backenddb/lookup_template.c
     18  * @brief Implementation of the lookup_template function for Postgres
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include <taler/taler_error_codes.h>
     23 #include <taler/taler_dbevents.h>
     24 #include <taler/taler_pq_lib.h>
     25 #include "merchant-database/lookup_template.h"
     26 #include "helper.h"
     27 
     28 
     29 /**
     30  * Lookup details about a particular template.
     31  *
     32  * @param pg database context
     33  * @param instance_id instance to lookup template for
     34  * @param template_id template to lookup
     35  * @param[out] td set to the template details on success, can be NULL
     36  *             (in that case we only want to check if the template exists)
     37  * @return database result code
     38  */
     39 enum GNUNET_DB_QueryStatus
     40 TALER_MERCHANTDB_lookup_template (struct TALER_MERCHANTDB_PostgresContext *pg,
     41                                   const char *instance_id,
     42                                   const char *template_id,
     43                                   struct TALER_MERCHANTDB_TemplateDetails *td)
     44 {
     45   struct GNUNET_PQ_QueryParam params[] = {
     46     GNUNET_PQ_query_param_string (instance_id),
     47     GNUNET_PQ_query_param_string (template_id),
     48     GNUNET_PQ_query_param_end
     49   };
     50   enum GNUNET_DB_QueryStatus qs;
     51 
     52   check_connection (pg);
     53   PREPARE (pg,
     54            "lookup_template",
     55            "SELECT"
     56            " mt.template_description"
     57            ",mod.otp_id"
     58            ",mt.template_contract::TEXT"
     59            ",mt.editable_defaults::TEXT"
     60            " FROM merchant_template mt"
     61            " JOIN merchant_instances mi"
     62            "   ON (mi.merchant_serial = mt.merchant_serial)"
     63            " LEFT JOIN merchant_otp_devices mod"
     64            "   ON (mod.otp_serial = mt.otp_device_id)"
     65            " WHERE mi.merchant_id=$1"
     66            "   AND mt.template_id=$2");
     67   if (NULL == td)
     68   {
     69     struct GNUNET_PQ_ResultSpec rs_null[] = {
     70       GNUNET_PQ_result_spec_end
     71     };
     72 
     73     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
     74                                                    "lookup_template",
     75                                                    params,
     76                                                    rs_null);
     77     GNUNET_PQ_cleanup_query_params_closures (params);
     78     return qs;
     79   }
     80   else
     81   {
     82     struct GNUNET_PQ_ResultSpec rs[] = {
     83       GNUNET_PQ_result_spec_string ("template_description",
     84                                     &td->template_description),
     85       GNUNET_PQ_result_spec_allow_null (
     86         GNUNET_PQ_result_spec_string ("otp_id",
     87                                       &td->otp_id),
     88         NULL),
     89       TALER_PQ_result_spec_json ("template_contract",
     90                                  &td->template_contract),
     91       GNUNET_PQ_result_spec_allow_null (
     92         TALER_PQ_result_spec_json ("editable_defaults",
     93                                    &td->editable_defaults),
     94         NULL),
     95       GNUNET_PQ_result_spec_end
     96     };
     97 
     98     memset (td,
     99             0,
    100             sizeof (*td));
    101     qs = GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
    102                                                    "lookup_template",
    103                                                    params,
    104                                                    rs);
    105     GNUNET_PQ_cleanup_query_params_closures (params);
    106     return qs;
    107   }
    108 }