diff options
author | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-06 18:23:44 +0800 |
---|---|---|
committer | ꦌ ꦫꦶ ꦏ꧀ꦦꦿ ꦧ ꦮ ꦑ ꦩ ꦭ꧀ <erik@darapsa.co.id> | 2019-10-06 18:23:44 +0800 |
commit | fc555594c636e87ffe2384e6b5240cfa934c6cf4 (patch) | |
tree | 47d75d9b701e1182c5c882628e8349a01416d2a5 | |
parent | c2c89d376b0d80299ad0fe8228ecef4279557160 (diff) |
Order function
-rw-r--r-- | client.c | 61 | ||||
-rw-r--r-- | icclient/client.h | 3 | ||||
-rw-r--r-- | icclient/ord.h | 4 |
3 files changed, 63 insertions, 5 deletions
@@ -1,11 +1,16 @@ #include <stdbool.h> #include <stdlib.h> +#include <string.h> #include "login.h" #include "icclient/product.h" #include "icclient/catalog.h" +#include "icclient/ord.h" #include "icclient/client.h" +typedef struct icclient_product icclient_product; typedef struct icclient_catalog icclient_catalog; +typedef struct icclient_ord_item icclient_ord_item; +typedef struct icclient_ord_order icclient_ord_order; CURL *curl = NULL; char *server_url = NULL; @@ -19,9 +24,6 @@ bool icclient_init(const char *url, const char *certificate) curl_easy_setopt(curl, CURLOPT_COOKIEFILE, ""); if (certificate) curl_easy_setopt(curl, CURLOPT_CAINFO, certificate); -#ifdef DEBUG - curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); -#endif size_t length = strlen(url); bool append = !(bool)(url[length - 1] == '/'); server_url = malloc(length + (size_t)append + 1); @@ -40,6 +42,57 @@ void icclient_allproducts(size_t (*handler)(void *, size_t, size_t, void *) request(handler, (void *)catalogptr, NULL, "%s", "All-Products"); } +static int prodcmp(const void *product1, const void *product2) +{ + return strcmp(((icclient_product *)product1)->sku + , ((icclient_product *)product2)->sku); +} + +static int itemcmp(const void *item1, const void *item2) +{ + return strcmp(((icclient_ord_item *)item1)->product->sku + , ((icclient_ord_item *)item2)->product->sku); +} + +void icclient_order(icclient_ord_order **orderptr, const char *sku + , icclient_catalog *catalog) +{ + if (!*orderptr) { + (*orderptr) = malloc(sizeof(icclient_ord_order)); + (*orderptr)->subtotal = .0; + (*orderptr)->shipping = .0; + (*orderptr)->subtotal = .0; + (*orderptr)->nitems = 0; + } + icclient_ord_order *order = *orderptr; + + qsort(catalog->products, catalog->length, sizeof(icclient_product) + , prodcmp); + icclient_product *product = bsearch(sku, catalog->products + , catalog->length, sizeof(icclient_product), prodcmp); + + qsort(order->items, order->nitems, sizeof(icclient_ord_item), itemcmp); + icclient_ord_item *item = bsearch(sku, order->items, order->nitems + , sizeof(icclient_ord_item), itemcmp); + + if (item) + item->quantity++; + else { + item = malloc(sizeof(icclient_ord_item)); + item->product = product; + item->quantity = 1; + *orderptr = realloc(*orderptr, sizeof(*orderptr) + + sizeof(icclient_ord_item)); + (*orderptr)->items[(*orderptr)->nitems++] = item; + order = *orderptr; + } + + order->subtotal += item->product->price; + order->total_cost += item->product->price; + + request(NULL, NULL, NULL, "%s%s", "order?mv_arg=", sku); +} + void icclient_newaccount(const char *username, const char *password , const char *verify, const char *successpage, const char *nextpage , const char *failpage) @@ -66,7 +119,7 @@ void icclient_page(const char *path, size_t (*handler)(void *, size_t, size_t request(handler, (void *)dataptr, NULL, "%s", path); } -void icclient_freeproduct(struct icclient_product *product) +void icclient_freeproduct(icclient_product *product) { if (product->image) free(product->image); diff --git a/icclient/client.h b/icclient/client.h index e49af36..db38926 100644 --- a/icclient/client.h +++ b/icclient/client.h @@ -3,6 +3,7 @@ struct icclient_product; struct icclient_catalog; +struct icclient_ord_order; #ifdef __cplusplus extern "C" { @@ -12,6 +13,8 @@ extern "C" { void icclient_allproducts(size_t (*handler)(void *contents, size_t size , size_t nmemb, void *userdata) , struct icclient_catalog **catalogptr); + void icclient_order(struct icclient_ord_order **orderptr, const char *sku + , struct icclient_catalog *catalog); void icclient_newaccount(const char *username, const char *password , const char *verify, const char *successpage , const char *nextpage, const char *failpage); diff --git a/icclient/ord.h b/icclient/ord.h index 0cd0e75..5632ffe 100644 --- a/icclient/ord.h +++ b/icclient/ord.h @@ -7,7 +7,9 @@ struct icclient_ord_item { }; struct icclient_ord_order { - double subtotal, shipping, total_cost; + double subtotal; + double shipping; + double total_cost; size_t nitems; struct icclient_ord_item *items[]; }; |