2024-01-28 11:59:04 +00:00
|
|
|
from .database import DatabaseController
|
|
|
|
from models.users.user import User
|
|
|
|
from models.users.customer import Customer
|
|
|
|
from models.users.seller import Seller
|
2024-02-04 23:47:00 +00:00
|
|
|
from models.users.admin import Admin
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class UserController(DatabaseController):
|
|
|
|
FIELDS = ['id', 'username', 'password', 'firstName',
|
|
|
|
'lastName', 'email', 'phone', 'role']
|
2024-02-04 23:47:00 +00:00
|
|
|
TYPE = User
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
def convert_type(self, user: User | None
|
|
|
|
) -> User | Customer | Seller | Admin | None:
|
|
|
|
""" Function to convert a given user to their correct subtype """
|
|
|
|
if user is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Set the object to the correct type
|
|
|
|
type = Customer
|
|
|
|
if user.role == "Seller":
|
|
|
|
type = Seller
|
|
|
|
if user.role == "Admin":
|
|
|
|
type = Admin
|
|
|
|
|
|
|
|
obj = self.new_instance(type, user.__dict__)
|
|
|
|
return obj
|
|
|
|
|
2024-01-28 11:59:04 +00:00
|
|
|
def create(self, user: User):
|
|
|
|
params = [
|
|
|
|
user.username,
|
|
|
|
user.password,
|
|
|
|
user.firstName,
|
|
|
|
user.lastName,
|
|
|
|
user.email,
|
|
|
|
user.phone,
|
|
|
|
user.role
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = """
|
|
|
|
INSERT INTO Users
|
2024-01-28 11:59:04 +00:00
|
|
|
(username, password, first_name, last_name, email, phone, role)
|
2024-02-04 23:47:00 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.do(query, params)
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def read(self, username: str) -> User | None:
|
|
|
|
params = [
|
|
|
|
username
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = "SELECT * FROM Users WHERE Username = ?"
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.convert_type(self.get_one(query, params))
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def read_id(self, id: int) -> User | None:
|
|
|
|
params = [
|
|
|
|
id
|
|
|
|
]
|
2024-02-04 23:47:00 +00:00
|
|
|
query = "SELECT * FROM Users WHERE id = ?"
|
2024-01-28 11:59:04 +00:00
|
|
|
|
2024-02-04 23:47:00 +00:00
|
|
|
return self.convert_type(self.get_one(query, params))
|
2024-01-28 11:59:04 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
print("Doing work")
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
print("Doing work")
|