LCOV - code coverage report
Current view: top level - backend - taler-merchant-httpd_private-post-products-ID-lock.c (source / functions) Hit Total Coverage
Test: GNU Taler merchant coverage report Lines: 26 34 76.5 %
Date: 2021-08-30 06:54:17 Functions: 1 1 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
       6             :   it under the terms of the GNU Affero General Public License as
       7             :   published by the Free Software Foundation; either version 3,
       8             :   or (at your option) any later version.
       9             : 
      10             :   TALER is distributed in the hope that it will be useful, but
      11             :   WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :   GNU General Public License for more details.
      14             : 
      15             :   You should have received a copy of the GNU General Public
      16             :   License along with TALER; see the file COPYING.  If not,
      17             :   see <http://www.gnu.org/licenses/>
      18             : */
      19             : 
      20             : /**
      21             :  * @file taler-merchant-httpd_private-post-products-ID-lock.c
      22             :  * @brief implementing POST /products/$ID/lock request handling
      23             :  * @author Christian Grothoff
      24             :  */
      25             : #include "platform.h"
      26             : #include "taler-merchant-httpd_private-post-products-ID-lock.h"
      27             : #include "taler-merchant-httpd_helper.h"
      28             : #include <taler/taler_json_lib.h>
      29             : 
      30             : 
      31             : MHD_RESULT
      32           4 : TMH_private_post_products_ID_lock (const struct TMH_RequestHandler *rh,
      33             :                                    struct MHD_Connection *connection,
      34             :                                    struct TMH_HandlerContext *hc)
      35             : {
      36           4 :   struct TMH_MerchantInstance *mi = hc->instance;
      37           4 :   const char *product_id = hc->infix;
      38             :   enum GNUNET_DB_QueryStatus qs;
      39             :   const char *uuids;
      40             :   struct GNUNET_Uuid uuid;
      41             :   uint32_t quantity;
      42             :   struct GNUNET_TIME_Relative duration;
      43             :   struct GNUNET_JSON_Specification spec[] = {
      44           4 :     GNUNET_JSON_spec_string ("lock_uuid",
      45             :                              &uuids),
      46           4 :     TALER_JSON_spec_relative_time ("duration",
      47             :                                    &duration),
      48           4 :     GNUNET_JSON_spec_uint32 ("quantity",
      49             :                              &quantity),
      50           4 :     GNUNET_JSON_spec_end ()
      51             :   };
      52             : 
      53           4 :   GNUNET_assert (NULL != mi);
      54           4 :   GNUNET_assert (NULL != product_id);
      55             :   {
      56             :     enum GNUNET_GenericReturnValue res;
      57             : 
      58           4 :     res = TALER_MHD_parse_json_data (connection,
      59           4 :                                      hc->request_body,
      60             :                                      spec);
      61           4 :     if (GNUNET_OK != res)
      62             :       return (GNUNET_NO == res)
      63             :              ? MHD_YES
      64           0 :              : MHD_NO;
      65             :   }
      66           4 :   TMH_uuid_from_string (uuids,
      67             :                         &uuid);
      68           4 :   TMH_db->expire_locks (TMH_db->cls);
      69           4 :   qs = TMH_db->lock_product (TMH_db->cls,
      70           4 :                              mi->settings.id,
      71             :                              product_id,
      72             :                              &uuid,
      73             :                              quantity,
      74             :                              GNUNET_TIME_relative_to_absolute (duration));
      75           4 :   switch (qs)
      76             :   {
      77           0 :   case GNUNET_DB_STATUS_HARD_ERROR:
      78           0 :     return TALER_MHD_reply_with_error (connection,
      79             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      80             :                                        TALER_EC_GENERIC_DB_STORE_FAILED,
      81             :                                        NULL);
      82           0 :   case GNUNET_DB_STATUS_SOFT_ERROR:
      83           0 :     GNUNET_break (0);
      84           0 :     return TALER_MHD_reply_with_error (connection,
      85             :                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
      86             :                                        TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
      87             :                                        "Serialization error for single-statment request");
      88           2 :   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
      89           2 :     qs = TMH_db->lookup_product (TMH_db->cls,
      90           2 :                                  mi->settings.id,
      91             :                                  product_id,
      92             :                                  NULL);
      93           2 :     if (GNUNET_DB_STATUS_HARD_ERROR == qs)
      94           0 :       return TALER_MHD_reply_with_error (connection,
      95             :                                          MHD_HTTP_INTERNAL_SERVER_ERROR,
      96             :                                          TALER_EC_GENERIC_DB_STORE_FAILED,
      97             :                                          "lookup_product");
      98           2 :     if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
      99           1 :       return TALER_MHD_reply_with_error (connection,
     100             :                                          MHD_HTTP_NOT_FOUND,
     101             :                                          TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
     102             :                                          product_id);
     103           1 :     return TALER_MHD_reply_with_error (connection,
     104             :                                        MHD_HTTP_GONE,
     105             :                                        TALER_EC_MERCHANT_PRIVATE_POST_PRODUCTS_LOCK_INSUFFICIENT_STOCKS,
     106             :                                        product_id);
     107           2 :   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
     108           2 :     return TALER_MHD_reply_static (connection,
     109             :                                    MHD_HTTP_NO_CONTENT,
     110             :                                    NULL,
     111             :                                    NULL,
     112             :                                    0);
     113             :   }
     114           0 :   GNUNET_assert (0);
     115             :   return MHD_NO;
     116             : }
     117             : 
     118             : 
     119             : /* end of taler-merchant-httpd_private-patch-products-ID-lock.c */

Generated by: LCOV version 1.14