/**
 * Nudr_DataRepository API OpenAPI file
 * Unified Data Repository Service. © 2020, 3GPP Organizational Partners (ARIB,
 * ATIS, CCSA, ETSI, TSDSI, TTA, TTC). All rights reserved.
 *
 * The version of the OpenAPI document: 2.1.2
 *
 *
 * NOTE: This class is auto generated by OpenAPI Generator
 * (https://openapi-generator.tech). https://openapi-generator.tech Do not edit
 * the class manually.
 */

#include "PduSessionManagementDataApi.h"
#include "Helpers.h"

namespace org {
namespace openapitools {
namespace server {
namespace api {

using namespace org::openapitools::server::helpers;
using namespace org::openapitools::server::model;

PduSessionManagementDataApi::PduSessionManagementDataApi(
    std::shared_ptr<Pistache::Rest::Router> rtr) {
  router = rtr;
}

void PduSessionManagementDataApi::init() { setupRoutes(); }

void PduSessionManagementDataApi::setupRoutes() {
  using namespace Pistache::Rest;

  Routes::Put(
      *router,
      base + "/exposure-data/:ueId/session-management-data/:pduSessionId",
      Routes::bind(&PduSessionManagementDataApi::
                       create_or_replace_session_management_data_handler,
                   this));
  Routes::Delete(
      *router,
      base + "/exposure-data/:ueId/session-management-data/:pduSessionId",
      Routes::bind(
          &PduSessionManagementDataApi::delete_session_management_data_handler,
          this));
  Routes::Get(
      *router,
      base + "/exposure-data/:ueId/session-management-data/:pduSessionId",
      Routes::bind(
          &PduSessionManagementDataApi::query_session_management_data_handler,
          this));

  // Default handler, called when a route is not found
  router->addCustomHandler(
      Routes::bind(&PduSessionManagementDataApi::
                       pdu_session_management_data_api_default_handler,
                   this));
}

void PduSessionManagementDataApi::
    create_or_replace_session_management_data_handler(
        const Pistache::Rest::Request &request,
        Pistache::Http::ResponseWriter response) {
  // Getting the path params
  auto ueId = request.param(":ueId").as<std::string>();
  auto pduSessionId = request.param(":pduSessionId").as<int32_t>();

  // Getting the body param

  PduSessionManagementData pduSessionManagementData;

  try {
    nlohmann::json::parse(request.body()).get_to(pduSessionManagementData);
    this->create_or_replace_session_management_data(
        ueId, pduSessionId, pduSessionManagementData, response);
  } catch (nlohmann::detail::exception &e) {
    // send a 400 error
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
  } catch (Pistache::Http::HttpError &e) {
    response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
    return;
  } catch (std::exception &e) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}
void PduSessionManagementDataApi::delete_session_management_data_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {
  // Getting the path params
  auto ueId = request.param(":ueId").as<std::string>();
  auto pduSessionId = request.param(":pduSessionId").as<int32_t>();

  try {
    this->delete_session_management_data(ueId, pduSessionId, response);
  } catch (nlohmann::detail::exception &e) {
    // send a 400 error
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
  } catch (Pistache::Http::HttpError &e) {
    response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
    return;
  } catch (std::exception &e) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}
void PduSessionManagementDataApi::query_session_management_data_handler(
    const Pistache::Rest::Request &request,
    Pistache::Http::ResponseWriter response) {
  // Getting the path params
  auto ueId = request.param(":ueId").as<std::string>();
  auto pduSessionId = request.param(":pduSessionId").as<int32_t>();

  // Getting the query params
  auto ipv4AddrQuery = request.query().get("ipv4-addr");
  Pistache::Optional<std::string> ipv4Addr;
  if (!ipv4AddrQuery.isEmpty()) {
    std::string valueQuery_instance;
    if (fromStringValue(ipv4AddrQuery.get(), valueQuery_instance)) {
      ipv4Addr = Pistache::Some(valueQuery_instance);
    }
  }
  auto ipv6PrefixQuery = request.query().get("ipv6-prefix");
  Pistache::Optional<Ipv6Prefix> ipv6Prefix;
  if (!ipv6PrefixQuery.isEmpty()) {
    //        Ipv6Prefix valueQuery_instance;
    //        if(fromStringValue(ipv6PrefixQuery.get(), valueQuery_instance)){
    //            ipv6Prefix = Pistache::Some(valueQuery_instance);
    //        }
  }
  auto dnnQuery = request.query().get("dnn");
  Pistache::Optional<std::string> dnn;
  if (!dnnQuery.isEmpty()) {
    std::string valueQuery_instance;
    if (fromStringValue(dnnQuery.get(), valueQuery_instance)) {
      dnn = Pistache::Some(valueQuery_instance);
    }
  }
  auto fieldsQuery = request.query().get("fields");
  Pistache::Optional<std::vector<std::string>> fields;
  if (!fieldsQuery.isEmpty()) {
    std::vector<std::string> valueQuery_instance;
    if (fromStringValue(fieldsQuery.get(), valueQuery_instance)) {
      fields = Pistache::Some(valueQuery_instance);
    }
  }
  auto suppFeatQuery = request.query().get("supp-feat");
  Pistache::Optional<std::string> suppFeat;
  if (!suppFeatQuery.isEmpty()) {
    std::string valueQuery_instance;
    if (fromStringValue(suppFeatQuery.get(), valueQuery_instance)) {
      suppFeat = Pistache::Some(valueQuery_instance);
    }
  }

  try {
    this->query_session_management_data(ueId, pduSessionId, ipv4Addr,
                                        ipv6Prefix, dnn, fields, suppFeat,
                                        response);
  } catch (nlohmann::detail::exception &e) {
    // send a 400 error
    response.send(Pistache::Http::Code::Bad_Request, e.what());
    return;
  } catch (Pistache::Http::HttpError &e) {
    response.send(static_cast<Pistache::Http::Code>(e.code()), e.what());
    return;
  } catch (std::exception &e) {
    // send a 500 error
    response.send(Pistache::Http::Code::Internal_Server_Error, e.what());
    return;
  }
}

void PduSessionManagementDataApi::
    pdu_session_management_data_api_default_handler(
        const Pistache::Rest::Request &,
        Pistache::Http::ResponseWriter response) {
  response.send(Pistache::Http::Code::Not_Found,
                "The requested method does not exist");
}

} // namespace api
} // namespace server
} // namespace openapitools
} // namespace org
