package entity import ( "errors" "time" "github.com/gocql/gocql" ) // Category represents a category entity for organizing posts. type Category struct { ID gocql.UUID `db:"id" partition_key:"true"` // Category unique identifier Slug string `db:"slug"` // URL-friendly slug (unique) Name string `db:"name"` // Category name Description *string `db:"description,omitempty"` // Category description (optional) ParentID *gocql.UUID `db:"parent_id,omitempty"` // Parent category ID (for nested categories) PostCount int64 `db:"post_count"` // Number of posts in this category IsActive bool `db:"is_active"` // Whether the category is active SortOrder int32 `db:"sort_order"` // Sort order for display CreatedAt int64 `db:"created_at"` // Creation timestamp UpdatedAt int64 `db:"updated_at"` // Last update timestamp } // TableName returns the Cassandra table name for Category entities. func (c *Category) TableName() string { return "categories" } // Validate validates the Category entity func (c *Category) Validate() error { if c.Name == "" { return errors.New("category name is required") } if c.Slug == "" { return errors.New("category slug is required") } return nil } // SetTimestamps sets the create and update timestamps func (c *Category) SetTimestamps() { now := time.Now().UTC().UnixNano() / 1e6 // milliseconds if c.CreatedAt == 0 { c.CreatedAt = now } c.UpdatedAt = now } // IsNew returns true if this is a new category (no ID set) func (c *Category) IsNew() bool { var zeroUUID gocql.UUID return c.ID == zeroUUID } // IsRoot returns true if this category has no parent func (c *Category) IsRoot() bool { var zeroUUID gocql.UUID return c.ParentID == nil || *c.ParentID == zeroUUID } // IncrementPostCount increments the post count func (c *Category) IncrementPostCount() { c.PostCount++ c.SetTimestamps() } // DecrementPostCount decrements the post count func (c *Category) DecrementPostCount() { if c.PostCount > 0 { c.PostCount-- c.SetTimestamps() } } // Activate activates the category func (c *Category) Activate() { c.IsActive = true c.SetTimestamps() } // Deactivate deactivates the category func (c *Category) Deactivate() { c.IsActive = false c.SetTimestamps() }