chore: sync
This commit is contained in:
@@ -2,9 +2,11 @@ package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"sproutworkcollect-backend/internal/model"
|
||||
"sproutworkcollect-backend/internal/service"
|
||||
)
|
||||
|
||||
@@ -46,16 +48,26 @@ func (h *PublicHandler) GetWorks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
responses := make([]any, len(works))
|
||||
for i, w := range works {
|
||||
responses[i] = h.workSvc.BuildResponse(w)
|
||||
page, pageSize, paged := resolvePageParams(c)
|
||||
total := len(works)
|
||||
worksPage := paginateWorks(works, page, pageSize)
|
||||
|
||||
ids := make([]string, len(worksPage))
|
||||
for i, w := range worksPage {
|
||||
ids[i] = w.WorkID
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
resp := gin.H{
|
||||
"success": true,
|
||||
"data": responses,
|
||||
"total": len(responses),
|
||||
})
|
||||
"data": ids,
|
||||
"total": total,
|
||||
}
|
||||
if paged {
|
||||
resp["page"] = page
|
||||
resp["page_size"] = pageSize
|
||||
resp["total_pages"] = calcTotalPages(total, pageSize)
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// GetWorkDetail handles GET /api/works/:work_id
|
||||
@@ -91,16 +103,90 @@ func (h *PublicHandler) SearchWorks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
responses := make([]any, len(works))
|
||||
for i, w := range works {
|
||||
responses[i] = h.workSvc.BuildResponse(w)
|
||||
page, pageSize, paged := resolvePageParams(c)
|
||||
total := len(works)
|
||||
worksPage := paginateWorks(works, page, pageSize)
|
||||
|
||||
ids := make([]string, len(worksPage))
|
||||
for i, w := range worksPage {
|
||||
ids[i] = w.WorkID
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
resp := gin.H{
|
||||
"success": true,
|
||||
"data": responses,
|
||||
"total": len(responses),
|
||||
})
|
||||
"data": ids,
|
||||
"total": total,
|
||||
}
|
||||
if paged {
|
||||
resp["page"] = page
|
||||
resp["page_size"] = pageSize
|
||||
resp["total_pages"] = calcTotalPages(total, pageSize)
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func resolvePageParams(c *gin.Context) (int, int, bool) {
|
||||
page, hasPage := parsePositiveInt(c, "page")
|
||||
pageSize, hasSize := parsePositiveInt(c, "page_size")
|
||||
if !hasSize {
|
||||
pageSize, hasSize = parsePositiveInt(c, "pageSize")
|
||||
}
|
||||
if !hasSize {
|
||||
pageSize, hasSize = parsePositiveInt(c, "limit")
|
||||
}
|
||||
if !hasPage && !hasSize {
|
||||
return 0, 0, false
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 12
|
||||
}
|
||||
if pageSize > 200 {
|
||||
pageSize = 200
|
||||
}
|
||||
return page, pageSize, true
|
||||
}
|
||||
|
||||
func parsePositiveInt(c *gin.Context, key string) (int, bool) {
|
||||
raw, ok := c.GetQuery(key)
|
||||
if !ok {
|
||||
return 0, false
|
||||
}
|
||||
val, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
return 0, false
|
||||
}
|
||||
return val, true
|
||||
}
|
||||
|
||||
func paginateWorks(works []*model.WorkConfig, page, pageSize int) []*model.WorkConfig {
|
||||
if pageSize <= 0 {
|
||||
return works
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
start := (page - 1) * pageSize
|
||||
if start >= len(works) {
|
||||
return []*model.WorkConfig{}
|
||||
}
|
||||
end := start + pageSize
|
||||
if end > len(works) {
|
||||
end = len(works)
|
||||
}
|
||||
return works[start:end]
|
||||
}
|
||||
|
||||
func calcTotalPages(total, pageSize int) int {
|
||||
if pageSize <= 0 {
|
||||
return 1
|
||||
}
|
||||
if total == 0 {
|
||||
return 0
|
||||
}
|
||||
return (total + pageSize - 1) / pageSize
|
||||
}
|
||||
|
||||
// GetCategories handles GET /api/categories
|
||||
|
||||
Reference in New Issue
Block a user