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
54
55
56
57
58
|
package repository
import (
"context"
"github.com/uptrace/bun"
"gitrepo.ru/neonxp/gorum/models"
)
type Node struct {
db *bun.DB
}
func NewNode(db *bun.DB) *Node {
return &Node{
db: db,
}
}
func (t *Node) Create(
ctx context.Context,
ntype models.NodeType,
text string,
authorID int,
parentID int,
) (int, error) {
post := &models.Node{
Type: ntype,
Text: text,
AuthorID: authorID,
ParentID: parentID,
}
_, err := t.db.NewInsert().Model(post).Returning("id").Exec(ctx)
return post.ID, err
}
func (t *Node) Get(ctx context.Context, topicID int) (*models.Node, error) {
node := new(models.Node)
return node, t.db.NewSelect().
Model(node).
Where(`n.id = ?`, topicID).
Relation("Author").
Scan(ctx)
}
func (t *Node) List(ctx context.Context, topicID int) ([]*models.Node, int, error) {
posts := make([]*models.Node, 0)
count, err := t.db.NewSelect().
Model(&posts).
Where(`parent_id = ?`, topicID).
Relation("Author").
ScanAndCount(ctx)
return posts, count, err
}
|