商品管理ui优化, 后台支持分页参数

This commit is contained in:
yml
2026-06-01 00:56:45 +08:00
parent 4437c3b439
commit aff108fc96
7 changed files with 503 additions and 73 deletions
+9
View File
@@ -54,6 +54,15 @@ type AdminListQuery struct {
Status string
ReviewStatus string
Limit int
Page int
PageSize int
}
type AdminListResult struct {
Items []ListingDTO `json:"items"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
type AdminActionRequest struct {
+18 -2
View File
@@ -95,12 +95,12 @@ func (h *Handler) ListAdmin(c *gin.Context) {
if !ok {
return
}
items, err := h.service.ListAdmin(query)
result, err := h.service.ListAdmin(query)
if err != nil {
writeListingError(c, err)
return
}
response.OK(c, gin.H{"items": items})
response.OK(c, result)
}
func (h *Handler) FindAdmin(c *gin.Context) {
@@ -347,6 +347,22 @@ func parseAdminListQuery(c *gin.Context) (AdminListQuery, bool) {
}
query.Limit = value
}
if raw := c.Query("page"); raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
response.BadRequest(c, "页码不正确")
return query, false
}
query.Page = value
}
if raw := c.Query("page_size"); raw != "" {
value, err := strconv.Atoi(raw)
if err != nil || value <= 0 {
response.BadRequest(c, "每页条数不正确")
return query, false
}
query.PageSize = value
}
query.Status = c.Query("status")
query.ReviewStatus = c.Query("review_status")
return query, true
+40 -12
View File
@@ -170,13 +170,47 @@ func (r *Repository) ListPendingReview() ([]ListingDTO, error) {
return rowsToDTO(rows), nil
}
func (r *Repository) ListAdmin(query AdminListQuery) ([]ListingDTO, error) {
limit := query.Limit
if limit <= 0 || limit > 500 {
limit = 200
func (r *Repository) ListAdmin(query AdminListQuery) (*AdminListResult, error) {
page := query.Page
if page <= 0 {
page = 1
}
pageSize := query.PageSize
if pageSize <= 0 {
pageSize = query.Limit
}
if pageSize <= 0 {
pageSize = 20
}
if pageSize > 100 {
pageSize = 100
}
db := r.baseQuery()
countDB := r.applyAdminListFilters(r.db.Table("rental_listings AS l"), query)
var total int64
if err := countDB.Count(&total).Error; err != nil {
return nil, err
}
db := r.applyAdminListFilters(r.baseQuery(), query)
offset := (page - 1) * pageSize
if offset < 0 {
offset = 0
}
var rows []listingRow
err := db.Order("l.id DESC").Limit(pageSize).Offset(offset).Scan(&rows).Error
if err != nil {
return nil, err
}
return &AdminListResult{
Items: rowsToDTO(rows),
Total: total,
Page: page,
PageSize: pageSize,
}, nil
}
func (r *Repository) applyAdminListFilters(db *gorm.DB, query AdminListQuery) *gorm.DB {
if query.OwnerID > 0 {
db = db.Where("l.owner_id = ?", query.OwnerID)
}
@@ -186,13 +220,7 @@ func (r *Repository) ListAdmin(query AdminListQuery) ([]ListingDTO, error) {
if query.ReviewStatus != "" {
db = db.Where("l.review_status = ?", query.ReviewStatus)
}
var rows []listingRow
err := db.Order("l.id DESC").Limit(limit).Scan(&rows).Error
if err != nil {
return nil, err
}
return rowsToDTO(rows), nil
return db
}
func (r *Repository) FindAdmin(listingID uint64) (*ListingDTO, error) {
+1 -1
View File
@@ -105,7 +105,7 @@ func (s *Service) ListPendingReview() ([]ListingDTO, error) {
return s.repo.ListPendingReview()
}
func (s *Service) ListAdmin(query AdminListQuery) ([]ListingDTO, error) {
func (s *Service) ListAdmin(query AdminListQuery) (*AdminListResult, error) {
if s.repo == nil {
return nil, ErrDependencyUnavailable
}