implement currency and report
This commit is contained in:
53
transportmanager/server/models/currency.py
Normal file
53
transportmanager/server/models/currency.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from datetime import datetime
|
||||
from database import get_connection, is_postgres
|
||||
|
||||
class Currency:
|
||||
def __init__(self):
|
||||
self.ph = "%s" if is_postgres() else "?"
|
||||
|
||||
def currency_to_dict(self, row):
|
||||
currency = {
|
||||
'id': row[0],
|
||||
'user_id': row[1],
|
||||
'name': row[2],
|
||||
'value': row[3],
|
||||
'created_at': row[4]
|
||||
}
|
||||
return currency
|
||||
|
||||
def insert_currency(self, access_data):
|
||||
created_at = datetime.now().isoformat()
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
returning = "RETURNING id" if is_postgres() else ""
|
||||
query = f"""
|
||||
INSERT INTO currency (
|
||||
user_id, name, value, created_at
|
||||
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
|
||||
"""
|
||||
cursor.execute(
|
||||
query,
|
||||
(
|
||||
access_data['user_id'],
|
||||
access_data['name'],
|
||||
access_data['value'],
|
||||
created_at
|
||||
)
|
||||
)
|
||||
inserted_id = None
|
||||
if is_postgres():
|
||||
inserted_id = cursor.fetchone()[0]
|
||||
else:
|
||||
inserted_id = cursor.lastrowid
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
return inserted_id
|
||||
|
||||
def get_currency_user_id(self, user_id):
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT * FROM currency WHERE user_id = {self.ph}", (user_id,))
|
||||
row = cursor.fetchone()
|
||||
return self.currency_to_dict(row) if row else None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user