improve the functions related to commenting on images

This commit is contained in:
landaiqing
2024-09-23 00:53:43 +08:00
parent 8d5d918a7d
commit 7aae53e533
32 changed files with 1004 additions and 86 deletions

52
utils/paginator.go Normal file
View File

@@ -0,0 +1,52 @@
package utils
import (
"gorm.io/gorm"
)
// 标准分页结构体接收最原始的DO
// 建议在外部再建一个字段一样的结构体用以将DO转换成DTO或VO
type Page[T any] struct {
CurrentPage int64 `json:"currentPage"`
PageSize int64 `json:"pageSize"`
Total int64 `json:"total"`
Pages int64 `json:"pages"`
Data []T `json:"data"`
}
// 各种查询条件先在query设置好后再放进来
func (page *Page[T]) SelectPages(query *gorm.DB) (e error) {
var model T
query.Model(&model).Count(&page.Total)
if page.Total == 0 {
page.Data = []T{}
return
}
e = query.Model(&model).Scopes(Paginate(page)).Find(&page.Data).Error
return
}
func Paginate[T any](page *Page[T]) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {
if page.CurrentPage <= 0 {
page.CurrentPage = 1
}
switch {
case page.PageSize > 10000:
page.PageSize = 10000 // 限制一下分页大小
case page.PageSize <= 0:
page.PageSize = 10
}
page.Pages = page.Total / page.PageSize
if page.Total%page.PageSize != 0 {
page.Pages++
}
p := page.CurrentPage
if page.CurrentPage > page.Pages {
p = page.Pages
}
size := page.PageSize
offset := int((p - 1) * size)
return db.Offset(offset).Limit(int(size))
}
}