Skip to content

Commit

Permalink
feat: type hints and abstract classes
Browse files Browse the repository at this point in the history
- the abstract classes service,
repository and mapped are implemented,
and some type hints are added to make
the code more readable
  • Loading branch information
ElCabris committed Apr 29, 2024
2 parents 3f0c19a + ca0c265 commit afab93e
Show file tree
Hide file tree
Showing 21 changed files with 428 additions and 253 deletions.
20 changes: 14 additions & 6 deletions app/business/models/subject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

class Subject:
def __init__(self, id: int = None, name: str = None,
description: str = None, user_id: int = None, topics: list = None):
self.id: int = id
self.name: str = name
self.description: str = description
self.user_id: int = user_id
description: str = None, user_id: int = None,
topics: list = None) -> None:
self.id: int | None = id
self.name: str | None = name
self.description: str | None = description
self.user_id: int | None = user_id
self.topics: list = topics

def __repr__(self) -> str:
return f"Subject(id={self.id}, name={self.name}, description={self.description}, user_id={self.user_id})"
return (
f"Subject("
f"id={self.id}, "
f"name={self.name}, "
f"description={self.description}, "
f"user_id={self.user_id}"
")"
)
27 changes: 20 additions & 7 deletions app/business/models/topic.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
# app/business/model/topic.py

class Topic:
def __init__(self, id: int = None, title: str = None,
content: str = None, subject_id: int = None):
self.id = id
self.title = title
self.content = content
self.subject_id = subject_id
def __init__(self, id: int = None,
title: str = None,
content: str = None,
subject_id: int = None,
description: str = None,
name: str = None) -> None:
self.id: int | None = id
self.title: str | None = title
self.content: str | None = content
self.subject_id: int | None = subject_id
self.name: str | None = name
self.description: str | None = description

def __repr__(self):
return f"<Topic(id={self.id}, title={self.title}, content={self.content}, subject_id={self.subject_id})>"
return (
f"<Topic("
f"id={self.id}, "
f"title={self.title}, "
f"content={self.content}, "
f"subject_id={self.subject_id}"
")>"
)
14 changes: 11 additions & 3 deletions app/business/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

class User:
def __init__(self, id: int = None, discord_id: str = None,
username: str = None, created_at: str = None, subjects: list = None):
self.id: int = id
username: str = None, created_at: str = None,
subjects: list = None) -> None:
self.id: int | None = id
self.discord_id: str | None = discord_id
self.username: str | None = username
self.created_at: str | None = created_at
self.subjects: list = subjects

def __repr__(self) -> str:
return f"User(id={self.id}, discord_id={self.discord_id}, username={self.username}, created_at={self.created_at})"
return (
f"User("
f"id={self.id}, "
f"discord_id={self.discord_id}, "
f"username={self.username}, "
f"created_at={self.created_at}"
")"
)
40 changes: 40 additions & 0 deletions app/business/services/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""app/business/services/service.py
Module docstring ..."""

from abc import ABC, abstractmethod


class Service(ABC):
"""Class docstring"""

@abstractmethod
def __init__(self):
pass

@abstractmethod
def create(self):
"""Method docstring"""

@abstractmethod
def get_element_by_id(self, id_):
"""Method docstring"""

@abstractmethod
def get_elements_by_object_id(self, id_):
"""Method docstring"""

@abstractmethod
def get_all_elements(self):
"""Method docstring"""

@abstractmethod
def update_element(self):
"""Method docstring"""

@abstractmethod
def delete_element(self, id_):
"""Method docstring"""

@abstractmethod
def delete_element_by_object_id(self, element_id):
"""Method docstring"""
51 changes: 30 additions & 21 deletions app/business/services/subject_service.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,46 @@
# app/business/services/subject_service.py
"""app/business/services/subject_service.py
Module docstring ..."""

from app.business.models.subject import Subject
from app.persistence.repositories.subject_repository import SubjectRepository


class SubjectService:
def __init__(self):
self.subject_repository = SubjectRepository()

def create_subject(self, name, description, user_id):
subject = Subject(name=name, description=description, user_id=user_id)
"""Class docstrign"""

def __init__(self) -> None:
self.subject_repository: SubjectRepository = SubjectRepository()

def create_subject(self,
name: str,
description: str,
user_id: int) -> Subject:
subject: Subject = Subject(name=name,
description=description,
user_id=user_id)
return self.subject_repository.create(subject)

def get_subject_by_id(self, id):
return self.subject_repository.get_by_id(id)
def get_subject_by_id(self, id_: int) -> Subject | None:
return self.subject_repository.get_by_id(id_)

def get_subjects_by_user_id(self, user_id):
return self.subject_repository.get_by_user_id(user_id)
def get_subjects_by_user_id(self, user_id: int) -> list[Subject] | None:
return self.subject_repository.get_by_element_id(user_id)

def get_all_subjects(self):
def get_all_subjects(self) -> list[Subject]:
return self.subject_repository.get_all()

def update_subject(self, id, name, description, user_id):
subject = Subject(
id=id,
name=name,
description=description,
user_id=user_id)
def update_subject(self, id_: int,
name: str,
description: str,
user_id: int) -> Subject:
subject: Subject = Subject(id=id_, name=name,
description=description,
user_id=user_id)
return self.subject_repository.update(subject)

def delete_subject(self, id):
subject = self.subject_repository.get_by_id(id)
def delete_subject(self, id_: int) -> Subject:
subject: Subject | None = self.subject_repository.get_by_id(id_)
return self.subject_repository.delete(subject)

def delete_subject_by_user_id(self, user_id):
return self.subject_repository.delete_by_user_id(user_id)
def delete_subject_by_user_id(self, user_id: int) -> list[Subject] | None:
return self.subject_repository.delete_by_element_id(user_id)
47 changes: 24 additions & 23 deletions app/business/services/topic_service.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
# app/business/services/topic_service.py
"""app/business/services/topic_service.py
Module docstring ..."""

from app.business.services.service import Service
from app.business.models.topic import Topic
from app.persistence.repositories.topic_repository import TopicRepository


class TopicService:
class TopicService(Service):
"""Class docstring"""

def __init__(self):
self.topic_repository = TopicRepository()
self.topic_repository: TopicRepository = TopicRepository()

def create_topic(self, name, description, subject_id):
topic = Topic(
name=name,
description=description,
subject_id=subject_id)
def create(self, name: str, description: str, subject_id: int):
topic = Topic(name=name,
description=description,
subject_id=subject_id)
return self.topic_repository.create(topic)

def get_topic_by_id(self, id):
return self.topic_repository.get_by_id(id)
def get_element_by_id(self, id_):
return self.topic_repository.get_by_id(id_)

def get_topics_by_subject_id(self, subject_id):
return self.topic_repository.get_by_subject_id(subject_id)
def get_elements_by_object_id(self, id_):
return self.topic_repository.get_by_element_id(id_)

def get_all_topics(self):
def get_all_elements(self):
return self.topic_repository.get_all()

def update_topic(self, id, name, description, subject_id):
topic = Topic(
id=id,
name=name,
description=description,
subject_id=subject_id)
def update_element(self, id_, name, description, subject_id):
topic = Topic(id=id_, name=name,
description=description,
subject_id=subject_id)
return self.topic_repository.update(topic)

def delete_topic(self, id):
topic = self.topic_repository.get_by_id(id)
def delete_element(self, id_):
topic = self.topic_repository.get_by_id(id_)
return self.topic_repository.delete(topic)

def delete_topic_by_subject_id(self, subject_id):
return self.topic_repository.delete_by_subject_id(subject_id)
def delete_element_by_object_id(self, element_id):
return self.topic_repository.delete_by_element_id(element_id)
17 changes: 10 additions & 7 deletions app/business/services/user_service.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# app/business/services/user_service.py
"""app/business/services/user_service.py
Module docstring ..."""

from app.business.models.user import User
from app.persistence.repositories.user_repository import UserRepository


class UserService:
"""Class docstring"""

def __init__(self):
self.user_repository: UserRepository = UserRepository()

def create_user(self, discord_id, username):
user = User(discord_id=discord_id, username=username)
return self.user_repository.create(user)

def get_user_by_id(self, id):
return self.user_repository.get_by_id(id)
def get_user_by_id(self, id_):
return self.user_repository.get_by_id(id_)

def get_user_by_discord_id(self, discord_id):
return self.user_repository.get_by_discord_id(discord_id)
return self.user_repository.get_by_element_id(discord_id)

def get_all_users(self):
return self.user_repository.get_all()
Expand All @@ -25,10 +28,10 @@ def update_user(self, discord_id, username):
user = User(discord_id=discord_id, username=username)
return self.user_repository.update(user)

def delete_user(self, id):
user = self.user_repository.get_by_id(id)
def delete_user(self, id_):
user = self.user_repository.get_by_id(id_)
return self.user_repository.delete(user)

def delete_user_by_discord_id(self, discord_id):
# user = self.user_repository.get_by_discord_id(discord_id)
return self.user_repository.discord_delete(discord_id)
return self.user_repository.delete_by_element_id(discord_id)
2 changes: 1 addition & 1 deletion app/database/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'check_same_thread': False})

# Create a session factory
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
SessionLocal: sessionmaker = sessionmaker(autocommit=False, autoflush=False, bind=engine)

# Base class for declarative class definitions
Base = declarative_base()
Expand Down
3 changes: 2 additions & 1 deletion app/database/models/subject_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# app/database/models/subject_model.py


from typing import List
from sqlalchemy import ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column, relationship
Expand All @@ -8,7 +9,7 @@


class SubjectModel(Base):
__tablename__ = 'subjects'
__tablename__: str = 'subjects'

id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(String)
Expand Down
5 changes: 4 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@


def main():
"""docstring"""

# Check if the tables need to be created
create_tables()

Expand All @@ -16,7 +18,8 @@ def main():
discord_token = os.getenv("DISCORD_TOKEN")
if not discord_token:
raise ValueError(
"DISCORD_TOKEN is not set in the environment variables")
"DISCORD_TOKEN is not set in the environment variables"
)

# Start the bot
bot = setup_bot(discord_token)
Expand Down
16 changes: 16 additions & 0 deletions app/persistence/mappers/mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""app.persistence.mappers.mapper.py
Module docstring ..."""

from abc import ABC


class Mapper(ABC):
"""Class docstring"""

@staticmethod
def element_to_model(element):
"""Method docstring"""

@staticmethod
def element_to_object(element):
"""Method docstring"""
Loading

0 comments on commit afab93e

Please sign in to comment.