update doc list api antrian + pagination
This commit is contained in:
@@ -14,6 +14,11 @@ type BaseResponse[T any] struct {
|
||||
Data T `json:"data" swaggertype:"object"`
|
||||
}
|
||||
|
||||
type BaseResponsePaginate[T any] struct {
|
||||
BaseResponse[T]
|
||||
Paginate PaginationInfo
|
||||
}
|
||||
|
||||
func ToBaseResponse[T any](data T, isSuccess bool, code int, message string) BaseResponse[T] {
|
||||
return BaseResponse[T]{
|
||||
Success: true,
|
||||
@@ -22,3 +27,15 @@ func ToBaseResponse[T any](data T, isSuccess bool, code int, message string) Bas
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
func ToBaseResponsePaginate[T any](data T, paginate PaginationInfo, isSuccess bool, code int, message string) BaseResponsePaginate[T] {
|
||||
return BaseResponsePaginate[T]{
|
||||
BaseResponse: BaseResponse[T]{
|
||||
Success: true,
|
||||
Code: 200,
|
||||
Message: message,
|
||||
Data: data,
|
||||
},
|
||||
Paginate: paginate,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package shared
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PaginationInfo struct {
|
||||
Limit int
|
||||
Offset int
|
||||
Total int
|
||||
TotalPages int
|
||||
CurrentPage int
|
||||
HasNext bool
|
||||
HasPrev bool
|
||||
}
|
||||
|
||||
func ParseQueryLimit(c *gin.Context) int {
|
||||
limit := 10 // default
|
||||
l, err := strconv.Atoi(c.Query("limit"))
|
||||
if err == nil {
|
||||
if l > 100 {
|
||||
limit = 100 // max limit default
|
||||
} else if l < 100 && l > 0 {
|
||||
limit = l
|
||||
}
|
||||
}
|
||||
|
||||
return limit
|
||||
}
|
||||
|
||||
func ParseQueryOffset(c *gin.Context) int {
|
||||
offset := 0
|
||||
o, err := strconv.Atoi(c.Query("offset"))
|
||||
if err == nil {
|
||||
if o > 0 {
|
||||
offset = o
|
||||
}
|
||||
}
|
||||
return offset
|
||||
}
|
||||
|
||||
func (source *PaginationInfo) CalculatePagingInfo() *PaginationInfo {
|
||||
result := source
|
||||
|
||||
result.TotalPages, result.CurrentPage = 0, 1
|
||||
if result.Limit > 0 {
|
||||
result.TotalPages = (result.Total + result.Limit - 1) / result.Limit
|
||||
result.CurrentPage = (result.Offset / result.Limit) + 1
|
||||
}
|
||||
|
||||
result.HasNext = result.Offset+result.Limit < result.Total
|
||||
result.HasPrev = result.Offset > 0
|
||||
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user