From 06a030c5aead79e5e1b0e2da58772b8ab8e7f6ad Mon Sep 17 00:00:00 2001 From: Kai Wei Mo <66972679+kai-wei-mo@users.noreply.github.com> Date: Wed, 7 Dec 2022 19:52:37 -0500 Subject: [PATCH] [kai/child-routes] Children service delete (#111) --- backend/python/app/resources/child_dto.py | 20 +++++++++++++------ backend/python/app/rest/child_routes.py | 2 +- .../services/implementations/child_service.py | 15 ++++++++++++++ .../app/services/interfaces/child_service.py | 8 ++++++++ .../tests/functional/test_child_service.py | 8 ++++++++ 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/backend/python/app/resources/child_dto.py b/backend/python/app/resources/child_dto.py index 8092ecdfa..16cd34106 100644 --- a/backend/python/app/resources/child_dto.py +++ b/backend/python/app/resources/child_dto.py @@ -22,16 +22,10 @@ def __init__(self, **kwargs): def validate(self): error_list = [] - if self.intake_id and not type(self.intake_id) == int: - error_list.append("The intake_id supplied is invalid") if not self.first_name or not type(self.first_name) == str: error_list.append("The first_name supplied is invalid") if not self.last_name or not type(self.last_name) == str: error_list.append("The last_name supplied is invalid") - if self.date_of_birth and not isinstance(self.date_of_birth, datetime.date): - error_list.append("The date_of_birth supplied is invalid") - if self.cpin_number and not type(self.cpin_number) == str: - error_list.append("The cpin_number supplied is invalid") if ( not self.child_service_worker_id or not type(self.child_service_worker_id) == int @@ -50,4 +44,18 @@ def validate(self): "has_kinship_provider and has_foster_placement cannot have the same boolean value" ) + # optional args + if self.intake_id and not type(self.intake_id) == int: + error_list.append("The intake_id supplied is invalid") + if self.date_of_birth: + if type(self.date_of_birth) == str: + try: + datetime.datetime.strptime(self.date_of_birth, "%Y-%m-%d") + except ValueError: + error_list.append("The date_of_birth supplied is invalid") + elif not type(self.date_of_birth) == datetime.date: + error_list.append("The date_of_birth supplied is invalid") + if self.cpin_number and not type(self.cpin_number) == str: + error_list.append("The cpin_number supplied is invalid") + return error_list diff --git a/backend/python/app/rest/child_routes.py b/backend/python/app/rest/child_routes.py index 9051540bf..16d89a436 100644 --- a/backend/python/app/rest/child_routes.py +++ b/backend/python/app/rest/child_routes.py @@ -4,7 +4,7 @@ from ..middlewares.auth import require_authorization_by_role from ..middlewares.validate import validate_request -from ..resources.child_dto import ChildDTO +from ..resources.child_dto import ChildDTO, CreateChildDTO from ..services.implementations.child_service import ChildService child_service = ChildService(current_app.logger) diff --git a/backend/python/app/services/implementations/child_service.py b/backend/python/app/services/implementations/child_service.py index 480f96900..e71280f6f 100644 --- a/backend/python/app/services/implementations/child_service.py +++ b/backend/python/app/services/implementations/child_service.py @@ -8,6 +8,20 @@ class ChildService(IChildService): def __init__(self, logger): self.logger = logger + def get_all_children(self): + try: + children = Child.query.all() + return list( + map( + lambda child: ChildDTO( + **child.__dict__, + ), + children, + ) + ) + except Exception as error: + raise error + def add_new_child(self, child: CreateChildDTO): try: if not child: @@ -20,6 +34,7 @@ def add_new_child(self, child: CreateChildDTO): new_child_entry = Child(**child.__dict__) db.session.add(new_child_entry) db.session.commit() + return ChildDTO(**new_child_entry.to_dict()) except Exception as error: db.session.rollback() diff --git a/backend/python/app/services/interfaces/child_service.py b/backend/python/app/services/interfaces/child_service.py index 65b83fefc..85f72bb4d 100644 --- a/backend/python/app/services/interfaces/child_service.py +++ b/backend/python/app/services/interfaces/child_service.py @@ -2,6 +2,14 @@ class IChildService(ABC): + @abstractmethod + def get_all_children(self): + """Get all children + :rtype: list of ChildDTO + :raises Exception: if error occurs in the database layer + """ + pass + @abstractmethod def add_new_child(self, child): """Adds the specified child to the child table and returns diff --git a/backend/python/tests/functional/test_child_service.py b/backend/python/tests/functional/test_child_service.py index 71bb4c13f..9cb07d796 100644 --- a/backend/python/tests/functional/test_child_service.py +++ b/backend/python/tests/functional/test_child_service.py @@ -65,6 +65,8 @@ def child_service(): def seed_database(): + empty_database() + dummy_user = User(**DUMMY_USER_DATA) db.session.add(dummy_user) db.session.commit() @@ -80,9 +82,15 @@ def seed_database(): db.session.add(dummy_child) db.session.commit() + dummy_child = Child(**DUMMY_CHILD_DATA) + db.session.add(dummy_child) + db.session.commit() + def empty_database(): Child.query.delete() + db.session.commit() + Intake.query.delete() DaytimeContact.query.delete() User.query.delete()