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 0 : TMH_private_post_products_ID_lock (const struct TMH_RequestHandler *rh,
33 : struct MHD_Connection *connection,
34 : struct TMH_HandlerContext *hc)
35 : {
36 0 : struct TMH_MerchantInstance *mi = hc->instance;
37 0 : 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 0 : GNUNET_JSON_spec_string ("lock_uuid",
45 : &uuids),
46 0 : GNUNET_JSON_spec_relative_time ("duration",
47 : &duration),
48 0 : GNUNET_JSON_spec_uint32 ("quantity",
49 : &quantity),
50 0 : GNUNET_JSON_spec_end ()
51 : };
52 :
53 0 : GNUNET_assert (NULL != mi);
54 0 : GNUNET_assert (NULL != product_id);
55 : {
56 : enum GNUNET_GenericReturnValue res;
57 :
58 0 : res = TALER_MHD_parse_json_data (connection,
59 0 : hc->request_body,
60 : spec);
61 0 : if (GNUNET_OK != res)
62 : return (GNUNET_NO == res)
63 : ? MHD_YES
64 0 : : MHD_NO;
65 : }
66 0 : TMH_uuid_from_string (uuids,
67 : &uuid);
68 0 : TMH_db->expire_locks (TMH_db->cls);
69 0 : qs = TMH_db->lock_product (TMH_db->cls,
70 0 : mi->settings.id,
71 : product_id,
72 : &uuid,
73 : quantity,
74 : GNUNET_TIME_relative_to_timestamp (duration));
75 0 : 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 0 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
89 0 : qs = TMH_db->lookup_product (TMH_db->cls,
90 0 : mi->settings.id,
91 : product_id,
92 : NULL);
93 0 : 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 0 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
99 0 : return TALER_MHD_reply_with_error (connection,
100 : MHD_HTTP_NOT_FOUND,
101 : TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
102 : product_id);
103 0 : 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 0 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
108 0 : 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 */
|