taler-merchant-httpd_post-private-groups.c (3054B)
1 /* 2 This file is part of TALER 3 (C) 2025 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 Affero General Public License for more 12 details. 13 14 You should have received a copy of the GNU Affero General Public License 15 along with TALER; see the file COPYING. If not, see 16 <http://www.gnu.org/licenses/> 17 */ 18 /** 19 * @file src/backend/taler-merchant-httpd_post-private-groups.c 20 * @brief implementation of POST /private/groups 21 * @author Christian Grothoff 22 */ 23 #include "platform.h" 24 #include "taler-merchant-httpd_post-private-groups.h" 25 #include <taler/taler_json_lib.h> 26 #include "merchant-database/insert_product_group.h" 27 28 29 enum MHD_Result 30 TMH_private_post_groups (const struct TMH_RequestHandler *rh, 31 struct MHD_Connection *connection, 32 struct TMH_HandlerContext *hc) 33 { 34 const char *group_name; 35 const char *description; 36 enum GNUNET_DB_QueryStatus qs; 37 uint64_t group_id; 38 struct GNUNET_JSON_Specification spec[] = { 39 GNUNET_JSON_spec_string ("group_name", 40 &group_name), 41 GNUNET_JSON_spec_string ("description", 42 &description), 43 GNUNET_JSON_spec_end () 44 }; 45 46 (void) rh; 47 { 48 enum GNUNET_GenericReturnValue res; 49 50 res = TALER_MHD_parse_json_data (connection, 51 hc->request_body, 52 spec); 53 if (GNUNET_OK != res) 54 { 55 GNUNET_break_op (0); 56 return (GNUNET_NO == res) 57 ? MHD_YES 58 : MHD_NO; 59 } 60 } 61 62 qs = TALER_MERCHANTDB_insert_product_group (TMH_db, 63 hc->instance->settings.id, 64 group_name, 65 description, 66 &group_id); 67 if (qs < 0) 68 { 69 GNUNET_break (0); 70 return TALER_MHD_reply_with_error (connection, 71 MHD_HTTP_INTERNAL_SERVER_ERROR, 72 TALER_EC_GENERIC_DB_STORE_FAILED, 73 "insert_product_group"); 74 } 75 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs) 76 { 77 /* Zero will be returned on conflict */ 78 return TALER_MHD_reply_with_error (connection, 79 MHD_HTTP_CONFLICT, 80 TALER_EC_MERCHANT_PRIVATE_PRODUCT_GROUP_CONFLICTING_NAME, 81 group_name); 82 } 83 84 return TALER_MHD_REPLY_JSON_PACK ( 85 connection, 86 MHD_HTTP_OK, 87 GNUNET_JSON_pack_uint64 ("group_serial_id", 88 group_id)); 89 }