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

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 2019, 2020, 2021, 2024 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.c
      18             :  * @brief implement GET /products
      19             :  * @author Christian Grothoff
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler-merchant-httpd_private-get-products.h"
      23             : 
      24             : 
      25             : /**
      26             :  * Add product details to our JSON array.
      27             :  *
      28             :  * @param cls a `json_t *` JSON array to build
      29             :  * @param product_serial serial (row) number of the product in the database
      30             :  * @param product_id ID of the product
      31             :  */
      32             : static void
      33           2 : add_product (void *cls,
      34             :              uint64_t product_serial,
      35             :              const char *product_id)
      36             : {
      37           2 :   json_t *pa = cls;
      38             : 
      39           2 :   GNUNET_assert (0 ==
      40             :                  json_array_append_new (
      41             :                    pa,
      42             :                    GNUNET_JSON_PACK (
      43             :                      GNUNET_JSON_pack_uint64 ("product_serial",
      44             :                                               product_serial),
      45             :                      GNUNET_JSON_pack_string ("product_id",
      46             :                                               product_id))));
      47           2 : }
      48             : 
      49             : 
      50             : MHD_RESULT
      51          11 : TMH_private_get_products (const struct TMH_RequestHandler *rh,
      52             :                           struct MHD_Connection *connection,
      53             :                           struct TMH_HandlerContext *hc)
      54             : {
      55             :   json_t *pa;
      56             :   enum GNUNET_DB_QueryStatus qs;
      57             :   int64_t limit;
      58             :   uint64_t offset;
      59             : 
      60          11 :   limit = 20; /* default */
      61          11 :   TALER_MHD_parse_request_snumber (connection,
      62             :                                    "limit",
      63             :                                    &limit);
      64          11 :   if (limit < 0)
      65           0 :     offset = INT64_MAX;
      66             :   else
      67          11 :     offset = 0;
      68          11 :   TALER_MHD_parse_request_number (connection,
      69             :                                   "offset",
      70             :                                   &offset);
      71          11 :   pa = json_array ();
      72          11 :   GNUNET_assert (NULL != pa);
      73          11 :   qs = TMH_db->lookup_products (TMH_db->cls,
      74          11 :                                 hc->instance->settings.id,
      75             :                                 offset,
      76             :                                 limit,
      77             :                                 &add_product,
      78             :                                 pa);
      79          11 :   if (0 > qs)
      80             :   {
      81           0 :     GNUNET_break (0);
      82           0 :     json_decref (pa);
      83           0 :     return TALER_MHD_reply_with_error (connection,
      84             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      85             :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
      86             :                                        NULL);
      87             :   }
      88          11 :   return TALER_MHD_REPLY_JSON_PACK (connection,
      89             :                                     MHD_HTTP_OK,
      90             :                                     GNUNET_JSON_pack_array_steal ("products",
      91             :                                                                   pa));
      92             : }
      93             : 
      94             : 
      95             : /* end of taler-merchant-httpd_private-get-products.c */

Generated by: LCOV version 1.16