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 10 : TMH_private_post_products_ID_lock (
33 : const struct TMH_RequestHandler *rh,
34 : struct MHD_Connection *connection,
35 : struct TMH_HandlerContext *hc)
36 : {
37 10 : struct TMH_MerchantInstance *mi = hc->instance;
38 10 : const char *product_id = hc->infix;
39 : enum GNUNET_DB_QueryStatus qs;
40 : const char *uuids;
41 : struct GNUNET_Uuid uuid;
42 : uint32_t quantity;
43 : struct GNUNET_TIME_Relative duration;
44 : struct GNUNET_JSON_Specification spec[] = {
45 10 : GNUNET_JSON_spec_string ("lock_uuid",
46 : &uuids),
47 10 : GNUNET_JSON_spec_relative_time ("duration",
48 : &duration),
49 10 : GNUNET_JSON_spec_uint32 ("quantity",
50 : &quantity),
51 10 : GNUNET_JSON_spec_end ()
52 : };
53 :
54 10 : GNUNET_assert (NULL != mi);
55 10 : GNUNET_assert (NULL != product_id);
56 : {
57 : enum GNUNET_GenericReturnValue res;
58 :
59 10 : res = TALER_MHD_parse_json_data (connection,
60 10 : hc->request_body,
61 : spec);
62 10 : if (GNUNET_OK != res)
63 : return (GNUNET_NO == res)
64 : ? MHD_YES
65 0 : : MHD_NO;
66 : }
67 10 : TMH_uuid_from_string (uuids,
68 : &uuid);
69 10 : TMH_db->expire_locks (TMH_db->cls);
70 10 : qs = TMH_db->lock_product (TMH_db->cls,
71 10 : mi->settings.id,
72 : product_id,
73 : &uuid,
74 : quantity,
75 : GNUNET_TIME_relative_to_timestamp (duration));
76 10 : switch (qs)
77 : {
78 0 : case GNUNET_DB_STATUS_HARD_ERROR:
79 0 : return TALER_MHD_reply_with_error (
80 : connection,
81 : MHD_HTTP_INTERNAL_SERVER_ERROR,
82 : TALER_EC_GENERIC_DB_STORE_FAILED,
83 : NULL);
84 0 : case GNUNET_DB_STATUS_SOFT_ERROR:
85 0 : GNUNET_break (0);
86 0 : return TALER_MHD_reply_with_error (
87 : connection,
88 : MHD_HTTP_INTERNAL_SERVER_ERROR,
89 : TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
90 : "Serialization error for single-statment request");
91 5 : case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
92 : {
93 5 : size_t num_categories = 0;
94 5 : uint64_t *categories = NULL;
95 :
96 5 : qs = TMH_db->lookup_product (TMH_db->cls,
97 5 : mi->settings.id,
98 : product_id,
99 : NULL,
100 : &num_categories,
101 : &categories);
102 5 : if (GNUNET_DB_STATUS_HARD_ERROR == qs)
103 2 : return TALER_MHD_reply_with_error (
104 : connection,
105 : MHD_HTTP_INTERNAL_SERVER_ERROR,
106 : TALER_EC_GENERIC_DB_STORE_FAILED,
107 : "lookup_product");
108 5 : if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == qs)
109 2 : return TALER_MHD_reply_with_error (
110 : connection,
111 : MHD_HTTP_NOT_FOUND,
112 : TALER_EC_MERCHANT_GENERIC_PRODUCT_UNKNOWN,
113 : product_id);
114 3 : GNUNET_free (categories);
115 : }
116 3 : return TALER_MHD_reply_with_error (
117 : connection,
118 : MHD_HTTP_GONE,
119 : TALER_EC_MERCHANT_PRIVATE_POST_PRODUCTS_LOCK_INSUFFICIENT_STOCKS,
120 : product_id);
121 5 : case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
122 5 : return TALER_MHD_reply_static (connection,
123 : MHD_HTTP_NO_CONTENT,
124 : NULL,
125 : NULL,
126 : 0);
127 : }
128 0 : GNUNET_assert (0);
129 : return MHD_NO;
130 : }
131 :
132 :
133 : /* end of taler-merchant-httpd_private-patch-products-ID-lock.c */
|