From b69dd31ce8dadac8983a9bdb76e86f38270d4422 Mon Sep 17 00:00:00 2001 From: Surajit Das <67738912+surajit003@users.noreply.github.com> Date: Sat, 4 Mar 2023 22:18:13 +0300 Subject: [PATCH] implement list all leads (#19) implement list all leads --- core/apps/crm/handlers/web.py | 15 +++++++++++++-- core/apps/crm/logic/lead.py | 4 ++-- core/apps/crm/repo/base.py | 2 +- core/apps/crm/repo/lead.py | 2 +- core/apps/crm/tests/handlers/test_web.py | 12 ++++++++++++ core/apps/crm/tests/logic/test_lead.py | 4 ++-- 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/core/apps/crm/handlers/web.py b/core/apps/crm/handlers/web.py index c50dfab..61e7f36 100644 --- a/core/apps/crm/handlers/web.py +++ b/core/apps/crm/handlers/web.py @@ -5,7 +5,7 @@ from core.apps.crm.domain.exceptions import InvalidPhoneNumberException from core.apps.crm.handlers.factories import InternalUseCaseFactory -from core.apps.crm.logic.lead import create_lead, get_lead, update_lead +from core.apps.crm.logic.lead import create_lead, get_lead, update_lead, get_leads from core.apps.crm.repo.exceptions import LeadExistException, LeadDoesNotExistException @@ -38,7 +38,18 @@ def post(self, request): return Response(lead.dict(), status=status.HTTP_201_CREATED) - def get(self, request, lead_id): + def get(self, request, lead_id=None): + if not lead_id: + leads_dict = {} + if leads := get_leads(lead_repo=InternalUseCaseFactory().get_repo()): + leads_dict = [lead.__dict__ for lead in leads] + + return Response( + { + "message": leads_dict, + }, + status=status.HTTP_200_OK, + ) try: lead = get_lead( lead_id=lead_id, lead_repo=InternalUseCaseFactory().get_repo() diff --git a/core/apps/crm/logic/lead.py b/core/apps/crm/logic/lead.py index 8ae10ec..1fffdc4 100644 --- a/core/apps/crm/logic/lead.py +++ b/core/apps/crm/logic/lead.py @@ -21,5 +21,5 @@ def get_lead(lead_id: UUID, lead_repo: AbstractLeadRepository) -> Optional[LeadE return lead_repo.get(lead_id) -def list_all(lead_repo: AbstractLeadRepository) -> Optional[Sequence[LeadEntity]]: - return lead_repo.list_all() +def get_leads(lead_repo: AbstractLeadRepository) -> Optional[Sequence[LeadEntity]]: + return lead_repo.get_all() diff --git a/core/apps/crm/repo/base.py b/core/apps/crm/repo/base.py index bba6fb9..182a831 100644 --- a/core/apps/crm/repo/base.py +++ b/core/apps/crm/repo/base.py @@ -19,5 +19,5 @@ def delete(self, lead_id: UUID): ... @abstractmethod - def list_all(self): + def get_all(self): ... diff --git a/core/apps/crm/repo/lead.py b/core/apps/crm/repo/lead.py index e9cd191..da3b78b 100644 --- a/core/apps/crm/repo/lead.py +++ b/core/apps/crm/repo/lead.py @@ -39,6 +39,6 @@ def delete(self, lead_id: UUID) -> None: instance = self._get_instance(lead_id) return instance.delete() - def list_all(self) -> Optional[Sequence[LeadEntity]]: + def get_all(self) -> Optional[Sequence[LeadEntity]]: leads = Lead.objects.all() return [LeadEntity(**lead.__dict__) for lead in leads] diff --git a/core/apps/crm/tests/handlers/test_web.py b/core/apps/crm/tests/handlers/test_web.py index bf94e84..a29d538 100644 --- a/core/apps/crm/tests/handlers/test_web.py +++ b/core/apps/crm/tests/handlers/test_web.py @@ -91,6 +91,18 @@ def test_get_lead(client, lead_factory): assert resp_json["id"] == str(lead.id) +def test_leads(client, lead_factory): + lead_factory() + lead_factory() + response = client.get( + "/api/v1/leads/", + ) + resp_json = response.json() + assert response.status_code == 200 + assert resp_json + assert len(resp_json["message"]) == 2 + + def test_get_lead_raises_lead_does_not_exist_error(client): response = client.get( "/api/v1/leads/1082cef2-de55-4683-b78c-79e6b5ef8f6b/", diff --git a/core/apps/crm/tests/logic/test_lead.py b/core/apps/crm/tests/logic/test_lead.py index 0bafe72..d8ffebb 100644 --- a/core/apps/crm/tests/logic/test_lead.py +++ b/core/apps/crm/tests/logic/test_lead.py @@ -1,6 +1,6 @@ import pytest -from core.apps.crm.logic.lead import create_lead, list_all, update_lead +from core.apps.crm.logic.lead import create_lead, get_leads, update_lead pytestmark = pytest.mark.django_db @@ -30,6 +30,6 @@ def test_update_lead(lead_factory, lead_repo): def test_list_all(lead_factory, lead_repo): lead_factory() lead_factory() - leads = list_all(lead_repo) + leads = get_leads(lead_repo) assert leads assert len(leads) == 2