-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: type hints and abstract classes
- the abstract classes service, repository and mapped are implemented, and some type hints are added to make the code more readable
- Loading branch information
Showing
21 changed files
with
428 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" | ||
")>" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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""" |
Oops, something went wrong.