Skip to content
This repository has been archived by the owner on Sep 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from SELab-2/backend_file_structure_POC
Browse files Browse the repository at this point in the history
Backend file structure Proof Of Concept
  • Loading branch information
msathieu authored Feb 25, 2024
2 parents 185a98d + 684219a commit 3239fa0
Show file tree
Hide file tree
Showing 27 changed files with 852 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Backend
on: [push]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: pip install -r backend/requirements.txt
- name: Lint code
run: ruff check backend
163 changes: 163 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version routes.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version routes.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version routes.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version routes.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/

# Ruff linter
.ruff_cache
24 changes: 24 additions & 0 deletions backend/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from flask import Flask

from db.extensions import db
from routes.teachers import teachers_blueprint

app = Flask(__name__)

# Koppel routes uit andere modules.
app.register_blueprint(teachers_blueprint)

db_host = os.getenv("DB_HOST", "localhost")
db_port = os.getenv("DB_PORT", "5432")
db_user = os.getenv("DB_USERNAME", "postgres")
db_password = os.getenv("DB_PASSWORD", "postgres")
db_database = os.getenv("DB_DATABASE", "delphi")

# Koppel postgres uri en db aan app instantie
app.config["SQLALCHEMY_DATABASE_URI"] = f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_database}"
db.init_app(app)

if __name__ == "__main__":
app.run(debug=True)
4 changes: 4 additions & 0 deletions backend/db/errors/database_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ItemNotFoundError(Exception):

def __init__(self, message: str):
super().__init__(message)
9 changes: 9 additions & 0 deletions backend/db/extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import DeclarativeBase


class Base(DeclarativeBase):
pass


db = SQLAlchemy(model_class=Base)
27 changes: 27 additions & 0 deletions backend/db/implementation/SqlLesgeverDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.TeacherDAO import TeacherDAO
from db.models.models import Teacher
from domain.models.TeacherDataclass import TeacherDataclass


class SqlTeacherDAO(TeacherDAO):
def get_teacher(self, ident: int):
teacher: Teacher = Teacher.query.get(ident=ident)

if not teacher:
raise ItemNotFoundError("TeacherDataclass with given id not found.")

return teacher.to_domain_model()

def get_all_teachers(self) -> list[TeacherDataclass]:
teachers: list[Teacher] = Teacher.query.all()
return [lesgever.to_domain_model() for lesgever in teachers]

def create_teacher(self, teacher: TeacherDataclass):
new_teacher = Teacher(name=teacher.name)

db.session.add(new_teacher)
db.session.commit()

teacher.id = new_teacher.id
36 changes: 36 additions & 0 deletions backend/db/implementation/SqlVakDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from db.errors.database_errors import ItemNotFoundError
from db.extensions import db
from db.interface.SubjectDAO import SubjectDAO
from db.models.models import Subject, Teacher
from domain.models.SubjectDataclass import SubjectDataclass


class SqlSubjectDAO(SubjectDAO):
def create_subject(self, subject: SubjectDataclass, teacher_id: int):
teacher = Teacher.query.get(teacher_id)

if not teacher:
raise ItemNotFoundError(f"De teacher met id {teacher_id} kon niet in de databank gevonden worden")

new_subject = Subject(name=subject.name, teacher=teacher)

db.session.add(new_subject)
db.session.commit()

subject.id = new_subject.id

def get_subject(self, teacher_id: int):
subject = Subject.query.get(teacher_id)
if not subject:
raise ItemNotFoundError(f"De lesgever met id {teacher_id} kon niet in de databank gevonden worden")

return subject.to_domain_model()

def get_subjects(self, teacher_id: int) -> list[SubjectDataclass]:
teacher: Teacher = Teacher.query.get(ident=teacher_id)

if not teacher:
raise ItemNotFoundError(f"De teacher met id {teacher_id} kon niet in de databank gevonden worden")

subjects: list[Subject] = teacher.subjects
return [vak.to_domain_model() for vak in subjects]
37 changes: 37 additions & 0 deletions backend/db/interface/SubjectDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from abc import ABC, abstractmethod

from domain.models.SubjectDataclass import SubjectDataclass


class SubjectDAO(ABC):
@abstractmethod
def create_subject(self, subject: SubjectDataclass, teacher_id: int):
"""
Creëert een nieuw SubjectDataclass in de database en associeert het met een TeacherDataclass.
:param subject: De SubjectDataclass domeinmodel-instantie die aan de database moet worden toegevoegd.
:param teacher_id: De identificatie van de TeacherDataclass waarmee het SubjectDataclass geassocieerd wordt.
:raises: ItemNotFoundException: Als er geen TeacherDataclass met de opgegeven `teacher_id` in de database is.
"""
raise NotImplementedError()

@abstractmethod
def get_subject(self, teacher_id: int):
"""
Haalt een SubjectDataclass op aan de hand van zijn identificatie.
:param teacher_id: De identificatie van het op te halen SubjectDataclass.
:raises ItemNotFoundException: Als er geen SubjectDataclass met de opgegeven `ident` in de database bestaat.
:returns: De domeinmodel-instantie van het opgehaalde SubjectDataclass.
"""
raise NotImplementedError()

@abstractmethod
def get_subjects(self, teacher_id: int) -> list[SubjectDataclass]:
"""
Haalt de subjects op die door een bepaalde teacher worden gegeven.
:param teacher_id: De teacher waarvan de subjects opgehaald moeten worden.
:return: Een lijst van subjects die door de gegeven teacher worden gegeven.
"""
raise NotImplementedError()
34 changes: 34 additions & 0 deletions backend/db/interface/TeacherDAO.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from abc import ABC, abstractmethod

from domain.models.TeacherDataclass import TeacherDataclass


class TeacherDAO(ABC):
@abstractmethod
def get_teacher(self, ident: int) -> TeacherDataclass:
"""
Haalt een teacher op aan de hand van zijn identificatie.
:param ident: Het id van de te zoeken teacher.
:return: De teacher die overeenkomt met de gegeven id.
:raises ItemNotFoundException: Als geen teacher met het gegeven id gevonden werd.
"""
raise NotImplementedError()

@abstractmethod
def get_all_teachers(self) -> list[TeacherDataclass]:
"""
Haalt alle lesgevers op.
:return: Een lijst van alle lesgevers.
"""
raise NotImplementedError()

@abstractmethod
def create_teacher(self, teacher: TeacherDataclass):
"""
Maakt een nieuwe teacher aan.
:param teacher: De teacher die aangemaakt moet worden.
"""
raise NotImplementedError()
Loading

0 comments on commit 3239fa0

Please sign in to comment.