LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-products-ID.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.9 % 27 24
Test Date: 2025-12-16 19:21:50 Functions: 100.0 % 1 1

            Line data    Source code
       1              : /*
       2              :   This file is part of TALER
       3              :   (C) 2019, 2020, 2021 Taler Systems SA
       4              : 
       5              :   TALER is free software; you can redistribute it and/or modify it under the
       6              :   terms of the GNU Affero General Public License as published by the Free Software
       7              :   Foundation; either version 3, or (at your option) any later version.
       8              : 
       9              :   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
      10              :   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
      11              :   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
      12              : 
      13              :   You should have received a copy of the GNU General Public License along with
      14              :   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
      15              : */
      16              : /**
      17              :  * @file taler-merchant-httpd_private-get-products-ID.c
      18              :  * @brief implement GET /products/$ID
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "platform.h"
      22              : #include "taler-merchant-httpd_private-get-products-ID.h"
      23              : #include <taler/taler_json_lib.h>
      24              : #include "taler-merchant-httpd_helper.h"
      25              : #include <taler/taler_util.h>
      26              : 
      27              : /**
      28              :  * Handle a GET "/products/$ID" request.
      29              :  *
      30              :  * @param rh context of the handler
      31              :  * @param connection the MHD connection to handle
      32              :  * @param[in,out] hc context with further information about the request
      33              :  * @return MHD result code
      34              :  */
      35              : MHD_RESULT
      36           21 : TMH_private_get_products_ID (
      37              :   const struct TMH_RequestHandler *rh,
      38              :   struct MHD_Connection *connection,
      39              :   struct TMH_HandlerContext *hc)
      40              : {
      41           21 :   struct TMH_MerchantInstance *mi = hc->instance;
      42           21 :   struct TALER_MERCHANTDB_ProductDetails pd = { 0 };
      43              :   enum GNUNET_DB_QueryStatus qs;
      44           21 :   size_t num_categories = 0;
      45           21 :   uint64_t *categories = NULL;
      46              :   json_t *jcategories;
      47              : 
      48           21 :   GNUNET_assert (NULL != mi);
      49           21 :   qs = TMH_db->lookup_product (TMH_db->cls,
      50           21 :                                mi->settings.id,
      51           21 :                                hc->infix,
      52              :                                &pd,
      53              :                                &num_categories,
      54              :                                &categories);
      55           21 :   if (0 > qs)
      56              :   {
      57            0 :     GNUNET_break (0);
      58            0 :     return TALER_MHD_reply_with_error (connection,
      59              :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      60              :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
      61              :                                        "lookup_product");
      62              :   }
      63           21 :   if (0 == qs)
      64              :   {
      65            8 :     return TALER_MHD_reply_with_error (connection,
      66              :                                        MHD_HTTP_NOT_FOUND,
      67              :                                        TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
      68            8 :                                        hc->infix);
      69              :   }
      70           13 :   jcategories = json_array ();
      71           13 :   GNUNET_assert (NULL != jcategories);
      72           13 :   for (size_t i = 0; i<num_categories; i++)
      73              :   {
      74            0 :     GNUNET_assert (0 ==
      75              :                    json_array_append_new (jcategories,
      76              :                                           json_integer (categories[i])));
      77              :   }
      78           13 :   GNUNET_free (categories);
      79              : 
      80              :   {
      81              :     MHD_RESULT ret;
      82              :     int64_t total_stock_api;
      83              :     char unit_total_stock_buf[64];
      84              : 
      85           13 :     if (INT64_MAX == pd.total_stock)
      86            1 :       total_stock_api = -1;
      87              :     else
      88           12 :       total_stock_api = (int64_t) pd.total_stock;
      89              : 
      90           13 :     TMH_format_fractional_string (TMH_VK_STOCK,
      91              :                                   pd.total_stock,
      92              :                                   pd.total_stock_frac,
      93              :                                   sizeof (unit_total_stock_buf),
      94              :                                   unit_total_stock_buf);
      95              : 
      96           13 :     ret = TALER_MHD_REPLY_JSON_PACK (
      97              :       connection,
      98              :       MHD_HTTP_OK,
      99              :       GNUNET_JSON_pack_string ("product_name",
     100              :                                pd.product_name),
     101              :       GNUNET_JSON_pack_string ("description",
     102              :                                pd.description),
     103              :       GNUNET_JSON_pack_object_incref ("description_i18n",
     104              :                                       pd.description_i18n),
     105              :       GNUNET_JSON_pack_string ("unit",
     106              :                                pd.unit),
     107              :       GNUNET_JSON_pack_array_steal ("categories",
     108              :                                     jcategories),
     109              :       TALER_JSON_pack_amount ("price",
     110              :                               &pd.price),
     111              :       GNUNET_JSON_pack_allow_null (
     112              :         GNUNET_JSON_pack_string ("image",
     113              :                                  pd.image)),
     114              :       GNUNET_JSON_pack_allow_null (
     115              :         GNUNET_JSON_pack_array_incref ("taxes",
     116              :                                        pd.taxes)),
     117              :       GNUNET_JSON_pack_int64 ("total_stock",
     118              :                               total_stock_api),
     119              :       GNUNET_JSON_pack_string ("unit_total_stock",
     120              :                                unit_total_stock_buf),
     121              :       GNUNET_JSON_pack_bool ("unit_allow_fraction",
     122              :                              pd.allow_fractional_quantity),
     123              :       GNUNET_JSON_pack_uint64 ("unit_precision_level",
     124              :                                pd.fractional_precision_level),
     125              :       TALER_JSON_pack_amount_array ("unit_price",
     126              :                                     pd.price_array_length,
     127              :                                     pd.price_array),
     128              :       GNUNET_JSON_pack_uint64 ("total_sold",
     129              :                                pd.total_sold),
     130              :       GNUNET_JSON_pack_uint64 ("total_lost",
     131              :                                pd.total_lost),
     132              :       GNUNET_JSON_pack_allow_null (
     133              :         GNUNET_JSON_pack_object_incref ("address",
     134              :                                         pd.address)),
     135              :       GNUNET_JSON_pack_allow_null (
     136              :         GNUNET_JSON_pack_timestamp ("next_restock",
     137              :                                     (pd.next_restock))),
     138              :       GNUNET_JSON_pack_uint64 ("minimum_age",
     139              :                                pd.minimum_age));
     140           13 :     TALER_MERCHANTDB_product_details_free (&pd);
     141           13 :     return ret;
     142              :   }
     143              : }
     144              : 
     145              : 
     146              : /* end of taler-merchant-httpd_private-get-products-ID.c */
        

Generated by: LCOV version 2.0-1