Skip to content

Commit

Permalink
add paginated get methods for orders
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim2121512 committed Dec 17, 2024
1 parent 4e3d0f9 commit 3aaf933
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 125 deletions.
103 changes: 44 additions & 59 deletions backend/src/core/entities/order/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ class Order(BaseModel):
default_factory=lambda: datetime.now(timezone.utc),
description="create order time"
)
related_worker: ObjectIdStr = Field(
default=None,
description="related worker id"
)

class Config:
json_encoders = {
Expand Down Expand Up @@ -83,22 +79,39 @@ class OrderCreate(BaseModel):
description="client who ordered"
)
delivery_type: str = Field(
...,
"to_door",
description="delivery type (to door, at pickup point)"
)
delivery_state: str = Field(
...,
"delivered",
description="delivery state (accepted, on the way, delivered)"
)
payment_type: str = Field(
...,
"cash",
description="payment type (cash, card)"
)
payment_state: str = Field(
...,
"paid",
description="payment state (paid, not paid)"
)


class OrderInDB(Order):
id: Optional[ObjectIdStr] = Field(
...,
default_factory=ObjectIdStr,
alias="_id",
description="Unique identifier of order"
)

class Config:
json_encoders = {
ObjectId: str,
datetime: lambda v: v.isoformat()
}

allow_population_by_field_name = True



class OrderCreated(BaseModel):
message: str = Field(
Expand All @@ -116,6 +129,10 @@ class OrderSummary(BaseModel):
alias="_id",
description="Unique identifier of order"
)
price: float = Field(
default=None,
description="price of leasing"
)
tools: List[ToolSummary] = Field(
...,
description="Order tools, must contain at least 1 tool"
Expand Down Expand Up @@ -146,6 +163,10 @@ class OrderSummary(BaseModel):
...,
description="payment state (paid, not paid)"
)
create_order_time: datetime = Field(
...,
description="create order time"
)

class Config:
json_encoders = {
Expand All @@ -156,60 +177,24 @@ class Config:
allow_population_by_field_name = True


class OrderForWorker(BaseModel):
id: Optional[ObjectIdStr] = Field(
...,
default_factory=ObjectIdStr,
alias="_id",
description="Unique identifier of order"
)
tools: List[ToolSummary] = Field(
...,
description="Order tools"
)
start_leasing: Optional[datetime] = Field(
...,
default_factory=lambda: datetime.now(timezone.utc),
description="start of leasing tools"
)
price: float = Field(
default=None,
description="price of leasing"
)
end_leasing: Optional[datetime] = Field(
...,
default_factory=lambda: datetime.now(timezone.utc),
description="end of leasing"
)
class OrderForWorker(OrderSummary):
client: ClientForWorker = Field(
...,
description="client who ordered"
)
delivery_type: str = Field(
...,
description="delivery type (to door, at pickup point)"
)
delivery_state: str = Field(
...,
description="delivery state (accepted, on the way, delivered)"
)
payment_type: str = Field(
...,
description="payment type (cash, card)"

class PaginatedOrdersResponseForWorker(BaseModel):
orders: List[OrderForWorker] = Field(
...
)
payment_state: str = Field(
...,
description="payment state (paid, not paid)"
)
create_order_time: datetime = Field(
...,
description="create order time"
totalNumber: int = Field(
...
)

class Config:
json_encoders = {
ObjectId: str,
datetime: lambda v: v.isoformat()
}

allow_population_by_field_name = True
class PaginatedOrdersResponseForClient(BaseModel):
orders: List[OrderSummary] = Field(
...
)
totalNumber: int = Field(
...
)
39 changes: 32 additions & 7 deletions backend/src/core/repositories/order_repos/iorder_repository.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from abc import ABC, abstractmethod
from typing import List

from src.core.entities.order.order import Order, OrderSummary, OrderForWorker
from optparse import Option
from typing import List, Optional

from src.core.entities.order.order import Order, OrderSummary, OrderForWorker, OrderInDB
from datetime import datetime

class IOrderRepository(ABC):
@abstractmethod
Expand All @@ -13,10 +14,6 @@ async def create(self, order: Order) -> str:
async def get_orders_by_client_id(self, client_id: str) -> List[OrderSummary]:
pass

@abstractmethod
async def get_orders_by_worker_id(self, worker_id: str) -> List[OrderForWorker]:
pass

@abstractmethod
async def get_order_by_id(self, order_id: str) -> Order:
pass
Expand All @@ -31,4 +28,32 @@ async def paid(self, order_id: str) -> bool:

@abstractmethod
async def exists_by_id(self, order_id: str) -> bool:
pass

@abstractmethod
async def get_paginated_orders(
self,
page: int,
page_size: int,
customer_ids: Optional[List[str]] = None,
tool_ids: Optional[List[str]] = None,
status: Optional[str] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
min_price: Optional[float] = None,
max_price: Optional[float] = None
) -> List[OrderInDB]:
pass

@abstractmethod
async def count_orders(
self,
customer_ids: Optional[List[str]] = None,
tool_ids: Optional[List[str]] = None,
status: Optional[str] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
min_price: Optional[float] = None,
max_price: Optional[float] = None
) -> int:
pass
8 changes: 8 additions & 0 deletions backend/src/core/repositories/tool_repos/itool_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ async def exists_by_id(self, tool_id: str) -> bool:
async def get_name_by_id(self, tool_id: str) -> Optional[str]:
pass

@abstractmethod
async def get_ids_by_names(self, names: List[str]) -> List[str]:
pass

@abstractmethod
async def get_ids_by_name(self, name: str) -> List[str]:
pass
Expand All @@ -62,3 +66,7 @@ async def count_tools(
max_price: Optional[float] = None
) -> int:
pass

@abstractmethod
async def get_tools_summaries_by_ids(self, tool_ids: List[str]) -> List[ToolSummary]:
pass
Loading

0 comments on commit 3aaf933

Please sign in to comment.