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
}