From c913336ade10efa4f142a7537636ee54e0ae68e5 Mon Sep 17 00:00:00 2001 From: AlexSserb Date: Wed, 27 Nov 2024 17:00:49 +0400 Subject: [PATCH] fix: fix pagination info schema and expense router responses --- schemas/base.py | 4 ++-- services/expenses.py | 19 +++++++++++-------- services/payroll.py | 5 ++++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/schemas/base.py b/schemas/base.py index 2c1e51b..96525a5 100644 --- a/schemas/base.py +++ b/schemas/base.py @@ -47,8 +47,8 @@ class PaginationSchema(BaseSchema): class PaginationInfoSchema(BaseSchema): - total_pages: int = 0 - total_items: int = 0 + total_pages: int + total_items: int class BaseEnumSchema(BaseSchema): diff --git a/services/expenses.py b/services/expenses.py index a7ad6d0..14afca9 100644 --- a/services/expenses.py +++ b/services/expenses.py @@ -30,7 +30,10 @@ class ExpensesService(BaseService): if not total_records: return GetAllExpensesResponse( expenses=[], - pagination_info=PaginationInfoSchema() + pagination_info=PaginationInfoSchema( + total_pages=0, + total_items=0 + ) ) total_items = total_records total_pages = math.ceil(total_records / pagination.items_per_page) @@ -120,7 +123,7 @@ class ExpensesService(BaseService): async def create_tag(self, request: CreateExpenseTagRequest) -> CreateExpenseTagResponse: tag = await self.get_tag_by_name(request.tag.name) if tag: - return UpdateExpenseResponse(ok=False, message='Ошибка. Такой тег уже есть.') + return CreateExpenseTagResponse(ok=False, message='Такой тег уже есть.') tag_dict = request.tag.model_dump() stmt = ( @@ -129,16 +132,16 @@ class ExpensesService(BaseService): ) await self.session.execute(stmt) await self.session.commit() - return UpdateExpenseResponse(ok=True, message='Тег успешно создан.') + return CreateExpenseTagResponse(ok=True, message='Тег успешно создан.') async def update_tag(self, request: UpdateExpenseTagRequest) -> UpdateExpenseTagResponse: tag = await self.get_tag_by_name(request.tag.name) if tag: - return UpdateExpenseTagResponse(ok=False, message='Ошибка. Тег с таким названием уже есть.') + return UpdateExpenseTagResponse(ok=False, message='Тег с таким названием уже есть.') tag = await self.get_tag_by_id(request.tag.id) if not tag: - return UpdateExpenseTagResponse(ok=False, message='Ошибка. Тег не найден.') + return UpdateExpenseTagResponse(ok=False, message='Тег не найден.') tag_dict = request.tag.model_dump() del tag_dict['id'] @@ -149,14 +152,14 @@ class ExpensesService(BaseService): ) await self.session.execute(stmt) await self.session.commit() - return UpdateExpenseResponse(ok=True, message='Тег успешно изменен.') + return UpdateExpenseTagResponse(ok=True, message='Тег успешно изменен.') async def delete_tag(self, tag_id: int) -> DeleteExpenseTagResponse: tag = await self.get_tag_by_id(tag_id) if not tag: - return DeleteExpenseTagResponse(ok=False, message='Ошибка. Тег не найден.') + return DeleteExpenseTagResponse(ok=False, message='Тег не найден.') if len(tag.expenses) > 0: - return DeleteExpenseTagResponse(ok=False, message='Ошибка. Тег прикреплен к записи о расходах.') + return DeleteExpenseTagResponse(ok=False, message='Тег прикреплен к записи о расходах.') stmt = ( delete(ExpenseTag) diff --git a/services/payroll.py b/services/payroll.py index d6b5580..b52097e 100644 --- a/services/payroll.py +++ b/services/payroll.py @@ -149,7 +149,10 @@ class PayrollService(BaseService): if not total_records: return GetPaymentRecordsResponse( payment_records=[], - pagination_info=PaginationInfoSchema() + pagination_info=PaginationInfoSchema( + total_pages=0, + total_items=0 + ) ) total_items = total_records total_pages = math.ceil(total_records / pagination.items_per_page)