/
api.py
86 lines (70 loc) · 2.18 KB
/
api.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
import queryMapper
import database
def get_account_by_id(account_id):
"""
Fetches account that has matching account id
:param account_id: requested account id
:return: requested account if found
"""
conn = database.get_connection()
c = conn.cursor()
c.execute("""SELECT * FROM account WHERE account_id = :id""", {'id': account_id})
query_array = list(c.fetchone())
account = queryMapper.query_to_account(query_array)
return account
def create_transaction_history(account, amount):
"""
Creates transaction history of requested account
:param account: account requested
:param amount: amount of transaction requested
:return:
"""
conn = database.get_connection()
c = conn.cursor()
c.execute("""INSERT INTO transaction_history VALUES(
:account_id,
DATE('now', 'localtime'),
TIME('now', 'localtime'),
:amount,
:balance
)""", {'account_id': account.account_id,
'amount': amount,
'balance': account.balance})
conn.commit()
def update_account_balance(account):
"""
Updates account with the new balance
:param account: account to be updated
:return:
"""
conn = database.get_connection()
c = conn.cursor()
c.execute("""UPDATE account
SET balance = :balance
WHERE account_id = :account_id
""", {'balance': account.balance, 'account_id': account.account_id})
conn.commit()
def get_transaction_histories(account_id):
"""
Get transaction histories of requested account
:param account_id:
:return: list of transaction histories
"""
conn = database.get_connection()
c = conn.cursor()
c.execute("""SELECT * FROM transaction_history WHERE account_id = :id
ORDER BY date DESC, time DESC""", {'id': account_id})
histories = queryMapper.queries_to_histories(c.fetchall())
return histories
def initialize_db():
"""
Initializes tables and default entries in the database if it doesn't already exist.
:return:
"""
database.initialize_db()
def close_db_connection():
"""
Closes current database connection
:return:
"""
database.close_connection()