/*
  This file is part of GNU Taler
  (C) 2023 Taler Systems SA

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

  GNU 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 src/backend/taler-merchant-httpd_delete-private-tokens-SERIAL.c
 * @brief implementing DELETE /instances/$ID/token request handling
 * @author Christian Grothoff
 */
#include "platform.h"
#include "taler-merchant-httpd_delete-private-tokens-SERIAL.h"
#include "taler-merchant-httpd_helper.h"
#include <taler/taler_json_lib.h>
#include "merchant-database/delete_login_token_by_serial.h"


enum MHD_Result
TMH_private_delete_instances_ID_token_SERIAL (
  const struct TMH_RequestHandler *rh,
  struct MHD_Connection *connection,
  struct TMH_HandlerContext *hc)
{
  struct TMH_MerchantInstance *mi = hc->instance;
  enum GNUNET_DB_QueryStatus qs;
  unsigned long long serial;
  char dummy;

  GNUNET_assert (NULL != mi);
  GNUNET_assert (NULL != hc->infix);
  if (1 != sscanf (hc->infix,
                   "%llu%c",
                   &serial,
                   &dummy))
  {
    GNUNET_break_op (0);
    return TALER_MHD_reply_with_error (connection,
                                       MHD_HTTP_BAD_REQUEST,
                                       TALER_EC_GENERIC_PARAMETER_MALFORMED,
                                       "serial must be a number");
  }


  qs = TALER_MERCHANTDB_delete_login_token_by_serial (TMH_db,
                                                      mi->settings.id,
                                                      serial);
  switch (qs)
  {
  case GNUNET_DB_STATUS_HARD_ERROR:
  case GNUNET_DB_STATUS_SOFT_ERROR:
    GNUNET_break (0);
    return TALER_MHD_reply_with_ec (connection,
                                    TALER_EC_GENERIC_DB_STORE_FAILED,
                                    "delete_login_token_by_serial");
  case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    return TALER_MHD_reply_with_error (
      connection,
      MHD_HTTP_NOT_FOUND,
      TALER_EC_MERCHANT_GENERIC_ACCESS_TOKEN_UNKNOWN,
      hc->infix);
  case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    return TALER_MHD_reply_static (connection,
                                   MHD_HTTP_NO_CONTENT,
                                   NULL,
                                   NULL,
                                   0);
  }
  GNUNET_break (0);
  return MHD_NO;
}


/* end of taler-merchant-httpd_delete-private-tokens-SERIAL.c */
