Commit 1a5345d6 authored by Leo Peron's avatar Leo Peron
Browse files

Updating requests files

parent 6d9d61ff
import utils
import psycopg2 as sql
from psycopg2._psycopg import AsIs
from datetime import datetime as dt
from typing import List
class Requests:
......@@ -7,10 +12,11 @@ class Requests:
self.utils = utils.Utils()
self.datas = self.utils.loadDatas()
self.conn = self.__connect()
self.conn.autocommit = True
self.cur = self.__getCursor()
self.account_state = ["Ouvert", "Bloqué", "Fermé"]
self.operation_state = ["Traitée", "Non Traitée"]
self.account_type = ["Epargne", "Courant", "Revolving"]
self.account_type = ["courant", "revolving", "epargne"]
def __connect(self):
try:
......@@ -23,24 +29,20 @@ class Requests:
return None
def __getCursor(self):
if self.conn[0]:
if self.conn:
return self.conn.cursor()
else:
return None
def getUserByNum(self, num):
self.cur.execute(f"SELECT * FROM table WHERE num = {num}")
def getUserByNum(self, num: int) -> List[str]:
self.cur.execute("SELECT * FROM clients WHERE telephone = %s", (num,))
return self.cur.fetchone()
def getUsersByName(self, nom, prenom):
self.cur.execute(f"SELECT * FROM table WHERE nom = {nom} AND prenom = {prenom}")
return self.cur.fetchall()
def getUserAccountsId(self, num):
def getUserAccountsId(self, num: int):
self.cur.execute(
f"SELECT Courant, Revolving, Epargne FROM Appartenance WHERE id = {num}"
"SELECT Courant, Revolving, Epargne FROM Appartenance WHERE client = %s", (num,)
)
return self.__getAccounts(self.cur.fetchone())
return self.__getAccounts(self.cur.fetchall())
def __getAccounts(self, ids):
accounts = []
......@@ -49,16 +51,158 @@ class Requests:
accounts.append(self.cur.fetchone())
return accounts
# GESTION D'UN UTILISATEUR
def createUser(self, num: int, prenom: str, nom: str) -> bool:
try:
self.cur.execute("INSERT INTO clients VALUES (%s,%s,%s)", (num, prenom, nom))
return True
except sql.Error as e:
return False
def modifyUser(self, num: int, prenom: str, nom: str) -> bool:
try:
self.cur.execute("UPDATE clients SET prenom=%s, nom=%s WHRER telephone=%s", (prenom, nom, num))
return True
except sql.Error as e:
return False
def deleteUser(self, num: int) -> bool:
try:
self.cur.execute("DELETE FROM clients WHERE telephone=%s", (num,))
return True
except sql.Error as e:
return False
# CREATION DES COMPTES BANCAIRES
def createCourantAccount(self, num: int, solde: int, decouvert: int) -> bool:
id = self.__generateAccountId()
try:
self.cur.execute('INSERT INTO comptescourant VALUES (%s, current_timestamp , %s,%s,%s,%s,%s)',
(id, self.account_state[0], solde, solde, solde, decouvert))
self.addUserToAccount(num, id, "courant")
return True
except sql.Error as e:
return False
def createRevolvingAccount(self, num: int, solde: int, taux: int, montant: int) -> bool:
id = self.__generateAccountId()
try:
self.cur.execute('INSERT INTO comptesepargne VALUES (%s, current_timestamp, %s,%s,%s,%s)',
(id, self.account_state[0], taux, montant, solde))
self.addUserToAccount(num, id, "revolving")
return True
except sql.Error as e:
print(e)
return False
def createEpargneAccount(self, num: int, interet: int, plafond: int, solde: int) -> bool:
id = self.__generateAccountId()
try:
self.cur.execute('INSERT INTO ComptesEpargne VALUES (%s, current_timestamp, %s,%s,%s,%s)',
(id, self.account_state[0], interet, plafond, solde))
self.addUserToAccount(num, id, "epargne")
return True
except sql.Error as e:
print(e)
return False
# MODIFICATION DES COMPTES BANCAIRES
def modifyCourantAccount(self, id: int, decouvert: int) -> bool:
try:
self.cur.execute('UPDATE comptescourant SET decouvert=%s WHERE id=%s', (decouvert, id))
return True
except sql.Error as e:
return False
def modifyRevolvingAccount(self, id: int, taux: int) -> bool:
try:
self.cur.execute('UPDATE comptesrevolving SET taux=%s WHERE id=%s', (id, taux))
return True
except sql.Error as e:
return False
def modifyEpargneAccount(self, num: int, interet: int) -> bool:
try:
self.cur.execute('UPDATE comptesrevolving SET interet=%s WHERE id=%s', (id, interet))
return True
except sql.Error as e:
return False
# SUPPRESION D'UN COMPTE
def deleteAccount(self, id: int):
pass
# Insertion d'une opération
def createOperation(self, compte: int, op_type: str, acc_type: str, montant: int):
id = self.__generateOperationId()
try:
self.cur.execute('INSERT INTO %s (id, %s, montant, etat, date) VALUES (%s, %s, %s, %s, current_timestamp)',
(AsIs('operations' + op_type),AsIs(acc_type), id, compte, montant, self.operation_state[0]))
self.__updateSoldById(compte, type, montant)
return True
except sql.Error as e:
print(e)
return False
pass
def operationCheque(self):
pass
def operationGuichet(self):
pass
pass
# GESTION DES RELATIONS COMPTES - CLIENTS
def addUserToAccount(self, num: int, id: int, type: str) -> bool:
try:
self.cur.execute('INSERT INTO appartenance (client, %s) VALUES (%s, %s)', (AsIs(type), num, id))
return True
except sql.Error as e:
return False
def removeUserFromAccount(self, num: int, id: int, type: str) -> bool:
try:
self.cur.execute('DELETE FROM appartenance WHERE client=%s AND %s=%s', (num, AsIs(type), id))
return True
except sql.Error as e:
return False
# GENERATIONS CLES COMPTES ET OPERATIONS
def __generateAccountId(self) -> int:
self.cur.execute('SELECT MAX(id) FROM (SELECT id FROM ComptesEpargne UNION SELECT id FROM ComptesRevolving UNION SELECT id FROM ComptesCourant) AS comptes')
cle = self.cur.fetchone()
if not cle[0]:
return 1
return cle[0] + 1
def __generateOperationId(self) -> int:
self.cur.execute('SELECT MAX(id) FROM (SELECT id FROM operationscartebleue UNION SELECT id FROM operationscheque UNION SELECT id FROM operationsguichet UNION SELECT id FROM operationsvirement) AS operation')
cle = self.cur.fetchone()
if not cle[0]:
return 1
return cle[0] + 1
def getAccountById(self, id):
self.cur.execute(
f"SELECT * FROM Courant, Revolving, Epargne WHERE Courant.id = {id} OR Revolving.id = {id} OR Epargne.id = {id}"
"SELECT * FROM comptecourant, compterevolving, comptecourant WHERE Courant.id = {id} OR Revolving.id = {id} OR Epargne.id = {id}"
)
def getUserAccounts(self, num):
return self.getUserAccountsId(num)
def updateSoldById(self, var, type):
self.cur.execute(
f"SELECT Courant, Revolving, Epargne FROM Appartenance WHERE id = {num}"
)
pass
def __updateSoldById(self, id: int, type: str, var: int) -> bool:
try:
self.cur.execute("SELECT solde FROM %s WHERE id =%s", (AsIs(type), id))
raw = self.cur.fetchone()
self.cur.execute(
"UPDATE %s SET solde=%s WHERE id=%s", (AsIs('comptes'+type), raw[0] + var, id)
)
return True
except sql.Error as e:
return False
import json
from typing import Dict
from datetime import datetime as dt
class Utils:
def __init__(self):
......@@ -8,3 +9,9 @@ class Utils:
def loadDatas(self) -> Dict[str, str]:
with open("db_config.json", "r") as json_file:
return json.loads(json_file.read())
def hasAccount(self, id, datas):
for raw in datas:
if id in raw:
return True
return False
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment