fix
This commit is contained in:
90
backend-old/internal/model/request/common.go
Normal file
90
backend-old/internal/model/request/common.go
Normal file
@ -0,0 +1,90 @@
|
||||
package request
|
||||
|
||||
// PaginationRequest 分页请求
|
||||
type PaginationRequest struct {
|
||||
Page int `json:"page" form:"page" binding:"omitempty,min=1" validate:"omitempty,min=1"`
|
||||
Limit int `json:"limit" form:"limit" binding:"omitempty,min=1,max=100" validate:"omitempty,min=1,max=100"`
|
||||
}
|
||||
|
||||
// SortRequest 排序请求
|
||||
type SortRequest struct {
|
||||
Sort string `json:"sort" form:"sort" binding:"omitempty" validate:"omitempty"`
|
||||
Order string `json:"order" form:"order" binding:"omitempty,oneof=asc desc" validate:"omitempty,oneof=asc desc"`
|
||||
}
|
||||
|
||||
// SearchRequest 搜索请求
|
||||
type SearchRequest struct {
|
||||
Search string `json:"search" form:"search" binding:"omitempty,max=100" validate:"omitempty,max=100"`
|
||||
}
|
||||
|
||||
// BaseListRequest 基础列表请求
|
||||
type BaseListRequest struct {
|
||||
PaginationRequest
|
||||
SortRequest
|
||||
SearchRequest
|
||||
}
|
||||
|
||||
// IDRequest ID 请求
|
||||
type IDRequest struct {
|
||||
ID uint `json:"id" uri:"id" binding:"required,min=1" validate:"required,min=1"`
|
||||
}
|
||||
|
||||
// SlugRequest Slug 请求
|
||||
type SlugRequest struct {
|
||||
Slug string `json:"slug" uri:"slug" binding:"required,min=1" validate:"required,min=1"`
|
||||
}
|
||||
|
||||
// BulkIDsRequest 批量 ID 请求
|
||||
type BulkIDsRequest struct {
|
||||
IDs []uint `json:"ids" binding:"required,min=1" validate:"required,min=1"`
|
||||
}
|
||||
|
||||
// StatusRequest 状态请求
|
||||
type StatusRequest struct {
|
||||
IsActive *bool `json:"is_active" form:"is_active" binding:"omitempty"`
|
||||
}
|
||||
|
||||
// TimeRangeRequest 时间范围请求
|
||||
type TimeRangeRequest struct {
|
||||
StartDate string `json:"start_date" form:"start_date" binding:"omitempty" validate:"omitempty,datetime=2006-01-02"`
|
||||
EndDate string `json:"end_date" form:"end_date" binding:"omitempty" validate:"omitempty,datetime=2006-01-02"`
|
||||
}
|
||||
|
||||
// GetDefaultPagination 获取默认分页参数
|
||||
func (p *PaginationRequest) GetDefaultPagination() (int, int) {
|
||||
page := p.Page
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
limit := p.Limit
|
||||
if limit <= 0 {
|
||||
limit = 20
|
||||
}
|
||||
if limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
|
||||
return page, limit
|
||||
}
|
||||
|
||||
// GetDefaultSort 获取默认排序参数
|
||||
func (s *SortRequest) GetDefaultSort(defaultSort, defaultOrder string) (string, string) {
|
||||
sort := s.Sort
|
||||
if sort == "" {
|
||||
sort = defaultSort
|
||||
}
|
||||
|
||||
order := s.Order
|
||||
if order == "" {
|
||||
order = defaultOrder
|
||||
}
|
||||
|
||||
return sort, order
|
||||
}
|
||||
|
||||
// GetOffset 计算偏移量
|
||||
func (p *PaginationRequest) GetOffset() int {
|
||||
page, limit := p.GetDefaultPagination()
|
||||
return (page - 1) * limit
|
||||
}
|
||||
Reference in New Issue
Block a user