Created login functinality

This commit is contained in:
2024-01-02 22:22:14 +00:00
parent b26bd1a228
commit e0b04d13f6
9 changed files with 89 additions and 79 deletions

View File

@ -1,4 +1,5 @@
from abc import ABC, abstractmethod
from typing import Mapping, Any
import sqlite3
class DatabaseController(ABC):
@ -14,6 +15,12 @@ class DatabaseController(ABC):
self._conn.close()
print(e)
def new_instance(self, of: type, with_fields: Mapping[str, Any]):
obj = of.__new__(of)
for attr, value in with_fields.items():
setattr(obj, attr, value)
return obj
@abstractmethod
def create(self):
pass

View File

@ -1,30 +1,48 @@
from .database import DatabaseController
from models.users.user import User
from models.users.customer import Customer
class UserController(DatabaseController):
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
def __init__(self):
super().__init__()
def create(self, user: User):
params = [
user.username,
user.password,
user.firstName,
user.lastName,
user.email,
user.phone,
user.password,
user.role
]
self._conn.execute(
"INSERT INTO Users (first_name, last_name, email, phone, password, role) VALUES (?, ?, ?, ?, ?, ?)",
"INSERT INTO Users (username, password, first_name, last_name, email, phone, role) VALUES (?, ?, ?, ?, ?, ?, ?)",
params
)
self._conn.commit()
user.login()
def read(self):
print("Doing work")
def read(self, username: str) -> User | None:
params = [
username
]
cursor = self._conn.execute(
"SELECT * FROM Users WHERE Username = ?",
params
)
row = cursor.fetchone()
if row != None:
params = dict(zip(self.FIELDS, row))
obj = self.new_instance(Customer, params)
print(obj.__dict__)
return obj
return None
def update(self):
print("Doing work")