added get orders

parent 22ac274b
...@@ -33,3 +33,6 @@ class OrderController(BaseController): ...@@ -33,3 +33,6 @@ class OrderController(BaseController):
except common_exc.NotFoundException as e: except common_exc.NotFoundException as e:
raise http_exc.HTTPNotFoundException(detail=str(e)) raise http_exc.HTTPNotFoundException(detail=str(e))
async def get_list(self, **kwargs):
return await self.order_service.get_order_list(**kwargs)
...@@ -3,7 +3,7 @@ from uuid import UUID ...@@ -3,7 +3,7 @@ from uuid import UUID
import fastapi as fa import fastapi as fa
from .ctrl import OrderController from .ctrl import OrderController
from .schemas import OrderPostSchema, OrderIdSchema, OrderStatusSchema from .schemas import OrderPostSchema, OrderIdSchema, OrderStatusSchema, OrderListSchema
router = fa.APIRouter(prefix='/orders', tags=['Order']) router = fa.APIRouter(prefix='/orders', tags=['Order'])
...@@ -25,3 +25,8 @@ async def accepted_order(body: OrderIdSchema): ...@@ -25,3 +25,8 @@ async def accepted_order(body: OrderIdSchema):
async def get_order_status(id: UUID): async def get_order_status(id: UUID):
order = await ctrl.get_order_status(id) order = await ctrl.get_order_status(id)
return OrderStatusSchema.model_validate(order) return OrderStatusSchema.model_validate(order)
@router.get('')
async def get_orders(page: int = 1, size: int = 10):
return await ctrl.get_list(page=page, size=size)
import datetime
from typing import List from typing import List
from uuid import UUID from uuid import UUID
...@@ -6,10 +7,6 @@ from pydantic import BaseModel, ConfigDict ...@@ -6,10 +7,6 @@ from pydantic import BaseModel, ConfigDict
from api.product.schemas import ProductPostSchema, ProductOrderGetSchema from api.product.schemas import ProductPostSchema, ProductOrderGetSchema
class OrderBaseSchema(BaseModel):
name: str | None = None
class OrderIdSchema(BaseModel): class OrderIdSchema(BaseModel):
id: UUID id: UUID
...@@ -29,3 +26,10 @@ class OrderGetSchema(OrderIdSchema): ...@@ -29,3 +26,10 @@ class OrderGetSchema(OrderIdSchema):
class OrderStatusSchema(BaseModel): class OrderStatusSchema(BaseModel):
id: UUID id: UUID
status: str status: str
class OrderListSchema(BaseModel):
id: UUID
status: str
created_at: datetime.datetime
updated_at: datetime.datetime
...@@ -2,7 +2,7 @@ from uuid import UUID ...@@ -2,7 +2,7 @@ from uuid import UUID
from db.repositories.order import OrderRepository from db.repositories.order import OrderRepository
from db.repositories.product import ProductRepository from db.repositories.product import ProductRepository
from .schemas import OrderGetSchema from .schemas import OrderGetSchema, OrderListSchema, OrderStatusSchema
from exceptions import common as common_exc, http as http_exc from exceptions import common as common_exc, http as http_exc
from db.models import ProductOrder, Order from db.models import ProductOrder, Order
...@@ -57,3 +57,20 @@ class OrderService: ...@@ -57,3 +57,20 @@ class OrderService:
async def get_order_status(self, id: UUID): async def get_order_status(self, id: UUID):
order = await self.order_repository.get(id) order = await self.order_repository.get(id)
return {'id': order.id, 'status': order.status} return {'id': order.id, 'status': order.status}
async def get_order_list(self, **kwargs):
orders = await self.order_repository.get_list(**kwargs)
return {
"page": orders['page'],
"size": orders['size'],
"total": orders['total'],
"result": [
OrderListSchema.model_validate(
{
'id': order.id, 'status': order.status,
'created_at': order.created_at, 'updated_at': order.updated_at
}
) for order in orders['result']
]
}
import math
from .base import BaseRepository from .base import BaseRepository
from ..models import Order from ..models import Order
class OrderRepository(BaseRepository): class OrderRepository(BaseRepository):
model = Order model = Order
async def get_list(self, page, size, **kwargs):
query = self.model.all()
offset_min = (page - 1) * size
offset_max = page * size
data = await query
result = {
"page": page,
"size": size,
"total": math.ceil(len(data) / size) - 1,
"result": data[offset_min:offset_max]
}
return result
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment