2024-01-01 20:19:57 +00:00
|
|
|
from .database import DatabaseController
|
|
|
|
from models.users.user import User
|
2024-01-02 22:22:14 +00:00
|
|
|
from models.users.customer import Customer
|
2024-01-18 21:11:01 +00:00
|
|
|
from models.users.seller import Seller
|
2024-01-01 20:19:57 +00:00
|
|
|
|
|
|
|
class UserController(DatabaseController):
|
2024-01-02 22:22:14 +00:00
|
|
|
FIELDS = ['id', 'username', 'password', 'firstName', 'lastName', 'email', 'phone', 'role']
|
2024-01-01 20:19:57 +00:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def create(self, user: User):
|
|
|
|
params = [
|
2024-01-02 22:22:14 +00:00
|
|
|
user.username,
|
|
|
|
user.password,
|
2024-01-01 20:19:57 +00:00
|
|
|
user.firstName,
|
|
|
|
user.lastName,
|
|
|
|
user.email,
|
|
|
|
user.phone,
|
|
|
|
user.role
|
|
|
|
]
|
|
|
|
|
|
|
|
self._conn.execute(
|
2024-01-02 22:22:14 +00:00
|
|
|
"INSERT INTO Users (username, password, first_name, last_name, email, phone, role) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
2024-01-01 20:19:57 +00:00
|
|
|
params
|
|
|
|
)
|
|
|
|
self._conn.commit()
|
|
|
|
|
2024-01-02 22:22:14 +00:00
|
|
|
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))
|
2024-01-18 21:11:01 +00:00
|
|
|
|
|
|
|
# Is user a seller
|
|
|
|
type = Customer
|
|
|
|
if row[7] == "Seller":
|
|
|
|
type = Seller
|
|
|
|
|
|
|
|
obj = self.new_instance(type, params)
|
2024-01-02 22:22:14 +00:00
|
|
|
return obj
|
|
|
|
|
|
|
|
return None
|
2024-01-01 20:19:57 +00:00
|
|
|
|
2024-01-19 11:57:53 +00:00
|
|
|
|
|
|
|
def read_id(self, id: int) -> User | None:
|
|
|
|
params = [
|
|
|
|
id
|
|
|
|
]
|
|
|
|
|
|
|
|
cursor = self._conn.execute(
|
|
|
|
"SELECT * FROM Users WHERE id = ?",
|
|
|
|
params
|
|
|
|
)
|
|
|
|
row = cursor.fetchone()
|
|
|
|
|
|
|
|
if row != None:
|
|
|
|
params = dict(zip(self.FIELDS, row))
|
|
|
|
|
|
|
|
# Is user a seller
|
|
|
|
type = Customer
|
|
|
|
if row[7] == "Seller":
|
|
|
|
type = Seller
|
|
|
|
|
|
|
|
obj = self.new_instance(type, params)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
return None
|
2024-01-01 20:19:57 +00:00
|
|
|
def update(self):
|
|
|
|
print("Doing work")
|
|
|
|
|
|
|
|
def delete(self):
|
|
|
|
print("Doing work")
|