merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-httpd_private-patch-categories-ID.c (4171B)


      1 /*
      2   This file is part of TALER
      3   (C) 2024 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-patch-categories-ID.c
     22  * @brief implementing PATCH /categories/$ID request handling
     23  * @author Christian Grothoff
     24  */
     25 #include "platform.h"
     26 #include "taler-merchant-httpd_private-patch-categories-ID.h"
     27 #include "taler-merchant-httpd_helper.h"
     28 #include <taler/taler_json_lib.h>
     29 
     30 
     31 MHD_RESULT
     32 TMH_private_patch_categories_ID (const struct TMH_RequestHandler *rh,
     33                                  struct MHD_Connection *connection,
     34                                  struct TMH_HandlerContext *hc)
     35 {
     36   struct TMH_MerchantInstance *mi = hc->instance;
     37   unsigned long long cnum;
     38   char dummy;
     39   const char *category_name;
     40   const json_t *category_name_i18n;
     41   struct GNUNET_JSON_Specification spec[] = {
     42     GNUNET_JSON_spec_string ("name",
     43                              &category_name),
     44     GNUNET_JSON_spec_object_const ("name_i18n",
     45                                    &category_name_i18n),
     46     GNUNET_JSON_spec_end ()
     47   };
     48   enum GNUNET_DB_QueryStatus qs;
     49 
     50   GNUNET_assert (NULL != mi);
     51   GNUNET_assert (NULL != hc->infix);
     52   if (1 != sscanf (hc->infix,
     53                    "%llu%c",
     54                    &cnum,
     55                    &dummy))
     56   {
     57     GNUNET_break_op (0);
     58     return TALER_MHD_reply_with_error (connection,
     59                                        MHD_HTTP_BAD_REQUEST,
     60                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     61                                        "category_id must be a number");
     62   }
     63 
     64   {
     65     enum GNUNET_GenericReturnValue res;
     66 
     67     res = TALER_MHD_parse_json_data (connection,
     68                                      hc->request_body,
     69                                      spec);
     70     if (GNUNET_OK != res)
     71       return (GNUNET_NO == res)
     72              ? MHD_YES
     73              : MHD_NO;
     74   }
     75 
     76   qs = TMH_db->update_category (TMH_db->cls,
     77                                 mi->settings.id,
     78                                 cnum,
     79                                 category_name,
     80                                 category_name_i18n);
     81   {
     82     MHD_RESULT ret = MHD_NO;
     83 
     84     switch (qs)
     85     {
     86     case GNUNET_DB_STATUS_HARD_ERROR:
     87       GNUNET_break (0);
     88       ret = TALER_MHD_reply_with_error (connection,
     89                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
     90                                         TALER_EC_GENERIC_DB_STORE_FAILED,
     91                                         "update_category");
     92       break;
     93     case GNUNET_DB_STATUS_SOFT_ERROR:
     94       GNUNET_break (0);
     95       ret = TALER_MHD_reply_with_error (connection,
     96                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
     97                                         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
     98                                         "unexpected serialization problem");
     99       break;
    100     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    101       ret = TALER_MHD_reply_with_error (connection,
    102                                         MHD_HTTP_NOT_FOUND,
    103                                         TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN,
    104                                         category_name);
    105       break;
    106     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    107       ret = TALER_MHD_reply_static (connection,
    108                                     MHD_HTTP_NO_CONTENT,
    109                                     NULL,
    110                                     NULL,
    111                                     0);
    112       break;
    113     }
    114     GNUNET_JSON_parse_free (spec);
    115     return ret;
    116   }
    117 }
    118 
    119 
    120 /* end of taler-merchant-httpd_private-patch-categories-ID.c */