56 lines
1.3 KiB
Go
56 lines
1.3 KiB
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) GetCategory(db *gorm.DB, sectionID string) (*[]Category, error) {
|
|
|
|
var categories []Category
|
|
|
|
err := db.Debug().Model(&Category{}).Order("created_at desc").Where("section_id = ?", sectionID).Find(&categories).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &categories, nil
|
|
}
|
|
|
|
func (p *Category) GetCategoryByID(db *gorm.DB, categoryID string) (*Category, error) {
|
|
var result Category
|
|
|
|
err := db.Debug().Model(&Category{}).Where("id = ?", categoryID).Find(&result).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (p *Category) CreateCategory(db *gorm.DB, param *Category) (*Category, error) {
|
|
category := &Category{
|
|
ID: param.ID,
|
|
Name: param.Name,
|
|
SectionID: param.SectionID,
|
|
CreatedAt: time.Time{},
|
|
UpdatedAt: time.Time{},
|
|
}
|
|
|
|
err := db.Debug().Create(&category).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return category, nil
|
|
}
|