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
|
|
|
|
2024-02-13 10:04:59 +00:00
|
|
|
def read_all(self) -> list[User] | None:
|
|
|
|
params = []
|
|
|
|
query = """ SELECT * FROM Users """
|
|
|
|
|
|
|
|
return self.get_many(query, params)
|
|
|
|
|
2024-01-28 11:59:04 +00:00
|
|
|
def update(self):
|
|
|
|
print("Doing work")
|
|
|
|
|
2024-02-13 22:48:23 +00:00
|
|
|
def delete(self, id: int):
|
|
|
|
params = [
|
|
|
|
id
|
|
|
|
]
|
|
|
|
query = """ DELETE FROM Users WHERE id = ? """
|
|
|
|
|
|
|
|
return self.do(query, params)
|