From baef8cc91f5fd86cde021acc6a9630cca009669f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=A6=8C=EA=A6=AB=EA=A6=B6=EA=A6=8F=EA=A7=80=EA=A6=A6?= =?UTF-8?q?=EA=A6=BF=EA=A6=A7=EA=A6=AE=EA=A6=91=EA=A6=A9=EA=A6=AD=EA=A7=80?= Date: Sun, 14 May 2023 16:26:04 +0800 Subject: Improve the order implementation --- interchange.cxx | 35 +++++++++++++++++++++++++---------- interchange.hxx | 12 ++++++++---- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/interchange.cxx b/interchange.cxx index 6108547..21a1fb5 100644 --- a/interchange.cxx +++ b/interchange.cxx @@ -4,6 +4,7 @@ namespace QInterchange { static Interchange* interchange; + static char *mv_sku = nullptr, *mv_order_item = nullptr; static int sampleUrlLength = 0; Interchange::Interchange(const char* sampleURL, const char* image_Dir, @@ -70,6 +71,29 @@ namespace QInterchange { defaultCatalog("All-Products"); } + void Interchange::order(const QString &sku, const QString &item, + const int quantity) + { + mv_sku = (char *)malloc(sku.size() + 1); + strcpy(mv_sku, sku.toLatin1().constData()); + if (item.isEmpty()) { + mv_order_item = (char *)malloc(sku.size() + 1); + strcpy(mv_order_item, sku.toLatin1().constData()); + } else { + mv_order_item = (char *)malloc(item.size() + 1); + strcpy(mv_order_item, item.toLatin1().constData()); + } + interchange_ord_order(mv_sku, mv_order_item, quantity, + [](interchange_response *response) { + free(mv_sku); + mv_sku = nullptr; + free(mv_order_item); + mv_order_item = nullptr; + interchange->emitOrder(QString{response->data}); + interchange_free_response(response); + }); + } + void Interchange::emitPage(QString const& path, QString const& response) { emit gotPage(path, response); @@ -85,17 +109,8 @@ namespace QInterchange { emit gotProduct(response); } - void Interchange::emitOrder(QString const& response) + void Interchange::emitOrder(const QString &response) { emit gotOrder(response); } - - void Interchange::order(QString const& sku) - { - interchange_ord_order(sku.toLatin1().constData(), - [](interchange_response* response) { - interchange->emitOrder(QString{response->data}); - interchange_free_response(response); - }); - } } diff --git a/interchange.hxx b/interchange.hxx index f412377..582a007 100644 --- a/interchange.hxx +++ b/interchange.hxx @@ -61,23 +61,27 @@ namespace QInterchange { void defaultAllProducts(); /*! * \brief For putting an item to a cart. - * \param sku The SKU of the item to order. + * \param sku The product or variant SKU of the item. + * \param item The product SKU of the item to order. + * \param quantity The quantity of the item to order. */ - void order(QString const& sku); + void order(const QString &sku, + const QString &item = "", + const int quantity = 1); signals: void gotPage(QString const& path, QString const& response); void gotCatalog(QString const& response); void gotProduct(QString const& response); - void gotOrder(QString const& response); + void gotOrder(const QString &response); protected: void emitPage(QString const& path, QString const& response); void emitCatalog(QString const& response); void emitProduct(QString const& response); - void emitOrder(QString const& response); + void emitOrder(const QString &response); }; } -- cgit v1.2.3