Created base database controller and user database controller to allow for initial database control

This commit is contained in:
Luke Else 2024-01-01 20:19:57 +00:00
parent ef94773b8f
commit b1a22cb7bd
13 changed files with 84 additions and 14 deletions

2
app.py
View File

@ -1,6 +1,6 @@
from flask import Flask
from os import environ
from web import blueprint
from controllers.web import blueprint
'''
Main entrypoint for Flask application.

0
controllers/__init__.py Normal file
View File

View File

View File

@ -0,0 +1,31 @@
from abc import ABC, abstractmethod
import sqlite3
class DatabaseController(ABC):
__sqlitefile = "./data/wmgzon.db"
def __init__(self):
self._conn = None
try:
self._conn = sqlite3.connect(self.__sqlitefile)
except sqlite3.Error as e:
# Close the connection if still open
if self._conn:
self._conn.close()
print(e)
@abstractmethod
def create(self):
pass
@abstractmethod
def read(self):
pass
@abstractmethod
def update(self):
pass
@abstractmethod
def delete(self):
pass

View File

@ -0,0 +1,33 @@
from .database import DatabaseController
from models.users.user import User
class UserController(DatabaseController):
def __init__(self):
super().__init__()
def create(self, user: User):
params = [
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 (?, ?, ?, ?, ?, ?)",
params
)
self._conn.commit()
user.login()
def read(self):
print("Doing work")
def update(self):
print("Doing work")
def delete(self):
print("Doing work")

View File

@ -1,5 +1,7 @@
from . import blueprint
from flask import render_template, redirect, request, session
from flask import render_template, redirect, request
from controllers.database.user import UserController
from models.users.customer import Customer
# Function responsible for displaying the main landing page of the site
@ -31,5 +33,8 @@ def display_signup():
# Function responsible for handling signups to the site
@blueprint.post('/signup')
def signup():
database = UserController()
database.create(Customer())
print("Tryin to signup as " + request.form['username'])
return redirect("/")

View File

@ -1,4 +1,4 @@
from user import User
from .user import User
class Admin(User):
'''
@ -8,10 +8,10 @@ class Admin(User):
super().__init__()
self.store = ""
def login():
def login(self):
print("Logging in as Admin")
def signup():
def signup(self):
print("Signing up as Admin")
def createProduct():

View File

@ -1,4 +1,4 @@
from user import User
from .user import User
class Customer(User):
'''
@ -9,9 +9,9 @@ class Customer(User):
def __init__(self):
super().__init__()
def login():
def login(self):
print("Logging in as Customer")
def signup():
def signup(self):
print("Signing up as Customer")

View File

@ -1,4 +1,4 @@
from user import User
from .user import User
class Seller(User):
'''
@ -8,10 +8,10 @@ class Seller(User):
super().__init__()
self.store = ""
def login():
def login(self):
print("Logging in as Seller")
def signup():
def signup(self):
print("Signing up as Seller")
def createProduct():

View File

@ -11,6 +11,8 @@ class User(ABC):
self.firstName = ""
self.lastName = ""
self.phone = ""
self.password = ""
self.role=""
@abstractmethod
def login(self):

View File

@ -1,5 +1,4 @@
import sqlite3
from sqlite3 import Error
def create_connection(db_file):
@ -15,7 +14,7 @@ def create_connection(db_file):
print("SQLite Version: " + sqlite3.version)
print("Table creation complete")
except Error as e:
except sqlite3.Error as e:
print(e)
finally:
if conn:

View File

@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS Products (
CREATE TABLE IF NOT EXISTS Orders (
id INTEGER PRIMARY KEY,
sellerID TEXT NOT NULL
REFERENCES Users (id)
REFERENCES Users (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
total DECIMAL NOT NULL,