Line data Source code
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-pots.c
20 : * @brief implementation of POST /private/pots
21 : * @author Christian Grothoff
22 : */
23 : #include "platform.h"
24 : #include "taler-merchant-httpd_post-private-pots.h"
25 : #include <taler/taler_json_lib.h>
26 : #include "merchant-database/insert_money_pot.h"
27 :
28 :
29 : enum MHD_Result
30 1 : TMH_private_post_pots (const struct TMH_RequestHandler *rh,
31 : struct MHD_Connection *connection,
32 : struct TMH_HandlerContext *hc)
33 : {
34 : const char *pot_name;
35 : const char *description;
36 : enum GNUNET_DB_QueryStatus qs;
37 : uint64_t pot_id;
38 : struct GNUNET_JSON_Specification spec[] = {
39 1 : GNUNET_JSON_spec_string ("pot_name",
40 : &pot_name),
41 1 : GNUNET_JSON_spec_string ("description",
42 : &description),
43 1 : GNUNET_JSON_spec_end ()
44 : };
45 :
46 : (void) rh;
47 : {
48 : enum GNUNET_GenericReturnValue res;
49 :
50 1 : res = TALER_MHD_parse_json_data (connection,
51 1 : hc->request_body,
52 : spec);
53 1 : if (GNUNET_OK != res)
54 : {
55 0 : GNUNET_break_op (0);
56 : return (GNUNET_NO == res)
57 : ? MHD_YES
58 0 : : MHD_NO;
59 : }
60 : }
61 :
62 1 : qs = TALER_MERCHANTDB_insert_money_pot (TMH_db,
63 1 : hc->instance->settings.id,
64 : pot_name,
65 : description,
66 : &pot_id);
67 :
68 1 : if (qs < 0)
69 : {
70 : /* NOTE: Like product groups, we cannot distinguish between a
71 : * generic DB error and a unique constraint violation on pot_name.
72 : */
73 0 : return TALER_MHD_reply_with_error (connection,
74 : MHD_HTTP_INTERNAL_SERVER_ERROR,
75 : TALER_EC_GENERIC_DB_STORE_FAILED,
76 : "insert_money_pot");
77 : }
78 1 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
79 : {
80 : /* Zero will be returned on conflict */
81 0 : return TALER_MHD_reply_with_error (
82 : connection,
83 : MHD_HTTP_CONFLICT,
84 : TALER_EC_MERCHANT_PRIVATE_MONEY_POT_CONFLICTING_NAME,
85 : pot_name);
86 : }
87 1 : return TALER_MHD_REPLY_JSON_PACK (
88 : connection,
89 : MHD_HTTP_OK,
90 : GNUNET_JSON_pack_uint64 ("pot_serial_id",
91 : pot_id));
92 : }
|