aboutsummaryrefslogblamecommitdiff
path: root/models/node.go
blob: 98a8d4e50042edcfa2e20ced7769f3691b61c548 (plain) (tree)






















                                                                            
                                                                 





























                                                                              
package models

import (
	"context"
	"time"

	"github.com/uptrace/bun"
)

type Node struct {
	bun.BaseModel `bun:"table:nodes,alias:n"`

	ID         int `bun:"id,pk,autoincrement"`
	Type       NodeType
	Text       string
	AuthorID   int
	Author     *User `bun:"rel:belongs-to,join:author_id=id"`
	ParentID   int
	Parent     *Node `bun:"rel:belongs-to,join:parent_id=id"`
	Permission int
	CreatedAt  int64 `bun:",nullzero,notnull,default:current_timestamp"`
	UpdatedAt  int64 `bun:",nullzero,notnull,default:current_timestamp"`
	DeletedAt  int64
	Children   []*Node `bun:"rel:has-many,join:id=parent_id"`
}

var _ bun.BeforeAppendModelHook = (*Node)(nil)

func (m *Node) BeforeAppendModel(ctx context.Context, query bun.Query) error {
	switch query.(type) {
	case *bun.InsertQuery:
		m.CreatedAt = time.Now().Unix()
		m.UpdatedAt = time.Now().Unix()
	case *bun.UpdateQuery:
		m.UpdatedAt = time.Now().Unix()
	}
	return nil
}

type NodeType int

const (
	TopicType NodeType = iota
	PostType
)

type Permission int

const (
	UserPost Permission = iota << 1
	UserTopic
	AdminPost
	AdminTopic
)