aboutsummaryrefslogtreecommitdiff
path: root/models/node.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/node.go')
-rw-r--r--models/node.go53
1 files changed, 53 insertions, 0 deletions
diff --git a/models/node.go b/models/node.go
new file mode 100644
index 0000000..16e426a
--- /dev/null
+++ b/models/node.go
@@ -0,0 +1,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
+)