Skip to content

Commit c4fbb0f

Browse files
committed
feat: TT-418 create get_all, get_by_id and delete methods
1 parent cb8341d commit c4fbb0f

File tree

12 files changed

+187
-65
lines changed

12 files changed

+187
-65
lines changed

V2/serverless.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ service: azure-time-tracker
22

33
frameworkVersion: "2"
44

5+
#region Config
6+
57
provider:
68
name: azure
79
region: westus2
@@ -36,7 +38,14 @@ package:
3638
- "!.gitignore"
3739
- "!.git/**"
3840

41+
#endregion Fin Config
42+
43+
#region Functions
44+
3945
functions:
46+
47+
#region Activities
48+
4049
get_activities:
4150
handler: time_tracker/activities/interface.get_activities
4251
events:
@@ -77,6 +86,10 @@ functions:
7786
route: activities/
7887
authLevel: anonymous
7988

89+
#endregion fin Activities
90+
91+
#region Time-Entries
92+
8093
create_time_entry:
8194
handler: time_tracker/time_entries/interface.create_time_entry
8295
events:
@@ -97,6 +110,10 @@ functions:
97110
route: time-entries/{id}
98111
authLevel: anonymous
99112

113+
#endregion Fin TimeEntries
114+
115+
#region Customer
116+
100117
create_customer:
101118
handler: time_tracker/customers/interface.create_customer
102119
events:
@@ -106,3 +123,27 @@ functions:
106123
- POST
107124
route: customer/
108125
authLevel: anonymous
126+
127+
get_customers:
128+
handler: time_tracker/customers/interface.get_customers
129+
events:
130+
- http: true
131+
x-azure-settings:
132+
methods:
133+
- GET
134+
route: customers/{id:?}
135+
authLevel: anonymous
136+
137+
delete_customer:
138+
handler: time_tracker/customers/interface.delete_customer
139+
events:
140+
- http: true
141+
x-azure-settings:
142+
methods:
143+
- DELETE
144+
route: customers/{id}
145+
authLevel: anonymous
146+
147+
#endregion Fin Customer
148+
149+
#endregion Fin Funciones
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
# flake8: noqa
2-
from ._time_entries import create_customer
2+
from ._customers import create_customer
3+
from ._customers import get_customers
4+
from ._customers import delete_customer
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# flake8: noqa
2+
from ._create_customer import create_customer
3+
from ._get_customers import get_customers
4+
from ._delete_customer import delete_customer

V2/time_tracker/customers/_application/_time_entries/_create_customer.py renamed to V2/time_tracker/customers/_application/_customers/_create_customer.py

File renamed without changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import json
2+
3+
import azure.functions as func
4+
5+
from ... import _domain
6+
from ... import _infrastructure
7+
from time_tracker._infrastructure import DB
8+
9+
DATATYPE="application/json"
10+
11+
def delete_customer(req: func.HttpRequest) -> func.HttpResponse:
12+
customer_dao = _infrastructure.CustomersSQLDao(DB())
13+
customer_service = _domain.CustomerService(customer_dao)
14+
use_case = _domain._use_cases.DeleteCustomerUseCase(customer_service)
15+
16+
try:
17+
customer_id = int(req.route_params.get("id"))
18+
deleted_customer = use_case.delete_customer(customer_id)
19+
if not deleted_customer:
20+
return func.HttpResponse(
21+
body="Not found",
22+
status_code=404,
23+
mimetype=DATATYPE
24+
)
25+
26+
return func.HttpResponse(
27+
body=json.dumps(deleted_customer.__dict__, default=str),
28+
status_code=200,
29+
mimetype=DATATYPE,
30+
)
31+
32+
except ValueError:
33+
return func.HttpResponse(
34+
body=b"Invalid Format ID",
35+
status_code=400,
36+
mimetype=DATATYPE
37+
)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import json
2+
import logging
3+
4+
import azure.functions as func
5+
6+
from ... import _domain
7+
from ... import _infrastructure
8+
from time_tracker._infrastructure import DB
9+
10+
DATABASE = DB()
11+
12+
13+
def get_customers(req: func.HttpRequest) -> func.HttpResponse:
14+
logging.info(
15+
'Python HTTP trigger function processed a request to get an activity.'
16+
)
17+
customer_id = req.route_params.get('id')
18+
status_code = 200
19+
20+
try:
21+
if customer_id:
22+
response = _get_by_id(int(customer_id))
23+
if response == b'This customer does not exist':
24+
status_code = 404
25+
else:
26+
response = _get_all()
27+
28+
return func.HttpResponse(
29+
body=response, status_code=status_code, mimetype="application/json"
30+
)
31+
except ValueError:
32+
return func.HttpResponse(
33+
body=b"The id has an invalid format", status_code=400, mimetype="application/json"
34+
)
35+
36+
37+
def _get_by_id(customer_id: int) -> str:
38+
customer_use_case = _domain._use_cases.GetByIdCustomerUseCase(
39+
_create_customer_service(DATABASE)
40+
)
41+
customer = customer_use_case.get_customer_by_id(customer_id)
42+
43+
return json.dumps(customer.__dict__) if customer else b'This customer does not exist'
44+
45+
46+
def _get_all() -> str:
47+
customer_sql = _domain._use_cases.GetAllCustomerUseCase(
48+
_create_customer_service(DATABASE)
49+
)
50+
return json.dumps(
51+
[
52+
customer.__dict__
53+
for customer in customer_sql.get_all_customer()
54+
]
55+
)
56+
57+
58+
def _create_customer_service(db: DB) -> _domain.CustomerService:
59+
customer_sql = _infrastructure.CustomersSQLDao(db)
60+
return _domain.CustomerService(customer_sql)

V2/time_tracker/customers/_application/_time_entries/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

V2/time_tracker/customers/_application/_time_entries/_delete_time_entry.py

Lines changed: 0 additions & 36 deletions
This file was deleted.

V2/time_tracker/customers/_domain/_persistence_contracts/_customers_dao.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ def create(self, data: Customer) -> Customer:
1313
# def update(self, data: Customer) -> Customer:
1414
# pass
1515

16-
# @abc.abstractmethod
17-
# def get_by_id(self, id: int) -> Customer:
18-
# pass
16+
@abc.abstractmethod
17+
def get_by_id(self, id: int) -> Customer:
18+
pass
1919

20-
# @abc.abstractmethod
21-
# def get_all(self) -> typing.List[Customer]:
22-
# pass
20+
@abc.abstractmethod
21+
def get_all(self) -> typing.List[Customer]:
22+
pass
2323

24-
# @abc.abstractmethod
25-
# def delete(self, id: int) -> Customer:
26-
# pass
24+
@abc.abstractmethod
25+
def delete(self, id: int) -> Customer:
26+
pass

V2/time_tracker/customers/_domain/_services/_customer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ def create(self, data: Customer) -> Customer:
1414
# def update(self, data: Customer) -> Customer:
1515
# return self.customer_dao.update(data)
1616

17-
# def get_by_id(self, id: int) -> Customer:
18-
# return self.customer_dao.get_by_id(id)
17+
def get_by_id(self, id: int) -> Customer:
18+
return self.customer_dao.get_by_id(id)
1919

20-
# def get_all(self) -> typing.List[Customer]:
21-
# return self.customer_dao.get_all()
20+
def get_all(self) -> typing.List[Customer]:
21+
return self.customer_dao.get_all()
2222

23-
# def delete(self, id: int) -> Customer:
24-
# return self.customer_dao.delete(id)
23+
def delete(self, id: int) -> Customer:
24+
return self.customer_dao.delete(id)

0 commit comments

Comments
 (0)