Coverage for classes/transaction_manager.py: 100%
33 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-30 23:17 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-11-30 23:17 +0000
1from config.database import Database
2from datetime import datetime
3from datetime import datetime
5class TransactionManager:
6 @staticmethod
7 def register_transaction(user_id, divisa_origen, divisa_destino, monto, tasa_conversion, monto_convertido):
8 try:
9 conn = Database.get_connection()
10 cursor = conn.cursor()
11 TransactionManager.fecha = datetime.now()
12 fecha = TransactionManager.fecha
13 cursor.execute("""INSERT INTO Transactions (UserId, FromCurrency, ToCurrency, Amount, Rate, Result, TransactionDate) VALUES (?, ?, ?, ?, ?, ?, ?)""",
14 user_id, divisa_origen, divisa_destino, monto, tasa_conversion,
15 monto_convertido, fecha)
17 conn.commit()
18 return True
20 except Exception as e:
21 # Manejo de excepciones: devolver un mensaje de error
22 return {"error": str(e)}
24 finally:
25 if 'cursor' in locals() and cursor is not None:
26 cursor.close()
27 if 'conn' in locals() and conn is not None:
28 conn.close()
30 @staticmethod
31 def get_user_transactions(user_id):
32 try:
33 conn = Database.get_connection()
34 cursor = conn.cursor()
35 cursor.execute("SELECT * FROM Transactions WHERE UserId = ? ORDER BY TransactionDate DESC", (user_id,))
36 return cursor.fetchall()
38 except Exception as e:
39 # Manejo de excepciones: devolver un mensaje de error
40 return {"error": str(e)}
42 finally:
43 if 'cursor' in locals() and cursor is not None:
44 cursor.close()
45 if 'conn' in locals() and conn is not None:
46 conn.close()