LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-products-ID.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 22 25 88.0 %
Date: 2025-06-23 16:22:09 Functions: 1 1 100.0 %

          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             : 
      25             : 
      26             : /**
      27             :  * Handle a GET "/products/$ID" request.
      28             :  *
      29             :  * @param rh context of the handler
      30             :  * @param connection the MHD connection to handle
      31             :  * @param[in,out] hc context with further information about the request
      32             :  * @return MHD result code
      33             :  */
      34             : MHD_RESULT
      35          13 : TMH_private_get_products_ID (
      36             :   const struct TMH_RequestHandler *rh,
      37             :   struct MHD_Connection *connection,
      38             :   struct TMH_HandlerContext *hc)
      39             : {
      40          13 :   struct TMH_MerchantInstance *mi = hc->instance;
      41          13 :   struct TALER_MERCHANTDB_ProductDetails pd = { 0 };
      42             :   enum GNUNET_DB_QueryStatus qs;
      43          13 :   size_t num_categories = 0;
      44          13 :   uint64_t *categories = NULL;
      45             :   json_t *jcategories;
      46             : 
      47          13 :   GNUNET_assert (NULL != mi);
      48          13 :   qs = TMH_db->lookup_product (TMH_db->cls,
      49          13 :                                mi->settings.id,
      50          13 :                                hc->infix,
      51             :                                &pd,
      52             :                                &num_categories,
      53             :                                &categories);
      54          13 :   if (0 > qs)
      55             :   {
      56           0 :     GNUNET_break (0);
      57           0 :     return TALER_MHD_reply_with_error (connection,
      58             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      59             :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
      60             :                                        "lookup_product");
      61             :   }
      62          13 :   if (0 == qs)
      63             :   {
      64           8 :     return TALER_MHD_reply_with_error (connection,
      65             :                                        MHD_HTTP_NOT_FOUND,
      66             :                                        TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
      67           8 :                                        hc->infix);
      68             :   }
      69           5 :   jcategories = json_array ();
      70           5 :   GNUNET_assert (NULL != jcategories);
      71           5 :   for (size_t i = 0; i<num_categories; i++)
      72             :   {
      73           0 :     GNUNET_assert (0 ==
      74             :                    json_array_append_new (jcategories,
      75             :                                           json_integer (categories[i])));
      76             :   }
      77           5 :   GNUNET_free (categories);
      78             : 
      79             :   {
      80             :     MHD_RESULT ret;
      81             : 
      82           5 :     ret = TALER_MHD_REPLY_JSON_PACK (
      83             :       connection,
      84             :       MHD_HTTP_OK,
      85             :       GNUNET_JSON_pack_string ("description",
      86             :                                pd.description),
      87             :       GNUNET_JSON_pack_object_steal ("description_i18n",
      88             :                                      pd.description_i18n),
      89             :       GNUNET_JSON_pack_string ("unit",
      90             :                                pd.unit),
      91             :       GNUNET_JSON_pack_array_steal ("categories",
      92             :                                     jcategories),
      93             :       TALER_JSON_pack_amount ("price",
      94             :                               &pd.price),
      95             :       GNUNET_JSON_pack_allow_null (
      96             :         GNUNET_JSON_pack_string ("image",
      97             :                                  pd.image)),
      98             :       GNUNET_JSON_pack_allow_null (
      99             :         GNUNET_JSON_pack_array_steal ("taxes",
     100             :                                       pd.taxes)),
     101             :       GNUNET_JSON_pack_int64 ("total_stock",
     102             :                               (INT64_MAX == pd.total_stock)
     103             :                               ? -1LL
     104             :                               : pd.total_stock),
     105             :       GNUNET_JSON_pack_uint64 ("total_sold",
     106             :                                pd.total_sold),
     107             :       GNUNET_JSON_pack_uint64 ("total_lost",
     108             :                                pd.total_lost),
     109             :       GNUNET_JSON_pack_allow_null (
     110             :         GNUNET_JSON_pack_object_steal ("address",
     111             :                                        pd.address)),
     112             :       GNUNET_JSON_pack_allow_null (
     113             :         GNUNET_JSON_pack_timestamp ("next_restock",
     114             :                                     (pd.next_restock))),
     115             :       GNUNET_JSON_pack_uint64 ("minimum_age",
     116             :                                pd.minimum_age));
     117           5 :     GNUNET_free (pd.description);
     118           5 :     GNUNET_free (pd.image);
     119           5 :     GNUNET_free (pd.unit);
     120           5 :     return ret;
     121             :   }
     122             : }
     123             : 
     124             : 
     125             : /* end of taler-merchant-httpd_private-get-products-ID.c */

Generated by: LCOV version 1.16