hybrid pagination
This commit is contained in:
@@ -169,8 +169,16 @@ func (h *Handler) Search(c echo.Context) error {
|
||||
return response.ValidationError(c, "Invalid request data", err.Error())
|
||||
}
|
||||
|
||||
result, err := h.service.SearchCategories(c.Request().Context(), form, response.NewPagination(c))
|
||||
pagination, err := response.NewPagination(c)
|
||||
if err != nil {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
|
||||
result, err := h.service.SearchCategories(c.Request().Context(), form, pagination)
|
||||
if err != nil {
|
||||
if response.IsListPaginationError(err) {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to search categories")
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ type Repository interface {
|
||||
GetByID(ctx context.Context, id string) (*Category, error)
|
||||
GetByIDs(ctx context.Context, ids []string) ([]Category, error)
|
||||
GetByName(ctx context.Context, name string) (*Category, error)
|
||||
Search(ctx context.Context, form *SearchForm, pagination *response.Pagination) ([]Category, int64, error)
|
||||
Search(ctx context.Context, form *SearchForm, pagination *response.Pagination) (*orm.PaginatedResult[Category], error)
|
||||
Delete(ctx context.Context, id string) error
|
||||
TogglePublish(ctx context.Context, id string) (bool, error)
|
||||
}
|
||||
@@ -178,7 +178,7 @@ func (r *categoryRepository) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
// Search retrieves categories with search and filters
|
||||
func (r *categoryRepository) Search(ctx context.Context, form *SearchForm, pagination *response.Pagination) ([]Category, int64, error) {
|
||||
func (r *categoryRepository) Search(ctx context.Context, form *SearchForm, pagination *response.Pagination) (*orm.PaginatedResult[Category], error) {
|
||||
// Build filter
|
||||
filter := bson.M{}
|
||||
|
||||
@@ -198,34 +198,28 @@ func (r *categoryRepository) Search(ctx context.Context, form *SearchForm, pagin
|
||||
filter["published"] = *form.Published
|
||||
}
|
||||
|
||||
// Build sort
|
||||
sort := "created_at"
|
||||
if pagination.SortBy != "" {
|
||||
sort = pagination.SortBy
|
||||
mongoPagination, err := orm.BuildListPagination(
|
||||
pagination.Limit,
|
||||
pagination.Offset,
|
||||
pagination.Cursor,
|
||||
pagination.SortBy,
|
||||
pagination.SortOrder,
|
||||
filter,
|
||||
orm.ListPaginationOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sortOrder := "desc"
|
||||
if pagination.SortOrder != "" {
|
||||
sortOrder = pagination.SortOrder
|
||||
}
|
||||
|
||||
// Build pagination
|
||||
paginationBuilder := orm.NewPaginationBuilder().
|
||||
Limit(pagination.Limit).
|
||||
Skip(pagination.Offset).
|
||||
SortBy(sort, sortOrder).
|
||||
Build()
|
||||
|
||||
// Use ORM to find categories
|
||||
result, err := r.ormRepo.FindAll(ctx, filter, paginationBuilder)
|
||||
result, err := r.ormRepo.FindAll(ctx, filter, mongoPagination)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to search categories", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, 0, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Items, result.TotalCount, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// TogglePublish toggles category publish status
|
||||
|
||||
@@ -177,23 +177,22 @@ func (s *categoryService) Delete(ctx context.Context, id string) error {
|
||||
// SearchCategories searches categories with filters
|
||||
func (s *categoryService) SearchCategories(ctx context.Context, form *SearchForm, pagination *response.Pagination) (*CategoryListResponse, error) {
|
||||
// Get categories using search
|
||||
categories, total, err := s.repository.Search(ctx, form, pagination)
|
||||
page, err := s.repository.Search(ctx, form, pagination)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to search categories", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, errors.New("failed to search categories")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Convert to responses
|
||||
var categoryResponses []*CategoryResponse
|
||||
for _, category := range categories {
|
||||
for _, category := range page.Items {
|
||||
categoryResponses = append(categoryResponses, category.ToResponse())
|
||||
}
|
||||
|
||||
return &CategoryListResponse{
|
||||
Categories: categoryResponses,
|
||||
Meta: pagination.Response(total),
|
||||
Meta: pagination.ListMeta(page.TotalCount, page.NextCursor, page.HasMore, page.PageOffset),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user