/*
  This file is part of Anastasis
  Copyright (C) 2020-2022 Anastasis SARL

  Anastasis 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.

  Anastasis 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
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file stasis/insert_truth_payment.c
 * @brief Anastasis database: insert truth payment
 * @author Christian Grothoff
 */
#include "platform.h"
#include "anastasis-db_pg.h"
#include "anastasis/anastasis-database/insert_truth_payment.h"
#include "anastasis/anastasis-database/transaction.h"
#include "anastasis/anastasis-database/preflight.h"
#include <taler/taler_pq_lib.h>


/**
 * Record truth upload payment was made.
 *
 * @param uuid the truth's UUID
 * @param amount the amount that was paid
 * @param duration how long is the truth paid for
 * @return transaction status
 */
enum GNUNET_DB_QueryStatus
ANASTASIS_DB_insert_truth_payment (
  const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid,
  const struct TALER_Amount *amount,
  struct GNUNET_TIME_Relative duration)
{
  struct GNUNET_TIME_Timestamp exp = GNUNET_TIME_relative_to_timestamp (
    duration);
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_auto_from_type (uuid),
    TALER_PQ_query_param_amount_with_currency (pg->conn,
                                               amount),
    GNUNET_PQ_query_param_timestamp (&exp),
    GNUNET_PQ_query_param_end
  };

  PREPARE ("insert_truth_payment",
           "INSERT INTO anastasis_truth_payment "
           "(truth_uuid"
           ",amount"
           ",expiration"
           ") VALUES "
           "($1, $2, $3);");
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "insert_truth_payment",
                                             params);
}


/* end of insert_truth_payment.c */
