aboutsummaryrefslogtreecommitdiff
path: root/models/node.go
blob: 16e426ae323ec2c0ea0da79a066daf2789348d1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
}

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
)