39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type DocumentCategory struct {
|
|
Id int `json:"id" gorm:"primaryKey"`
|
|
Name string `json:"name" gorm:"not null"`
|
|
Slug string `json:"slug" gorm:"uniqueIndex;not null"`
|
|
ParentId *int `json:"parent_id" gorm:"index"`
|
|
SortOrder int `json:"sort_order" gorm:"default:0"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|
}
|
|
|
|
func GetDocumentCategories() ([]*DocumentCategory, error) {
|
|
var categories []*DocumentCategory
|
|
err := DB.Order("sort_order ASC, id ASC").Find(&categories).Error
|
|
return categories, err
|
|
}
|
|
|
|
func GetDocumentCategoryTree() ([]*DocumentCategory, error) {
|
|
var categories []*DocumentCategory
|
|
err := DB.Order("sort_order ASC, id ASC").Find(&categories).Error
|
|
return categories, err
|
|
}
|
|
|
|
func CreateDocumentCategory(category *DocumentCategory) error {
|
|
return DB.Create(category).Error
|
|
}
|
|
|
|
func UpdateDocumentCategory(category *DocumentCategory) error {
|
|
return DB.Model(category).Select("name", "slug", "parent_id", "sort_order").Updates(category).Error
|
|
}
|
|
|
|
func DeleteDocumentCategory(id int) error {
|
|
return DB.Delete(&DocumentCategory{}, id).Error
|
|
}
|