Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 8848b49

Browse files
committed
add input direction transaction
1 parent 3f3361b commit 8848b49

File tree

5 files changed

+44
-34
lines changed

5 files changed

+44
-34
lines changed

transaction_service/Dockerfile

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
FROM ubuntu:20.04
22
ENV HOME /root
33
ENV TZ=Europe/London
4-
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
54

65
SHELL ["/bin/bash", "-c"]
7-
6+
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
87
RUN apt-get update && apt-get -y --no-install-recommends install \
98
build-essential \
109
cmake \
@@ -14,15 +13,15 @@ RUN apt-get update && apt-get -y --no-install-recommends install \
1413
git
1514

1615
WORKDIR /app
17-
1816
COPY . .
1917

2018
USER root:root
21-
22-
RUN chmod +x ./install-libs.sh
19+
RUN chmod +x ./build.sh
2320
RUN apt-get install ca-certificates -y
24-
RUN ./install-libs.sh
21+
RUN ./build.sh
22+
2523
WORKDIR /app/build
2624
RUN chmod +x ./transaction_service
2725
ENV LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64:/usr/local/lib/json-parser
26+
2827
CMD [ "./transaction_service" ]

transaction_service/app/queue.c

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@ void *context;
99
void *zmq_responder;
1010
const char *address;
1111

12+
int receive_message(int LEN, char *buffer, char *json) {
13+
int size = zmq_recv (zmq_responder, buffer, LEN, 0);
14+
if (size == -1){
15+
return 0;
16+
}
17+
int i = 0;
18+
while (i <= LEN+1) {
19+
if(buffer[i] == '}') {
20+
buffer[i+1] = '\0';
21+
LEN = i+1;
22+
break;
23+
}
24+
i+=1;
25+
}
26+
int j = 0;
27+
while (j<=LEN) {
28+
json[j] = buffer[j];
29+
j+=1;
30+
}
31+
return LEN;
32+
}
33+
1234
void start_zmq() {
1335
context = zmq_ctx_new();
1436
zmq_responder = zmq_socket (context, ZMQ_REP);

transaction_service/app/queue.h

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <string.h>
1212
#include <assert.h>
1313

14+
int receive_message(int, char *, char *);
1415
void start_zmq();
1516

1617
#endif //TRANSACTION_SERVICE_QUEUE_H

transaction_service/app/transaction.c

+16-28
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,11 @@
55
#include "transaction.h"
66
#include <zmq.h>
77

8-
int receive_message(int LEN, char *buffer, char *json) {
9-
int size = zmq_recv (zmq_responder, buffer, LEN, 0);
10-
if (size == -1){
11-
return 0;
12-
}
13-
int i = 0;
14-
while (i <= LEN+1) {
15-
if(buffer[i] == '}') {
16-
buffer[i+1] = '\0';
17-
LEN = i+1;
18-
break;
19-
}
20-
i+=1;
21-
}
22-
int j = 0;
23-
while (j<=LEN) {
24-
json[j] = buffer[j];
25-
j+=1;
26-
}
27-
return LEN;
28-
}
8+
enum direction {in,out};
299

3010
void transaction() {
3111
json_value* value;
32-
int LEN = 50;
12+
int LEN = 1028;
3313
char *buffer = (char *) malloc(LEN);
3414
char *json = (char *) malloc(LEN);
3515
LEN = receive_message(LEN, buffer, json);
@@ -41,11 +21,16 @@ void transaction() {
4121
const char *error = "{\"error\":\"Unable to parse data\"}";
4222
zmq_send (zmq_responder, error, strlen(error), 0);
4323
}else {
44-
const char *param_values[2];
45-
const char *keys[] = {"user", "amount"};
46-
for (int i = 0; i < 2; ++i) {
24+
int param_len = 3;
25+
const char *param_values[param_len];
26+
const char *keys[] = {"user", "amount", "direction"};
27+
for (int i = 0; i < param_len; ++i) {
4728
if (strncasecmp(value->u.object.values[i].name, keys[i], strlen(keys[i])) == 0) {
4829
param_values[i] = value->u.object.values[i].value->u.string.ptr;
30+
if(i==2 && strncasecmp(value->u.object.values[i].value->u.string.ptr, "in", strlen("in")) == 0) {
31+
param_values[i] = "1";
32+
}else if(i==2 && strncasecmp(value->u.object.values[i].value->u.string.ptr, "out", strlen("out")) == 0)
33+
param_values[i]= "0";
4934
}
5035
}
5136
transactional(param_values);
@@ -77,9 +62,12 @@ void transactional(const char *const *param_values) {
7762

7863
}
7964
if (rollback == 0) {
80-
// Update user balance and subtract the withdraw amount
81-
char *udt = "UPDATE account SET balance = balance - $2 WHERE user_id=$1";
82-
response = PQexecParams(conn, udt, 2, NULL, param_values, NULL, NULL, 0);
65+
char *udt[] = {"UPDATE account SET balance=balance-$2 WHERE user_id=$1",
66+
"UPDATE account SET balance=balance+$2 WHERE user_id=$1"};
67+
if(strncasecmp(param_values[2], "0", 1)==0)
68+
response = PQexecParams(conn, udt[0], 2, NULL, param_values, NULL, NULL, 0);
69+
if(strncasecmp(param_values[2], "1", 1)==0)
70+
response = PQexecParams(conn, udt[1], 2, NULL, param_values, NULL, NULL, 0);
8371
command_error_handler(conn, response);
8472
PQclear(response);
8573

File renamed without changes.

0 commit comments

Comments
 (0)