From 7a464be6baa1425795fd354aac3671b0573a28b6 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 19 Apr 2020 17:07:22 +0200 Subject: work on delete/get products by ID --- src/backend/Makefile.am | 6 +- src/backend/taler-merchant-httpd.c | 16 ++++ ...ler-merchant-httpd_private-delete-products-ID.c | 85 +++++++++++++++++++ ...ler-merchant-httpd_private-delete-products-ID.h | 41 +++++++++ .../taler-merchant-httpd_private-get-products-ID.c | 99 ++++++++++++++++++++++ .../taler-merchant-httpd_private-get-products-ID.h | 41 +++++++++ 6 files changed, 286 insertions(+), 2 deletions(-) create mode 100644 src/backend/taler-merchant-httpd_private-delete-products-ID.c create mode 100644 src/backend/taler-merchant-httpd_private-delete-products-ID.h create mode 100644 src/backend/taler-merchant-httpd_private-get-products-ID.c create mode 100644 src/backend/taler-merchant-httpd_private-get-products-ID.h (limited to 'src/backend') 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 +*/ +/** + * @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 + + +/** + * 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 +*/ +/** + * @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 +*/ +/** + * @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 + + +/** + * 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 +*/ +/** + * @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 -- cgit v1.2.3