Skip to content

Commit cb8341d

Browse files
committed
feat: TT-418 create added
1 parent a6fcb35 commit cb8341d

File tree

22 files changed

+307
-0
lines changed

22 files changed

+307
-0
lines changed

V2/serverless.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,13 @@ functions:
9696
- DELETE
9797
route: time-entries/{id}
9898
authLevel: anonymous
99+
100+
create_customer:
101+
handler: time_tracker/customers/interface.create_customer
102+
events:
103+
- http: true
104+
x-azure-settings:
105+
methods:
106+
- POST
107+
route: customer/
108+
authLevel: anonymous
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flake8: noqa
2+
from ._time_entries import create_customer
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flake8: noqa
2+
from ._create_customer import create_customer
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import dataclasses
2+
import json
3+
import typing
4+
5+
import azure.functions as func
6+
7+
from ... import _domain
8+
from ... import _infrastructure
9+
from time_tracker._infrastructure import DB
10+
11+
DEFAULT_FIELDS = ["id", "deleted", "status"]
12+
13+
14+
def create_customer(req: func.HttpRequest) -> func.HttpResponse:
15+
database = DB()
16+
customer_dao = _infrastructure.CustomersSQLDao(database)
17+
customer_service = _domain.CustomerService(customer_dao)
18+
use_case = _domain._use_cases.CreateCustomerUseCase(customer_service)
19+
20+
customer_data = req.get_json()
21+
validation_errors = _validate_customer(customer_data)
22+
if validation_errors:
23+
return func.HttpResponse(
24+
body=validation_errors, status_code=400, mimetype="application/json"
25+
)
26+
27+
customer_to_create = _domain.Customer(
28+
id=None,
29+
name=customer_data["name"],
30+
description=customer_data["description"],
31+
deleted=False,
32+
status=-1
33+
)
34+
35+
created_customer = use_case.create_customer(customer_to_create)
36+
37+
if not created_customer:
38+
return func.HttpResponse(
39+
body=json.dumps({'error': 'customer could not be created'}),
40+
status_code=500,
41+
mimetype="application/json"
42+
)
43+
44+
return func.HttpResponse(
45+
body=json.dumps(created_customer.__dict__),
46+
status_code=201,
47+
mimetype="application/json"
48+
)
49+
50+
51+
def _validate_customer(customer_data: dict) -> typing.List[str]:
52+
customer_fields = [field.name for field in dataclasses.fields(_domain.Customer)
53+
if field.name not in DEFAULT_FIELDS]
54+
if [field for field in customer_fields if field not in customer_data]:
55+
return b'Invalid format or structure of the attributes of the customer'
56+
return []
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
10+
def delete_time_entry(req: func.HttpRequest) -> func.HttpResponse:
11+
time_entry_dao = _infrastructure.TimeEntriesSQLDao(DB())
12+
time_entry_service = _domain.TimeEntryService(time_entry_dao)
13+
use_case = _domain._use_cases.DeleteTimeEntryUseCase(time_entry_service)
14+
15+
try:
16+
time_entry_id = int(req.route_params.get("id"))
17+
deleted_time_entry = use_case.delete_time_entry(time_entry_id)
18+
if not deleted_time_entry:
19+
return func.HttpResponse(
20+
body="Not found",
21+
status_code=404,
22+
mimetype="application/json"
23+
)
24+
25+
return func.HttpResponse(
26+
body=json.dumps(deleted_time_entry.__dict__, default=str),
27+
status_code=200,
28+
mimetype="application/json",
29+
)
30+
31+
except ValueError:
32+
return func.HttpResponse(
33+
body=b"Invalid Format ID",
34+
status_code=400,
35+
mimetype="application/json"
36+
)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# flake8: noqa
2+
from ._entities import Customer
3+
from ._persistence_contracts import CustomersDao
4+
from ._services import CustomerService
5+
from ._use_cases import (
6+
CreateCustomerUseCase,
7+
UpdateCustomerUseCase,
8+
GetAllCustomerUseCase,
9+
GetByIdCustomerUseCase,
10+
DeleteCustomerUseCase
11+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flake8: noqa
2+
from ._customer import Customer
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass(frozen=True)
5+
class Customer:
6+
id: int
7+
name: str
8+
description: str
9+
deleted: bool
10+
status: int
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# flake8: noqa
2+
from ._customers_dao import CustomersDao
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import abc
2+
import typing
3+
4+
from time_tracker.customers._domain import Customer
5+
6+
7+
class CustomersDao(abc.ABC):
8+
@abc.abstractmethod
9+
def create(self, data: Customer) -> Customer:
10+
pass
11+
12+
# @abc.abstractmethod
13+
# def update(self, data: Customer) -> Customer:
14+
# pass
15+
16+
# @abc.abstractmethod
17+
# def get_by_id(self, id: int) -> Customer:
18+
# pass
19+
20+
# @abc.abstractmethod
21+
# def get_all(self) -> typing.List[Customer]:
22+
# pass
23+
24+
# @abc.abstractmethod
25+
# def delete(self, id: int) -> Customer:
26+
# pass

0 commit comments

Comments
 (0)