2025-11-17 12:17:44 +07:00

48 lines
1.4 KiB
Go

package models
import (
"time"
"gorm.io/gorm"
)
// Category represents an item category
type Category struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"type:varchar(100);not null" json:"name"`
Slug string `gorm:"type:varchar(100);uniqueIndex;not null" json:"slug"`
Description string `gorm:"type:text" json:"description"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// Relationships
Items []Item `gorm:"foreignKey:CategoryID" json:"items,omitempty"`
LostItems []LostItem `gorm:"foreignKey:CategoryID" json:"lost_items,omitempty"`
}
// TableName specifies the table name for Category model
func (Category) TableName() string {
return "categories"
}
// CategoryResponse represents category data for API responses
type CategoryResponse struct {
ID uint `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
ItemCount int64 `json:"item_count,omitempty"`
CreatedAt time.Time `json:"created_at"`
}
// ToResponse converts Category to CategoryResponse
func (c *Category) ToResponse() CategoryResponse {
return CategoryResponse{
ID: c.ID,
Name: c.Name,
Slug: c.Slug,
Description: c.Description,
CreatedAt: c.CreatedAt,
}
}