LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-get-tips.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 25 32 78.1 %
Date: 2021-08-30 06:54:17 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   This file is part of TALER
       3             :   (C) 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 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-tips.c
      18             :  * @brief implementation of a GET /private/tips handler
      19             :  * @author Jonathan Buchanan
      20             :  */
      21             : #include "platform.h"
      22             : #include "taler-merchant-httpd_private-get-tips.h"
      23             : #include <taler/taler_json_lib.h>
      24             : 
      25             : /**
      26             :  * Add tip details to our JSON array.
      27             :  *
      28             :  * @param[in,out] cls a `json_t *` JSON array to build
      29             :  * @param row_id row number of the tip
      30             :  * @param tip_id ID of the tip
      31             :  * @param amount the amount of the tip
      32             :  */
      33             : static void
      34           5 : add_tip (void *cls,
      35             :          uint64_t row_id,
      36             :          struct GNUNET_HashCode tip_id,
      37             :          struct TALER_Amount amount)
      38             : {
      39           5 :   json_t *pa = cls;
      40             : 
      41           5 :   GNUNET_assert (0 ==
      42             :                  json_array_append_new (
      43             :                    pa,
      44             :                    GNUNET_JSON_PACK (
      45             :                      GNUNET_JSON_pack_uint64 ("row_id",
      46             :                                               row_id),
      47             :                      GNUNET_JSON_pack_data_auto ("tip_id",
      48             :                                                  &tip_id),
      49             :                      TALER_JSON_pack_amount ("tip_amount",
      50             :                                              &amount))));
      51           5 : }
      52             : 
      53             : 
      54             : MHD_RESULT
      55           3 : TMH_private_get_tips (const struct TMH_RequestHandler *rh,
      56             :                       struct MHD_Connection *connection,
      57             :                       struct TMH_HandlerContext *hc)
      58             : {
      59             :   json_t *pa;
      60             :   enum GNUNET_DB_QueryStatus qs;
      61             :   enum TALER_EXCHANGE_YesNoAll expired;
      62             :   uint64_t offset;
      63             :   int64_t limit;
      64             : 
      65           3 :   if (! (TALER_arg_to_yna (connection,
      66             :                            "expired",
      67             :                            TALER_EXCHANGE_YNA_NO,
      68             :                            &expired)) )
      69           0 :     return TALER_MHD_reply_with_error (connection,
      70             :                                        MHD_HTTP_BAD_REQUEST,
      71             :                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
      72             :                                        "expired");
      73             :   {
      74             :     const char *limit_str;
      75             : 
      76           3 :     limit_str = MHD_lookup_connection_value (connection,
      77             :                                              MHD_GET_ARGUMENT_KIND,
      78             :                                              "limit");
      79           3 :     if (NULL == limit_str)
      80             :     {
      81           1 :       limit = -20;
      82             :     }
      83             :     else
      84             :     {
      85             :       char dummy[2];
      86             :       long long ll;
      87             : 
      88           2 :       if (1 !=
      89           2 :           sscanf (limit_str,
      90             :                   "%lld%1s",
      91             :                   &ll,
      92             :                   dummy))
      93           0 :         return TALER_MHD_reply_with_error (connection,
      94             :                                            MHD_HTTP_BAD_REQUEST,
      95             :                                            TALER_EC_GENERIC_PARAMETER_MALFORMED,
      96             :                                            "limit");
      97           2 :       limit = (uint64_t) ll;
      98             :     }
      99             :   }
     100             :   {
     101             :     const char *offset_str;
     102             : 
     103           3 :     offset_str = MHD_lookup_connection_value (connection,
     104             :                                               MHD_GET_ARGUMENT_KIND,
     105             :                                               "offset");
     106           3 :     if (NULL == offset_str)
     107             :     {
     108           1 :       if (limit > 0)
     109           1 :         offset = 0;
     110             :       else
     111           0 :         offset = INT64_MAX;
     112             :     }
     113             :     else
     114             :     {
     115             :       char dummy[2];
     116             :       unsigned long long ull;
     117             : 
     118           2 :       if (1 !=
     119           2 :           sscanf (offset_str,
     120             :                   "%llu%1s",
     121             :                   &ull,
     122             :                   dummy))
     123           0 :         return TALER_MHD_reply_with_error (connection,
     124             :                                            MHD_HTTP_BAD_REQUEST,
     125             :                                            TALER_EC_GENERIC_PARAMETER_MALFORMED,
     126             :                                            "offset");
     127           2 :       offset = (uint64_t) ull;
     128             :     }
     129             :   }
     130             : 
     131           3 :   pa = json_array ();
     132           3 :   GNUNET_assert (NULL != pa);
     133           3 :   qs = TMH_db->lookup_tips (TMH_db->cls,
     134           3 :                             hc->instance->settings.id,
     135             :                             expired,
     136             :                             limit,
     137             :                             offset,
     138             :                             &add_tip,
     139             :                             pa);
     140             : 
     141           3 :   if (0 > qs)
     142             :   {
     143           0 :     GNUNET_break (0);
     144           0 :     json_decref (pa);
     145           0 :     return TALER_MHD_reply_with_error (connection,
     146             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     147             :                                        TALER_EC_GENERIC_DB_FETCH_FAILED,
     148             :                                        "tips");
     149             :   }
     150             : 
     151           3 :   return TALER_MHD_REPLY_JSON_PACK (
     152             :     connection,
     153             :     MHD_HTTP_OK,
     154             :     GNUNET_JSON_pack_array_steal ("tips",
     155             :                                   pa));
     156             : }

Generated by: LCOV version 1.14