merchant

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

taler-merchant-httpd_patch-private-categories-CATEGORY_ID.c (4947B)


      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 src/backend/taler-merchant-httpd_patch-private-categories-CATEGORY_ID.c
     22  * @brief implementing PATCH /categories/$ID request handling
     23  * @author Christian Grothoff
     24  */
     25 #include "platform.h"
     26 #include "taler-merchant-httpd_patch-private-categories-CATEGORY_ID.h"
     27 #include "taler-merchant-httpd_helper.h"
     28 #include <taler/taler_json_lib.h>
     29 #include "merchant-database/update_category.h"
     30 
     31 
     32 enum MHD_Result
     33 TMH_private_patch_categories_ID (const struct TMH_RequestHandler *rh,
     34                                  struct MHD_Connection *connection,
     35                                  struct TMH_HandlerContext *hc)
     36 {
     37   struct TMH_MerchantInstance *mi = hc->instance;
     38   unsigned long long cnum;
     39   char dummy;
     40   const char *category_name;
     41   json_t *oempty = NULL;
     42   const json_t *category_name_i18n = NULL;
     43   struct GNUNET_JSON_Specification spec[] = {
     44     GNUNET_JSON_spec_string ("name",
     45                              &category_name),
     46     GNUNET_JSON_spec_mark_optional (
     47       GNUNET_JSON_spec_object_const ("name_i18n",
     48                                      &category_name_i18n),
     49       NULL),
     50     GNUNET_JSON_spec_end ()
     51   };
     52   enum GNUNET_DB_QueryStatus qs;
     53   bool conflict = false;
     54 
     55   GNUNET_assert (NULL != mi);
     56   GNUNET_assert (NULL != hc->infix);
     57   if (1 != sscanf (hc->infix,
     58                    "%llu%c",
     59                    &cnum,
     60                    &dummy))
     61   {
     62     GNUNET_break_op (0);
     63     return TALER_MHD_reply_with_error (connection,
     64                                        MHD_HTTP_BAD_REQUEST,
     65                                        TALER_EC_GENERIC_PARAMETER_MALFORMED,
     66                                        "category_id must be a number");
     67   }
     68 
     69   {
     70     enum GNUNET_GenericReturnValue res;
     71 
     72     res = TALER_MHD_parse_json_data (connection,
     73                                      hc->request_body,
     74                                      spec);
     75     if (GNUNET_OK != res)
     76       return (GNUNET_NO == res)
     77              ? MHD_YES
     78              : MHD_NO;
     79   }
     80   if (NULL == category_name_i18n)
     81   {
     82     /* NULL is not allowed in the DB, substitute with empty object */
     83     oempty = json_object ();
     84     category_name_i18n = oempty;
     85   }
     86 
     87   qs = TALER_MERCHANTDB_update_category (TMH_db,
     88                                          mi->settings.id,
     89                                          cnum,
     90                                          category_name,
     91                                          category_name_i18n,
     92                                          &conflict);
     93   json_decref (oempty);
     94   if (conflict)
     95   {
     96     enum MHD_Result ret;
     97 
     98     ret = TALER_MHD_reply_with_error (
     99       connection,
    100       MHD_HTTP_CONFLICT,
    101       TALER_EC_MERCHANT_PRIVATE_POST_CATEGORIES_CONFLICT_CATEGORY_EXISTS,
    102       category_name);
    103     GNUNET_JSON_parse_free (spec);
    104     return ret;
    105   }
    106   {
    107     enum MHD_Result ret = MHD_NO;
    108 
    109     switch (qs)
    110     {
    111     case GNUNET_DB_STATUS_HARD_ERROR:
    112       GNUNET_break (0);
    113       ret = TALER_MHD_reply_with_error (connection,
    114                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
    115                                         TALER_EC_GENERIC_DB_STORE_FAILED,
    116                                         "update_category");
    117       break;
    118     case GNUNET_DB_STATUS_SOFT_ERROR:
    119       GNUNET_break (0);
    120       ret = TALER_MHD_reply_with_error (connection,
    121                                         MHD_HTTP_INTERNAL_SERVER_ERROR,
    122                                         TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
    123                                         "unexpected serialization problem");
    124       break;
    125     case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    126       ret = TALER_MHD_reply_with_error (connection,
    127                                         MHD_HTTP_NOT_FOUND,
    128                                         TALER_EC_MERCHANT_GENERIC_CATEGORY_UNKNOWN,
    129                                         category_name);
    130       break;
    131     case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    132       ret = TALER_MHD_reply_static (connection,
    133                                     MHD_HTTP_NO_CONTENT,
    134                                     NULL,
    135                                     NULL,
    136                                     0);
    137       break;
    138     }
    139     GNUNET_JSON_parse_free (spec);
    140     return ret;
    141   }
    142 }
    143 
    144 
    145 /* end of taler-merchant-httpd_patch-private-categories-CATEGORY_ID.c */