[feat] negodata/backend: 상품·협력사 목록 서버 페이지네이션 + 검색/필터(카테고리·priority) + 상품 카테고리 distinct 엔드포인트
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
4587be5844
commit
1fef405e88
8 files changed
+269
-32
No files matched your search
@@ -6,8 +6,17 @@ from common.database.db_session_manager import DB_SESSION_MNG
|
||||
from common.database.model.models import items
|
||||
from common.enums import DBWRType, ErrorType
|
||||
from common.logger import LOG
|
||||
from common.models.gmodel import PageParams
|
||||
from crud.item_crud import IItemCRUD, ItemCRUD
|
||||
from router.v1.item.protocol import ItemData, Res_DeleteItem, Res_Item, Res_ItemList
|
||||
from router.v1.item.protocol import (
|
||||
ItemCategory,
|
||||
ItemData,
|
||||
Res_CheckCodes,
|
||||
Res_DeleteItem,
|
||||
Res_Item,
|
||||
Res_ItemCategories,
|
||||
Res_ItemList,
|
||||
)
|
||||
|
||||
|
||||
class ItemService:
|
||||
@@ -29,15 +38,14 @@ class ItemService:
|
||||
return ErrorType.ITEM_NOT_FOUND, None
|
||||
return ErrorType.SUCCESS, item
|
||||
|
||||
async def list_items(self, company_id: str, search, category, page: int, size: int) -> Res_ItemList:
|
||||
res = Res_ItemList(page=page, size=size)
|
||||
async def list_items(self, company_id: str, search, category, pg: PageParams) -> Res_ItemList:
|
||||
res = Res_ItemList(page=pg.page, size=pg.size)
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
skip = (page - 1) * size
|
||||
|
||||
err_type, rows, total = await DB_SESSION_MNG.execute_lambda(
|
||||
items.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.item_crud.search(s, company_uuid, search, category, skip, size),
|
||||
lambda s: self.item_crud.search(s, company_uuid, search, category, pg.skip, pg.size),
|
||||
)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
res.result.SetResult(err_type)
|
||||
@@ -46,6 +54,21 @@ class ItemService:
|
||||
res.total = total
|
||||
return res
|
||||
|
||||
async def list_categories(self, company_id: str) -> Res_ItemCategories:
|
||||
"""회사 전체 상품에서 distinct 카테고리(이름+타입)를 돌려준다. 카테고리 목록은 백엔드가 책임진다."""
|
||||
res = Res_ItemCategories()
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
err_type, rows = await DB_SESSION_MNG.execute_lambda(
|
||||
items.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.item_crud.distinct_categories(s, company_uuid),
|
||||
)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
res.result.SetResult(err_type)
|
||||
return res
|
||||
res.categories = [ItemCategory(name=r[0], category_type=r[1] if r[1] is not None else 1) for r in rows]
|
||||
return res
|
||||
|
||||
async def get_item(self, company_id: str, item_id: str) -> Res_Item:
|
||||
res = Res_Item()
|
||||
err_type, item = await self._fetch_owned(uuid.UUID(company_id), uuid.UUID(item_id))
|
||||
@@ -55,9 +78,41 @@ class ItemService:
|
||||
res.item = ItemData.model_validate(item)
|
||||
return res
|
||||
|
||||
async def check_codes(self, company_id: str, codes: list) -> Res_CheckCodes:
|
||||
"""업로드 즉시 호출: codes 중 같은 회사 DB 에 이미 있는 코드를 돌려준다(미리보기 사전검사)."""
|
||||
res = Res_CheckCodes()
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
err_type, existing = await DB_SESSION_MNG.execute_lambda(
|
||||
items.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.item_crud.existing_codes(s, company_uuid, codes),
|
||||
)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
res.result.SetResult(err_type)
|
||||
return res
|
||||
res.existing = list(existing)
|
||||
return res
|
||||
|
||||
async def create_item(self, company_id: str, user_id: str, data: dict) -> Res_Item:
|
||||
res = Res_Item()
|
||||
item = items(**data, company_id=uuid.UUID(company_id), user_id=uuid.UUID(user_id))
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
|
||||
# DB 중복코드 검증: 같은 회사에 동일 code 가 이미 있으면 거부(프론트는 받아온 목록만 보므로 여기서 최종 차단).
|
||||
code = data.get("code")
|
||||
if code:
|
||||
dup_err, exists = await DB_SESSION_MNG.execute_lambda(
|
||||
items.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.item_crud.code_exists(s, company_uuid, code),
|
||||
)
|
||||
if dup_err != ErrorType.SUCCESS:
|
||||
res.result.SetResult(dup_err)
|
||||
return res
|
||||
if exists:
|
||||
res.result.SetResult(ErrorType.ITEM_CODE_DUPLICATE)
|
||||
return res
|
||||
|
||||
item = items(**data, company_id=company_uuid, user_id=uuid.UUID(user_id))
|
||||
err_type = await DB_SESSION_MNG.execute_lambda_run(
|
||||
[items.DBType()],
|
||||
[lambda s: self.item_crud.add_item(s, item)],
|
||||
|
||||
@@ -6,8 +6,9 @@ from common.database.db_session_manager import DB_SESSION_MNG
|
||||
from common.database.model.models import suppliers
|
||||
from common.enums import DBWRType, ErrorType
|
||||
from common.logger import LOG
|
||||
from common.models.gmodel import PageParams
|
||||
from crud.supplier_crud import ISupplierCRUD, SupplierCRUD
|
||||
from router.v1.supplier.protocol import Res_DeleteSupplier, Res_Supplier, Res_SupplierList, SupplierData
|
||||
from router.v1.supplier.protocol import Res_CheckCodes, Res_DeleteSupplier, Res_Supplier, Res_SupplierList, SupplierData
|
||||
|
||||
|
||||
class SupplierService:
|
||||
@@ -29,15 +30,14 @@ class SupplierService:
|
||||
return ErrorType.SUPPLIER_NOT_FOUND, None
|
||||
return ErrorType.SUCCESS, supplier
|
||||
|
||||
async def list_suppliers(self, company_id: str, search, page: int, size: int) -> Res_SupplierList:
|
||||
res = Res_SupplierList(page=page, size=size)
|
||||
async def list_suppliers(self, company_id: str, search, priority, pg: PageParams) -> Res_SupplierList:
|
||||
res = Res_SupplierList(page=pg.page, size=pg.size)
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
skip = (page - 1) * size
|
||||
|
||||
err_type, rows, total = await DB_SESSION_MNG.execute_lambda(
|
||||
suppliers.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.supplier_crud.search(s, company_uuid, search, skip, size),
|
||||
lambda s: self.supplier_crud.search(s, company_uuid, search, priority, pg.skip, pg.size),
|
||||
)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
res.result.SetResult(err_type)
|
||||
@@ -55,9 +55,41 @@ class SupplierService:
|
||||
res.supplier = SupplierData.model_validate(supplier)
|
||||
return res
|
||||
|
||||
async def check_codes(self, company_id: str, codes: list) -> Res_CheckCodes:
|
||||
"""업로드 즉시 호출: codes 중 같은 회사 DB 에 이미 있는 코드를 돌려준다(미리보기 사전검사)."""
|
||||
res = Res_CheckCodes()
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
err_type, existing = await DB_SESSION_MNG.execute_lambda(
|
||||
suppliers.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.supplier_crud.existing_codes(s, company_uuid, codes),
|
||||
)
|
||||
if err_type != ErrorType.SUCCESS:
|
||||
res.result.SetResult(err_type)
|
||||
return res
|
||||
res.existing = list(existing)
|
||||
return res
|
||||
|
||||
async def create_supplier(self, company_id: str, user_id: str, data: dict) -> Res_Supplier:
|
||||
res = Res_Supplier()
|
||||
supplier = suppliers(**data, company_id=uuid.UUID(company_id), user_id=uuid.UUID(user_id))
|
||||
company_uuid = uuid.UUID(company_id)
|
||||
|
||||
# DB 중복코드 검증: 같은 회사에 동일 code 가 이미 있으면 거부(프론트는 받아온 목록만 보므로 여기서 최종 차단).
|
||||
code = data.get("code")
|
||||
if code:
|
||||
dup_err, exists = await DB_SESSION_MNG.execute_lambda(
|
||||
suppliers.DBType(),
|
||||
DBWRType.DB_READ.value,
|
||||
lambda s: self.supplier_crud.code_exists(s, company_uuid, code),
|
||||
)
|
||||
if dup_err != ErrorType.SUCCESS:
|
||||
res.result.SetResult(dup_err)
|
||||
return res
|
||||
if exists:
|
||||
res.result.SetResult(ErrorType.SUPPLIER_CODE_DUPLICATE)
|
||||
return res
|
||||
|
||||
supplier = suppliers(**data, company_id=company_uuid, user_id=uuid.UUID(user_id))
|
||||
err_type = await DB_SESSION_MNG.execute_lambda_run(
|
||||
[suppliers.DBType()],
|
||||
[lambda s: self.supplier_crud.add_supplier(s, supplier)],
|
||||
|
||||
Reference in new issue
Block a user