From 47cb2fb2c78e181072fc1235da89f51005c9ce8a Mon Sep 17 00:00:00 2001 From: Lev Date: Thu, 27 Nov 2025 21:02:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=BE=D0=B3=D0=B8=D0=BD=20+=20SQL=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=8A=D0=B5=D0=BA=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API/main.py | 10 +++++++++- API/utils.py | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/API/main.py b/API/main.py index e4fbc0a..a0653d9 100644 --- a/API/main.py +++ b/API/main.py @@ -5,9 +5,17 @@ app = FastAPI() db = utils.DataBase("") @app.post("/registration") -def reg(email: str, password: str) : +def registration(email: str, password: str) : answer = db.registration(email, password) if answer["code"] == 1 : HTTPException(400, "Email is busy.") + else : + return answer["data"] + +@app.post("/login") +def login(email: str, password: str) : + answer = db.login(email, password) + if answer["code"] == 1 : + HTTPException(400, "Incorrect email or password.") else : return answer["data"] \ No newline at end of file diff --git a/API/utils.py b/API/utils.py index edb60c1..5e5c295 100644 --- a/API/utils.py +++ b/API/utils.py @@ -2,6 +2,7 @@ import sqlite3 import pandas import datetime import hashlib +import json class DataBase : def __init__(self, path: str) : @@ -14,14 +15,20 @@ class DataBase : email TEXT NOT NULL UNIQUE, pass_hash TEXT NOT NULL, register TEXT NOT NULL, - urls TEXT DEFAULT "" + urls TEXT DEFAULT "", + payment TEXT DEFAULT "" ) """) self.con.commit() - def read_table(self, sql_command: str) -> dict : - return pandas.read_sql(sql_command, self.con).to_json() + def read_table(self, sql_command: str, params=None) -> dict : + if params == None : + params = () + return json.loads(pandas.read_sql(sql_command, self.con, params=params).to_json()) + + def get_hash(text: str) : + return hashlib.sha256(text.encode()).hexdigest() def registration(self, email: str, passwd: str) : """ @@ -30,17 +37,29 @@ class DataBase : """ date = datetime.datetime.now() try : - self.cur.execute(f""" + self.cur.execute(""" INSERT INTO users (email, pass_hash, register) - VALUES ({email} {hashlib.sha256(passwd.encode()).hexdigest()} {date.day}-{date.month}-{date.year}_{date.hour}:{date.minute}:{date.second}) - """) + VALUES (?, ?, ?) + """, (email, self.get_hash(passwd), f"{date.day}-{date.month}-{date.year} {date.hour}:{date.minute}:{date.second}")) + self.con.commit() except : return {"code": 1} - self.con.commit() - data = self.read_table(f""" + data = self.read_table(""" SELECT id FROM users - WHERE email = {email} - """) + WHERE email = ? + """, (email)) return {"code": 0, "data": {"id": data["id"]["0"], "email": email}} - \ No newline at end of file + def login(self, email: str, passwd: str) : + """ + 0 - Success + 1 - Incorrect email or password + """ + data = self.read_table(""" + SELECT id, urls, payment FROM users + WHERE email = ? AND pass_hash = ? + """, (email, self.get_hash(passwd))) + if data["id"] == {} : + return {"code": 1} + else : + return {"code": 0, "data": {"id": data["id"]["0"], "email": email, "urls": data["urls"]["0"], "payment": data["payment"]["0"]}} \ No newline at end of file