-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
101 lines (84 loc) · 2.41 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from flask import Flask, request, jsonify
import sqlite3
app = Flask(__name__)
# Connecting to the db
def db_connection():
conn = None
try:
conn = sqlite3.connect("bank.sqlite")
except sqlite3.error as e:
print(e)
return conn
# List of Dict from cursor
def ldict_txa(cursor):
list = [
dict(id=row[0], amount=row[1], account_no=row[2], created_datetime=row[3])
for row in cursor.fetchall()
]
return list
def ldict_bal(cursor):
list = [dict(account_no=row[0], balance=row[1]) for row in cursor.fetchall()]
return list
conn = db_connection()
from_account_no = 104
to_account_no = 105
amount = 75.00
#! some how all these in 1 string dont work ?!!
debit = """UPDATE balancet
SET balance = balance - {amt}
WHERE account_no = {fa}
""".format(
amt=amount, fa=from_account_no
)
credit = """UPDATE balancet
SET balance = balance + {amt}
WHERE account_no = {ta}
""".format(
amt=amount, ta=to_account_no
)
# FROM and TO TXA
update_from_txa = """
INSERT INTO transactionst(amount, account_no, created_datetime)
VALUES( -{amt}, {fa}, datetime('now'))
""".format(
amt=amount, fa=from_account_no
)
update_to_txa = """
INSERT INTO transactionst(amount, account_no, created_datetime)
VALUES( {amt}, {ta}, datetime('now'))
""".format(
amt=amount, ta=to_account_no
)
cursord = conn.execute(debit)
cursorc = conn.execute(credit)
cursor_from_txa = conn.execute(update_from_txa)
cursor_to_txa = conn.execute(update_to_txa)
# COMMIT
conn.commit()
# Need to commit to read from database,
# could return payload w/o reading too ?
# Will give to_txa, from_txa (since desc order)
txa_rows = conn.execute("SELECT * FROM transactionst ORDER BY id DESC LIMIT 2;")
balance_rows = conn.execute(
"SELECT * FROM balancet WHERE account_no={fa} OR account_no={ta}".format(
fa=from_account_no, ta=to_account_no
)
)
txas = ldict_txa(txa_rows)[::-1] # from_txa, to_txa
bals = ldict_bal(balance_rows) # from_bal, to_bal
payload = {
"id": txas[0]["id"],
"from": {
"id": bals[0]["account_no"],
"balance": bals[0]["balance"],
},
"to": {
"id": bals[1]["account_no"],
"balance": bals[1]["balance"],
},
"transfered": txas[1]["amount"], # to_txa
"created_datetime": txas[1]["created_datetime"],
}
print(payload)
# # @app.route("/Balance", methods = ["GET", "POST"])
# # def balance():