summaryrefslogtreecommitdiff
path: root/src/bank-lib/fakebank.c
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2018-12-14 00:34:35 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2018-12-14 19:25:42 +0100
commit15c1a877c617fd29ff61e2ebd1f6fac72e54afce (patch)
tree2781d327ca51bce8c172f0907bb93c86cc13d579 /src/bank-lib/fakebank.c
parentc2b6a1065474df88df8d501d7a83b67032bd121d (diff)
downloadexchange-15c1a877c617fd29ff61e2ebd1f6fac72e54afce.tar.gz
exchange-15c1a877c617fd29ff61e2ebd1f6fac72e54afce.tar.bz2
exchange-15c1a877c617fd29ff61e2ebd1f6fac72e54afce.zip
/history API semantics extended.
That API has now a new 'ordering' flag that lets the client choose what ordering the results should have: ascending or descending. In particular, this change adapted the fakebank and tests logic to such introduction.
Diffstat (limited to 'src/bank-lib/fakebank.c')
-rw-r--r--src/bank-lib/fakebank.c78
1 files changed, 68 insertions, 10 deletions
diff --git a/src/bank-lib/fakebank.c b/src/bank-lib/fakebank.c
index 82c5a6cab..52f0d73a9 100644
--- a/src/bank-lib/fakebank.c
+++ b/src/bank-lib/fakebank.c
@@ -96,6 +96,30 @@ struct Transaction
/**
+ * Needed to implement ascending/descending ordering
+ * of /history results.
+ */
+struct HistoryElement
+{
+
+ /**
+ * History JSON element.
+ */
+ json_t *element;
+
+ /**
+ * Previous element.
+ */
+ struct HistoryElement *prev;
+
+ /**
+ * Next element.
+ */
+ struct HistoryElement *next;
+};
+
+
+/**
* Handle for the fake bank.
*/
struct TALER_FAKEBANK_Handle
@@ -660,6 +684,7 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
const char *dir;
const char *acc;
const char *cancelled;
+ const char *ordering;
unsigned long long account_number;
unsigned long long start_number;
long long count;
@@ -668,6 +693,10 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
json_t *history;
json_t *jresponse;
int ret;
+ int ascending;
+ struct HistoryElement *history_results_head = NULL;
+ struct HistoryElement *history_results_tail = NULL;
+ struct HistoryElement *history_element = NULL;
auth = MHD_lookup_connection_value (connection,
MHD_GET_ARGUMENT_KIND,
@@ -684,6 +713,9 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
start = MHD_lookup_connection_value (connection,
MHD_GET_ARGUMENT_KIND,
"start");
+ ordering = MHD_lookup_connection_value (connection,
+ MHD_GET_ARGUMENT_KIND,
+ "ordering");
acc = MHD_lookup_connection_value (connection,
MHD_GET_ARGUMENT_KIND,
"account_number");
@@ -727,11 +759,11 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
return MHD_NO;
}
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
- "Client asked for up to %lld results of type %s for account %llu starting at %s\n",
+ "Client asked for up to %lld results of type %s for account %llu starting at %llu\n",
count,
dir,
(unsigned long long) account_number,
- start);
+ start_number);
if (0 == strcasecmp (dir,
"CREDIT"))
{
@@ -767,12 +799,7 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
return MHD_NO;
}
if (NULL == start)
- {
- if (count > 0)
- pos = h->transactions_head;
- else
- pos = h->transactions_tail;
- }
+ pos = h->transactions_tail;
else if (NULL != h->transactions_head)
{
for (pos = h->transactions_head;
@@ -800,6 +827,13 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
}
history = json_array ();
+ if ((NULL != ordering)
+ && 0 == strcmp ("ascending",
+ ordering))
+ ascending = GNUNET_YES;
+ else
+ ascending = GNUNET_NO;
+
while ( (NULL != pos) &&
(0 != count) )
{
@@ -846,8 +880,19 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
"wt_subject", subject);
GNUNET_assert (NULL != trans);
GNUNET_free (subject);
- GNUNET_assert (0 == json_array_append_new (history,
- trans));
+
+ history_element = GNUNET_new (struct HistoryElement);
+ history_element->element = trans;
+
+ if (((0 < count) && (GNUNET_YES == ascending))
+ || ((0 > count) && (GNUNET_NO == ascending)))
+ GNUNET_CONTAINER_DLL_insert_tail (history_results_head,
+ history_results_tail,
+ history_element);
+ else
+ GNUNET_CONTAINER_DLL_insert (history_results_head,
+ history_results_tail,
+ history_element);
if (count > 0)
{
pos = pos->next;
@@ -859,6 +904,19 @@ handle_history (struct TALER_FAKEBANK_Handle *h,
count++;
}
}
+
+ if (NULL != history_results_head)
+ history_element = history_results_head;
+ while (NULL != history_element)
+ {
+ json_array_append_new (history,
+ history_element->element);
+ history_element = history_element->next;
+ if (NULL != history_element)
+ GNUNET_free_non_null (history_element->prev);
+ }
+ GNUNET_free_non_null (history_results_tail);
+
if (0 == json_array_size (history))
{
struct MHD_Response *resp;