This repository has been archived on 2025-05-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
shop/app/models/Category.go

38 lines
854 B
Go

package models
import (
"gorm.io/gorm"
"time"
)
type Category struct {
ID string `gorm:"size:36;not null;uniqueIndex;primary_key"`
ParentID string `gorm:"size:36;"`
Section Section
SectionID string `gorm:"size:36;index"`
Products []Product `gorm:"many2many:product_categories;"`
Name string `gorm:"size:100;"`
Image string
CreatedAt time.Time
UpdatedAt time.Time
}
func (p *Category) Get(db *gorm.DB, perPage int, page int) (*[]Product, int64, error) {
var count int64
var products []Product
err := db.Debug().Model(&Product{}).Count(&count).Error
if err != nil {
return nil, 0, err
}
offset := (page - 1) * perPage
err = db.Debug().Model(&Product{}).Order("created_at desc").Limit(perPage).Offset(offset).Find(&products).Error
if err != nil {
return nil, 0, err
}
return &products, count, nil
}