LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_get-private-products-PRODUCT_ID.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.6 % 27 25
Test Date: 2026-09-04 23:42:01 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 src/backend/taler-merchant-httpd_get-private-products-PRODUCT_ID.c
      18              :  * @brief implement GET /products/$ID
      19              :  * @author Christian Grothoff
      20              :  */
      21              : #include "platform.h"
      22              : #include "taler-merchant-httpd_get-private-products-PRODUCT_ID.h"
      23              : #include <taler/taler_json_lib.h>
      24              : #include "taler-merchant-httpd_helper.h"
      25              : #include <taler/taler_util.h>
      26              : #include "merchant-database/get_product.h"
      27              : 
      28              : /**
      29              :  * Handle a GET "/products/$ID" request.
      30              :  *
      31              :  * @param rh context of the handler
      32              :  * @param connection the MHD connection to handle
      33              :  * @param[in,out] hc context with further information about the request
      34              :  * @return MHD result code
      35              :  */
      36              : enum MHD_Result
      37           29 : TMH_private_get_products_ID (
      38              :   const struct TMH_RequestHandler *rh,
      39              :   struct MHD_Connection *connection,
      40              :   struct TMH_HandlerContext *hc)
      41              : {
      42           29 :   struct TMH_MerchantInstance *mi = hc->instance;
      43           29 :   struct TALER_MERCHANTDB_ProductDetails pd = { 0 };
      44              :   enum GNUNET_DB_QueryStatus qs;
      45           29 :   size_t num_categories = 0;
      46           29 :   uint64_t *categories = NULL;
      47              :   json_t *jcategories;
      48              : 
      49           29 :   GNUNET_assert (NULL != mi);
      50           29 :   qs = TALER_MERCHANTDB_get_product (TMH_db,
      51           29 :                                      mi->settings.id,
      52           29 :                                      hc->infix,
      53              :                                      &pd,
      54              :                                      &num_categories,
      55              :                                      &categories);
      56           29 :   if (0 > qs)
      57              :   {
      58            0 :     GNUNET_break (0);
      59            0 :     return TALER_MHD_reply_with_error (connection,
      60              :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      61              :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
      62              :                                        "get_product");
      63              :   }
      64           29 :   if (0 == qs)
      65              :   {
      66           10 :     return TALER_MHD_reply_with_error (connection,
      67              :                                        MHD_HTTP_NOT_FOUND,
      68              :                                        TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
      69           10 :                                        hc->infix);
      70              :   }
      71           19 :   jcategories = json_array ();
      72           19 :   GNUNET_assert (NULL != jcategories);
      73           25 :   for (size_t i = 0; i<num_categories; i++)
      74              :   {
      75            6 :     GNUNET_assert (0 ==
      76              :                    json_array_append_new (jcategories,
      77              :                                           json_integer (categories[i])));
      78              :   }
      79           19 :   GNUNET_free (categories);
      80              : 
      81              :   {
      82              :     enum MHD_Result ret;
      83              :     int64_t total_stock_api;
      84              :     char unit_total_stock_buf[64];
      85              : 
      86           19 :     if (INT64_MAX == pd.total_stock)
      87            1 :       total_stock_api = -1;
      88              :     else
      89           18 :       total_stock_api = (int64_t) pd.total_stock;
      90              : 
      91           19 :     TALER_MERCHANT_vk_format_fractional_string (
      92              :       TALER_MERCHANT_VK_STOCK,
      93              :       pd.total_stock,
      94              :       pd.total_stock_frac,
      95              :       sizeof (unit_total_stock_buf),
      96              :       unit_total_stock_buf);
      97              : 
      98           19 :     ret = TALER_MHD_REPLY_JSON_PACK (
      99              :       connection,
     100              :       MHD_HTTP_OK,
     101              :       GNUNET_JSON_pack_string ("product_name",
     102              :                                pd.product_name),
     103              :       GNUNET_JSON_pack_string ("description",
     104              :                                pd.description),
     105              :       GNUNET_JSON_pack_object_incref ("description_i18n",
     106              :                                       pd.description_i18n),
     107              :       GNUNET_JSON_pack_string ("unit",
     108              :                                pd.unit),
     109              :       GNUNET_JSON_pack_array_steal ("categories",
     110              :                                     jcategories),
     111              :       // Note: deprecated field
     112              :       GNUNET_JSON_pack_allow_null (
     113              :         TALER_JSON_pack_amount ("price",
     114              :                                 (0 == pd.price_array_length)
     115              :                                 ? NULL
     116              :                                 : &pd.price_array[0])),
     117              :       GNUNET_JSON_pack_allow_null (
     118              :         GNUNET_JSON_pack_string ("image",
     119              :                                  pd.image)),
     120              :       GNUNET_JSON_pack_allow_null (
     121              :         GNUNET_JSON_pack_array_incref ("taxes",
     122              :                                        pd.taxes)),
     123              :       GNUNET_JSON_pack_int64 ("total_stock",
     124              :                               total_stock_api),
     125              :       GNUNET_JSON_pack_string ("unit_total_stock",
     126              :                                unit_total_stock_buf),
     127              :       GNUNET_JSON_pack_bool ("unit_allow_fraction",
     128              :                              pd.allow_fractional_quantity),
     129              :       GNUNET_JSON_pack_uint64 ("unit_precision_level",
     130              :                                pd.fractional_precision_level),
     131              :       TALER_JSON_pack_amount_array ("unit_price",
     132              :                                     pd.price_array_length,
     133              :                                     pd.price_array),
     134              :       GNUNET_JSON_pack_uint64 ("total_sold",
     135              :                                pd.total_sold),
     136              :       GNUNET_JSON_pack_uint64 ("total_lost",
     137              :                                pd.total_lost),
     138              :       GNUNET_JSON_pack_allow_null (
     139              :         GNUNET_JSON_pack_object_incref ("address",
     140              :                                         pd.address)),
     141              :       GNUNET_JSON_pack_allow_null (
     142              :         GNUNET_JSON_pack_timestamp ("next_restock",
     143              :                                     (pd.next_restock))),
     144              :       GNUNET_JSON_pack_uint64 ("product_group_id",
     145              :                                pd.product_group_id),
     146              :       GNUNET_JSON_pack_uint64 ("money_pot_id",
     147              :                                pd.money_pot_id),
     148              :       GNUNET_JSON_pack_bool ("price_is_net",
     149              :                              pd.price_is_net),
     150              :       GNUNET_JSON_pack_uint64 ("minimum_age",
     151              :                                pd.minimum_age));
     152           19 :     TALER_MERCHANTDB_product_details_free (&pd);
     153           19 :     return ret;
     154              :   }
     155              : }
     156              : 
     157              : 
     158              : /* end of taler-merchant-httpd_get-private-products-PRODUCT_ID.c */
        

Generated by: LCOV version 2.0-1