hybrid pagination
This commit is contained in:
@@ -62,7 +62,7 @@ type (
|
||||
|
||||
type FeedbackListResponse struct {
|
||||
Feedback []*FeedbackResponse `json:"feedback"`
|
||||
Meta *response.Meta `json:"meta"`
|
||||
Meta *response.Meta `json:"-"`
|
||||
}
|
||||
|
||||
func (f *Feedback) ToResponse(tender *tenderResponse, company *companyResponse) *FeedbackResponse {
|
||||
|
||||
@@ -60,12 +60,20 @@ func (h *Handler) Search(c echo.Context) error {
|
||||
return response.ValidationError(c, "Invalid request data", err.Error())
|
||||
}
|
||||
|
||||
feedbackList, err := h.service.Search(c.Request().Context(), *form, response.NewPagination(c))
|
||||
pagination, err := response.NewPagination(c)
|
||||
if err != nil {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
|
||||
feedbackList, err := h.service.Search(c.Request().Context(), *form, pagination)
|
||||
if err != nil {
|
||||
if response.IsListPaginationError(err) {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to retrieve feedback list")
|
||||
}
|
||||
|
||||
return response.Success(c, feedbackList.Feedback, "feedback list retrieved successfully")
|
||||
return response.SuccessWithMeta(c, feedbackList, feedbackList.Meta, "feedback list retrieved successfully")
|
||||
}
|
||||
|
||||
// GetFeedbackByID retrieves feedback by ID with loaded related data
|
||||
@@ -217,13 +225,20 @@ func (h *Handler) PublicList(c echo.Context) error {
|
||||
}
|
||||
form.Company = &companyID
|
||||
|
||||
// Call service
|
||||
result, err := h.service.Search(c.Request().Context(), *form, response.NewPagination(c))
|
||||
pagination, err := response.NewPagination(c)
|
||||
if err != nil {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
|
||||
result, err := h.service.Search(c.Request().Context(), *form, pagination)
|
||||
if err != nil {
|
||||
if response.IsListPaginationError(err) {
|
||||
return response.PaginationBadRequest(c, err)
|
||||
}
|
||||
return response.InternalServerError(c, "Failed to list feedback")
|
||||
}
|
||||
|
||||
return response.Success(c, result, "Feedback list retrieved successfully")
|
||||
return response.SuccessWithMeta(c, result, result.Meta, "Feedback list retrieved successfully")
|
||||
}
|
||||
|
||||
// PublicGetFeedback retrieves feedback by ID
|
||||
|
||||
@@ -16,7 +16,7 @@ type Repository interface {
|
||||
// Basic CRUD operations
|
||||
Create(ctx context.Context, feedback *Feedback) error
|
||||
Update(ctx context.Context, feedback *Feedback) error
|
||||
Search(ctx context.Context, criteria SearchForm, pagination *response.Pagination) ([]Feedback, int64, error)
|
||||
Search(ctx context.Context, criteria SearchForm, pagination *response.Pagination) (*orm.PaginatedResult[Feedback], error)
|
||||
GetByID(ctx context.Context, id string) (*Feedback, error)
|
||||
GetByCompanyID(ctx context.Context, companyID string, tender string) (*Feedback, error)
|
||||
GetByTenderID(ctx context.Context, tenderID, companyID string) (*Feedback, error)
|
||||
@@ -172,37 +172,31 @@ func (r *feedbackRepository) Delete(ctx context.Context, id string) error {
|
||||
}
|
||||
|
||||
// Search searches feedback based on criteria
|
||||
func (r *feedbackRepository) Search(ctx context.Context, criteria SearchForm, pagination *response.Pagination) ([]Feedback, int64, error) {
|
||||
func (r *feedbackRepository) Search(ctx context.Context, criteria SearchForm, pagination *response.Pagination) (*orm.PaginatedResult[Feedback], error) {
|
||||
filter := r.buildSearchFilter(criteria)
|
||||
|
||||
// Build sort
|
||||
sort := "created_at"
|
||||
if pagination.SortBy != "" {
|
||||
sort = pagination.SortBy
|
||||
}
|
||||
|
||||
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 users
|
||||
result, err := r.repo.FindAll(ctx, filter, paginationBuilder)
|
||||
mongoPagination, err := orm.BuildListPagination(
|
||||
pagination.Limit,
|
||||
pagination.Offset,
|
||||
pagination.Cursor,
|
||||
pagination.SortBy,
|
||||
pagination.SortOrder,
|
||||
filter,
|
||||
orm.ListPaginationOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to list users", map[string]interface{}{
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result, err := r.repo.FindAll(ctx, filter, mongoPagination)
|
||||
if err != nil {
|
||||
r.logger.Error("Failed to search feedback", map[string]interface{}{
|
||||
"error": err.Error(),
|
||||
})
|
||||
return nil, 0, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return result.Items, result.TotalCount, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Stats calculates feedback statistics for a company
|
||||
|
||||
@@ -152,7 +152,7 @@ func (s *feedbackService) Get(ctx context.Context, id string) (*FeedbackResponse
|
||||
}
|
||||
|
||||
func (s *feedbackService) Search(ctx context.Context, criteria SearchForm, pagination *response.Pagination) (*FeedbackListResponse, error) {
|
||||
feedbacks, total, err := s.feedbackRepo.Search(ctx, criteria, pagination)
|
||||
page, err := s.feedbackRepo.Search(ctx, criteria, pagination)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to list feedback", map[string]interface{}{
|
||||
"criteria": criteria,
|
||||
@@ -163,16 +163,10 @@ func (s *feedbackService) Search(ctx context.Context, criteria SearchForm, pagin
|
||||
return nil, err
|
||||
}
|
||||
|
||||
meta := &response.Meta{
|
||||
Total: int(total),
|
||||
Limit: pagination.Limit,
|
||||
Offset: pagination.Offset,
|
||||
Page: (pagination.Offset / pagination.Limit) + 1,
|
||||
Pages: int((total + int64(pagination.Limit) - 1) / int64(pagination.Limit)),
|
||||
}
|
||||
meta := pagination.ListMeta(page.TotalCount, page.NextCursor, page.HasMore, page.PageOffset)
|
||||
|
||||
result := make([]*FeedbackResponse, len(feedbacks))
|
||||
for i, feedback := range feedbacks {
|
||||
result := make([]*FeedbackResponse, len(page.Items))
|
||||
for i, feedback := range page.Items {
|
||||
tender, company, _ := s.getDetails(ctx, feedback.TenderID, *feedback.CompanyID)
|
||||
result[i] = feedback.ToResponse(tender, company)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user