backend/pkg/post/domain/entity/like.go

62 lines
1.7 KiB
Go
Raw Permalink Normal View History

2025-11-19 05:33:06 +00:00
package entity
import (
"errors"
"time"
"github.com/gocql/gocql"
)
// Like represents a like entity for posts or comments.
// Uses composite primary key: (target_id, user_uid) for uniqueness.
type Like struct {
ID gocql.UUID `db:"id" partition_key:"true"` // Like unique identifier
TargetID gocql.UUID `db:"target_id" clustering_key:"true"` // Target ID (post_id or comment_id)
UserUID string `db:"user_uid" clustering_key:"true"` // User UID who liked
TargetType string `db:"target_type"` // Target type: "post" or "comment"
CreatedAt int64 `db:"created_at"` // Creation timestamp
}
// TableName returns the Cassandra table name for Like entities.
func (l *Like) TableName() string {
return "likes"
}
// Validate validates the Like entity
func (l *Like) Validate() error {
var zeroUUID gocql.UUID
if l.TargetID == zeroUUID {
return errors.New("target_id is required")
}
if l.UserUID == "" {
return errors.New("user_uid is required")
}
if l.TargetType != "post" && l.TargetType != "comment" {
return errors.New("target_type must be 'post' or 'comment'")
}
return nil
}
// SetTimestamps sets the create timestamp
func (l *Like) SetTimestamps() {
if l.CreatedAt == 0 {
l.CreatedAt = time.Now().UTC().UnixNano() / 1e6 // milliseconds
}
}
// IsNew returns true if this is a new like (no ID set)
func (l *Like) IsNew() bool {
var zeroUUID gocql.UUID
return l.ID == zeroUUID
}
// IsPostLike returns true if this like is for a post
func (l *Like) IsPostLike() bool {
return l.TargetType == "post"
}
// IsCommentLike returns true if this like is for a comment
func (l *Like) IsCommentLike() bool {
return l.TargetType == "comment"
}