diff --git a/apps/api/modules/transactions/handler.go b/apps/api/modules/transactions/handler.go index a42c32a..465cde8 100644 --- a/apps/api/modules/transactions/handler.go +++ b/apps/api/modules/transactions/handler.go @@ -34,13 +34,13 @@ func (handler Handler) GetTransactionList(w http.ResponseWriter, r *http.Request return } - transactions, err := handler.usecase.GetTransactionList(ctx, query, sortBy, order, skip, limit) + transactions, total, err := handler.usecase.GetTransactionList(ctx, query, sortBy, order, skip, limit) if err != nil { base.WriteError(w, apiContract.Error{Code: apiContract.SERVER_ERROR, Message: err.Error()}) return } - json.NewEncoder(w).Encode(apiContract.TransactionList200Response{Data: transactions}) + json.NewEncoder(w).Encode(apiContract.TransactionList200Response{Data: transactions, Meta: apiContract.MetaPage{total}}) } func (handler Handler) GetTransactionById(w http.ResponseWriter, r *http.Request) { diff --git a/apps/api/modules/transactions/repository.go b/apps/api/modules/transactions/repository.go index 96d869b..4c7c983 100644 --- a/apps/api/modules/transactions/repository.go +++ b/apps/api/modules/transactions/repository.go @@ -49,6 +49,20 @@ func (repo Repository) GetTransactionList(ctx context.Context, query string, sor return transactions, result.Error } +func (repo Repository) GetTransactionListTotal(ctx context.Context, query string) (int64, error) { + db := utils.GetDbFromCtx(ctx, repo.db) + var count int64 + result := db.Table("transactions").Where("deleted_at", nil) + + if query != "" { + result = result.Where("name LIKE ?", "%"+query+"%") + } + + result = result.Count(&count) + + return count, result.Error +} + func (repo Repository) GetTransactionById(ctx context.Context, id int64) (apiContract.Transaction, error) { db := utils.GetDbFromCtx(ctx, repo.db) diff --git a/apps/api/modules/transactions/usecase.go b/apps/api/modules/transactions/usecase.go index 53a8215..932e7d9 100644 --- a/apps/api/modules/transactions/usecase.go +++ b/apps/api/modules/transactions/usecase.go @@ -26,8 +26,18 @@ func NewUsecase(repository Repository, productRepository products.Repository, wa } } -func (usecase Usecase) GetTransactionList(ctx context.Context, query string, sortBy string, order string, skip int, limit int) ([]apiContract.Transaction, error) { - return usecase.repository.GetTransactionList(ctx, query, sortBy, order, skip, limit) +func (usecase Usecase) GetTransactionList(ctx context.Context, query string, sortBy string, order string, skip int, limit int) ([]apiContract.Transaction, int64, error) { + transactions, err := usecase.repository.GetTransactionList(ctx, query, sortBy, order, skip, limit) + if err != nil { + return []apiContract.Transaction{}, 0, err + } + + total, err := usecase.repository.GetTransactionListTotal(ctx, query) + if err != nil { + return []apiContract.Transaction{}, 0, err + } + + return transactions, total, nil } func (usecase Usecase) GetTransactionById(ctx context.Context, id int64) (apiContract.Transaction, error) { diff --git a/libs/api-contract/src/api.yaml b/libs/api-contract/src/api.yaml index f0e87a4..60854ef 100644 --- a/libs/api-contract/src/api.yaml +++ b/libs/api-contract/src/api.yaml @@ -773,11 +773,14 @@ paths: type: object required: - data + - meta properties: data: type: array items: $ref: '#/components/schemas/Transaction' + meta: + $ref: '#/components/schemas/MetaPage' '500': description: server error content: diff --git a/libs/ui/src/base/components/Pagination/Pagination.tsx b/libs/ui/src/base/components/Pagination/Pagination.tsx index 582a51c..2efa6a5 100644 --- a/libs/ui/src/base/components/Pagination/Pagination.tsx +++ b/libs/ui/src/base/components/Pagination/Pagination.tsx @@ -27,6 +27,7 @@ export const Pagination = ({ /> {Array.from(Array(totalPage)).map((_, index) => (