Skip to content

Commit

Permalink
[kai/child-routes] Children service delete (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
kai-wei-mo authored Dec 8, 2022
1 parent e46bb5b commit 06a030c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
20 changes: 14 additions & 6 deletions backend/python/app/resources/child_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion backend/python/app/rest/child_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions backend/python/app/services/implementations/child_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand Down
8 changes: 8 additions & 0 deletions backend/python/app/services/interfaces/child_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions backend/python/tests/functional/test_child_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 06a030c

Please sign in to comment.