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: 87.0 % 23 20
Test Date: 2025-10-31 14:20:14 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              : 
      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 ("product_name",
      86              :                                pd.product_name),
      87              :       GNUNET_JSON_pack_string ("description",
      88              :                                pd.description),
      89              :       GNUNET_JSON_pack_object_incref ("description_i18n",
      90              :                                       pd.description_i18n),
      91              :       GNUNET_JSON_pack_string ("unit",
      92              :                                pd.unit),
      93              :       GNUNET_JSON_pack_array_steal ("categories",
      94              :                                     jcategories),
      95              :       TALER_JSON_pack_amount ("price",
      96              :                               &pd.price),
      97              :       GNUNET_JSON_pack_allow_null (
      98              :         GNUNET_JSON_pack_string ("image",
      99              :                                  pd.image)),
     100              :       GNUNET_JSON_pack_allow_null (
     101              :         GNUNET_JSON_pack_array_incref ("taxes",
     102              :                                        pd.taxes)),
     103              :       GNUNET_JSON_pack_int64 ("total_stock",
     104              :                               (INT64_MAX == pd.total_stock)
     105              :                               ? -1LL
     106              :                               : pd.total_stock),
     107              :       GNUNET_JSON_pack_uint64 ("total_sold",
     108              :                                pd.total_sold),
     109              :       GNUNET_JSON_pack_uint64 ("total_lost",
     110              :                                pd.total_lost),
     111              :       GNUNET_JSON_pack_allow_null (
     112              :         GNUNET_JSON_pack_object_incref ("address",
     113              :                                         pd.address)),
     114              :       GNUNET_JSON_pack_allow_null (
     115              :         GNUNET_JSON_pack_timestamp ("next_restock",
     116              :                                     (pd.next_restock))),
     117              :       GNUNET_JSON_pack_uint64 ("minimum_age",
     118              :                                pd.minimum_age));
     119            5 :     TALER_MERCHANTDB_product_details_free (&pd);
     120            5 :     return ret;
     121              :   }
     122              : }
     123              : 
     124              : 
     125              : /* end of taler-merchant-httpd_private-get-products-ID.c */
        

Generated by: LCOV version 2.0-1