github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/contract/pageable.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package contract
    19  
    20  import (
    21  	"net/http"
    22  
    23  	"github.com/mysteriumnetwork/go-rest/apierror"
    24  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    25  )
    26  
    27  const (
    28  	defaultPageSize = 50
    29  	defaultPage     = 1
    30  )
    31  
    32  // NewPaginationQuery creates pagination query with default values.
    33  func NewPaginationQuery() PaginationQuery {
    34  	return PaginationQuery{
    35  		PageSize: defaultPageSize,
    36  		Page:     defaultPage,
    37  	}
    38  }
    39  
    40  // PaginationQuery allows to page response items.
    41  type PaginationQuery struct {
    42  	// Number of items per page.
    43  	// in: query
    44  	// default: 50
    45  	PageSize int `json:"page_size"`
    46  
    47  	// Page to filter the items by.
    48  	// in: query
    49  	// default: 1
    50  	Page int `json:"page"`
    51  }
    52  
    53  // Bind creates and validates query from API request.
    54  func (q *PaginationQuery) Bind(request *http.Request) *apierror.APIError {
    55  	v := apierror.NewValidator()
    56  
    57  	qs := request.URL.Query()
    58  	if qStr := qs.Get("page_size"); qStr != "" {
    59  		if qVal, err := parseInt(qStr); err != nil {
    60  			v.Invalid("page_size", "Cannot parse page_size")
    61  		} else {
    62  			q.PageSize = *qVal
    63  		}
    64  	}
    65  	if qStr := qs.Get("page"); qStr != "" {
    66  		if qVal, err := parseInt(qStr); err != nil {
    67  			v.Invalid("page", "Cannot parse page")
    68  		} else {
    69  			q.Page = *qVal
    70  		}
    71  	}
    72  
    73  	return v.Err()
    74  }
    75  
    76  // NewPageableDTO maps to API pagination DTO.
    77  func NewPageableDTO(paginator *utils.Paginator) PageableDTO {
    78  	return PageableDTO{
    79  		Page:       paginator.Page(),
    80  		PageSize:   paginator.PageSize(),
    81  		TotalItems: paginator.Nums(),
    82  		TotalPages: paginator.PageNums(),
    83  	}
    84  }
    85  
    86  // PageableDTO holds pagination information.
    87  // swagger:model PageableDTO
    88  type PageableDTO struct {
    89  	// The current page of the items.
    90  	Page int `json:"page"`
    91  	// Number of items per page.
    92  	PageSize int `json:"page_size"`
    93  	// The total items.
    94  	TotalItems int `json:"total_items"`
    95  	// The last page of the items.
    96  	TotalPages int `json:"total_pages"`
    97  }