diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/Makefile.am | 6 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd.c | 16 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-delete-products-ID.c | 85 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-delete-products-ID.h | 41 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-get-products-ID.c | 99 | ||||
-rw-r--r-- | src/backend/taler-merchant-httpd_private-get-products-ID.h | 41 | ||||
-rw-r--r-- | src/include/taler_merchantdb_plugin.h | 98 |
7 files changed, 384 insertions, 2 deletions
diff --git a/src/backend/Makefile.am b/src/backend/Makefile.am index 14aed023..31245456 100644 --- a/src/backend/Makefile.am +++ b/src/backend/Makefile.am @@ -27,10 +27,12 @@ taler_merchant_httpd_SOURCES = \ taler-merchant-httpd_private-delete-instances-ID.h \ taler-merchant-httpd_private-get-instances.c \ taler-merchant-httpd_private-get-instances.h \ - taler-merchant-httpd_private-get-products.c \ - taler-merchant-httpd_private-get-products.h \ taler-merchant-httpd_private-get-instances-ID.c \ taler-merchant-httpd_private-get-instances-ID.h \ + taler-merchant-httpd_private-get-products.c \ + taler-merchant-httpd_private-get-products.h \ + taler-merchant-httpd_private-get-products-ID.c \ + taler-merchant-httpd_private-get-products-ID.h \ taler-merchant-httpd_private-patch-instances-ID.c \ taler-merchant-httpd_private-patch-instances-ID.h \ taler-merchant-httpd_private-post-instances.c \ diff --git a/src/backend/taler-merchant-httpd.c b/src/backend/taler-merchant-httpd.c index af77a5db..cc722487 100644 --- a/src/backend/taler-merchant-httpd.c +++ b/src/backend/taler-merchant-httpd.c @@ -29,9 +29,11 @@ #include "taler-merchant-httpd_exchanges.h" #include "taler-merchant-httpd_mhd.h" #include "taler-merchant-httpd_private-delete-instances-ID.h" +#include "taler-merchant-httpd_private-delete-products-ID.h" #include "taler-merchant-httpd_private-get-instances.h" #include "taler-merchant-httpd_private-get-instances-ID.h" #include "taler-merchant-httpd_private-get-products.h" +#include "taler-merchant-httpd_private-get-products-ID.h" #include "taler-merchant-httpd_private-patch-instances-ID.h" #include "taler-merchant-httpd_private-post-instances.h" @@ -766,6 +768,20 @@ url_handler (void *cls, .method = MHD_HTTP_METHOD_GET, .handler = &TMH_private_get_products }, + /* GET /products/$ID/: */ + { + .url_prefix = "/products", + .method = MHD_HTTP_METHOD_GET, + .have_id_segment = true, + .handler = &TMH_private_get_instances_ID + }, + /* DELETE /products/$ID/: */ + { + .url_prefix = "/products", + .method = MHD_HTTP_METHOD_DELETE, + .have_id_segment = true, + .handler = &TMH_private_delete_instances_ID + }, { NULL } diff --git a/src/backend/taler-merchant-httpd_private-delete-products-ID.c b/src/backend/taler-merchant-httpd_private-delete-products-ID.c new file mode 100644 index 00000000..995d4221 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-delete-products-ID.c @@ -0,0 +1,85 @@ +/* + This file is part of TALER + (C) 2020 Taler Systems SA + + 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. + + 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 backend/taler-merchant-httpd_private-delete-products-ID.c + * @brief implement DELETE /products/$ID + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler-merchant-httpd_private-delete-products-ID.h" +#include <taler/taler_json_lib.h> + + +/** + * Handle a DELETE "/products/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_delete_products_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_assert (NULL != mi); + qs = TMH_db->delete_product (TMH_db->cls, + mi->settings.id, + hc->infix); + switch (qs) + { + case GNUNET_DB_STATUS_HARD_ERROR: + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_DELETE_PRODUCTS_ID_DB_HARD_FAILURE, + "Transaction failed"); + case GNUNET_DB_STATUS_SOFT_ERROR: + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_INTERNAL_INVARIANT_FAILURE, + "Serialization error for single SQL statement"); + case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS: + qs = TMH_db->lookup_product (TMH_db->cls, + mi->settings.id, + hc->infix, + NULL); + if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_NOT_FOUND, + TALER_EC_DELETE_PRODUCTS_NO_SUCH_PRODUCT, + "Product unknown"); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_CONFLICT, + TALER_EC_DELETE_PRODUCTS_CONFLICTING_LOCK, + "Product deletion impossible, product is locked"); + case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT: + return TALER_MHD_reply_static (connection, + MHD_HTTP_NO_CONTENT, + NULL, + NULL, + 0); + } + GNUNET_assert (0); + return MHD_NO; +} + + +/* end of taler-merchant-httpd_private-delete-products-ID.c */ diff --git a/src/backend/taler-merchant-httpd_private-delete-products-ID.h b/src/backend/taler-merchant-httpd_private-delete-products-ID.h new file mode 100644 index 00000000..507eedf9 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-delete-products-ID.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + 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. + + 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 backend/taler-merchant-httpd_private-delete-products-ID.h + * @brief implement DELETE /products/$ID/ + * @author Christian Grothoff + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_DELETE_PRODUCTS_ID_H +#define TALER_MERCHANT_HTTPD_PRIVATE_DELETE_PRODUCTS_ID_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a DELETE "/products/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_delete_products_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-delete-products-ID.h */ +#endif diff --git a/src/backend/taler-merchant-httpd_private-get-products-ID.c b/src/backend/taler-merchant-httpd_private-get-products-ID.c new file mode 100644 index 00000000..85358ff8 --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-products-ID.c @@ -0,0 +1,99 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + 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. + + 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 backend/taler-merchant-httpd_private-get-products-ID.c + * @brief implement GET /products/$ID + * @author Christian Grothoff + */ +#include "platform.h" +#include "taler-merchant-httpd_private-get-products-ID.h" +#include <taler/taler_json_lib.h> + + +/** + * Handle a GET "/products/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_products_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc) +{ + struct TMH_MerchantInstance *mi = hc->instance; + struct TALER_MERCHANTDB_ProductDetails pd; + enum GNUNET_DB_QueryStatus qs; + + GNUNET_assert (NULL != mi); + qs = TMH_db->lookup_product (TMH_db->cls, + mi->settings.id, + hc->infix, + &pd); + if (0 > qs) + { + GNUNET_break (0); + return TALER_MHD_reply_with_error (connection, + MHD_HTTP_INTERNAL_SERVER_ERROR, + TALER_EC_GET_PRODUCTS_DB_LOOKUP_ERROR, + "failed to lookup products in database"); + } + { + json_t *reply; + + reply = json_pack ( + "{s:s, s:o, s:s, s:o, s:o," + " s:I, s:I, s:I, s:o, s:o," + " s:o}", + "description", + pd.description, + "unit", + pd.unit, + "price", + TALER_JSON_from_amount (&pd.price), + "taxes", + pd.taxes, + /* end of first group of 5 */ + "total_stocked", + (UINT64_MAX == pd.total_stocked) + ? (json_int_t) -1 + : (json_int_t) pd.total_stocked, + "total_sold", + (json_int_t) pd.total_sold, + "total_lost", + (json_int_t) pd.total_lost, + "description_i18n", + pd.description_i18n, + "location", + pd.location, + "image", + pd.image); + GNUNET_free (pd.description); + GNUNET_free (pd.unit); + if (0 != pd.next_restock.abs_value_us) + json_object_set_new (reply, + "next_restock", + GNUNET_JSON_from_time_abs (pd.next_restock)); + return TALER_MHD_reply_json (connection, + reply, + MHD_HTTP_OK); + } +} + + +/* end of taler-merchant-httpd_private-get-products-ID.c */ diff --git a/src/backend/taler-merchant-httpd_private-get-products-ID.h b/src/backend/taler-merchant-httpd_private-get-products-ID.h new file mode 100644 index 00000000..d6e9c7af --- /dev/null +++ b/src/backend/taler-merchant-httpd_private-get-products-ID.h @@ -0,0 +1,41 @@ +/* + This file is part of TALER + (C) 2019, 2020 Taler Systems SA + + 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. + + 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 backend/taler-merchant-httpd_private-get-products-ID.h + * @brief implement GET /products/$ID/ + * @author Christian Grothoff + */ +#ifndef TALER_MERCHANT_HTTPD_PRIVATE_GET_PRODUCTS_ID_H +#define TALER_MERCHANT_HTTPD_PRIVATE_GET_PRODUCTS_ID_H + +#include "taler-merchant-httpd.h" + + +/** + * Handle a GET "/products/$ID" request. + * + * @param rh context of the handler + * @param connection the MHD connection to handle + * @param[in,out] hc context with further information about the request + * @return MHD result code + */ +MHD_RESULT +TMH_private_get_products_ID (const struct TMH_RequestHandler *rh, + struct MHD_Connection *connection, + struct TMH_HandlerContext *hc); + +/* end of taler-merchant-httpd_private-get-products-ID.h */ +#endif diff --git a/src/include/taler_merchantdb_plugin.h b/src/include/taler_merchantdb_plugin.h index fa67daa8..c20cdbb7 100644 --- a/src/include/taler_merchantdb_plugin.h +++ b/src/include/taler_merchantdb_plugin.h @@ -157,6 +157,73 @@ typedef void const char *unit); +/** + * Details about a product. + */ +struct TALER_MERCHANTDB_ProductDetails +{ + /** + * Description of the product. + */ + char *description; + + /** + * Internationalized description. + */ + json_t *description_i18n; + + /** + * Unit in which the product is sold. + */ + char *unit; + + /** + * Price per unit of the product. Zero to imply that the + * product is not sold separately or that the price is not fixed. + */ + struct TALER_Amount price; + + /** + * List of taxes the merchant pays for this product. Never NULL, + * but can be an empty array. + */ + json_t *taxes; + + /** + * Number of units of the product in stock in sum in total, including all + * existing sales and lost product, in product-specific units. UINT64_MAX + * indicates "infinite". + */ + uint64_t total_stocked; + + /** + * Number of units of the product in sold, in product-specific units. + */ + uint64_t total_sold; + + /** + * Number of units of stock lost. + */ + uint64_t total_lost; + + /** + * Base64-encoded product image, or an empty string. + */ + json_t *image; + + /** + * Identifies where the product is in stock, possibly an empty map. + */ + json_t *location; + + /** + * Identifies when the product will be restocked. 0 for unknown, + * #GNUNET_TIME_UNIT_FOREVER_ABS for never. + */ + struct GNUNET_TIME_Absolute next_restock; +}; + + /* **************** OLD: ******************** */ /** @@ -450,6 +517,7 @@ struct TALER_MERCHANTDB_Plugin * @param instance_id instance to lookup products for * @param cb function to call on all products found * @param cb_cls closure for @a cb + * @return database result code */ enum GNUNET_DB_QueryStatus (*lookup_products)(void *cls, @@ -457,6 +525,36 @@ struct TALER_MERCHANTDB_Plugin TALER_MERCHANTDB_ProductsCallback cb, void *cb_cls); + /** + * Lookup details about a particular product. + * + * @param cls closure + * @param instance_id instance to lookup products for + * @param product_id product to lookup + * @param[out] pd set to the product details on success, can be NULL + * (in that case we only want to check if the product exists) + * @return database result code + */ + enum GNUNET_DB_QueryStatus + (*lookup_product)(void *cls, + const char *instance_id, + const char *product_id, + struct TALER_MERCHANTDB_ProductDetails *pd); + + /** + * Delete information about a product. + * + * @param cls closure + * @param instance_id instance to delete product of + * @param product_id product to delete + * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS + * if locks prevent deletion OR product unknown + */ + enum GNUNET_DB_QueryStatus + (*delete_product)(void *cls, + const char *instance_id, + const char *product_id); + /* ****************** OLD API ******************** */ |