aboutsummaryrefslogtreecommitdiff
path: root/repository/node.go
diff options
context:
space:
mode:
authorAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-20 21:56:37 +0300
committerAlexander NeonXP Kiryukhin <i@neonxp.ru>2024-07-20 21:58:27 +0300
commite9a64f3b41b5eae47dec7c0ecfd1caae83136abc (patch)
treec07898520a7015c3f2a201ca0674efea82dd76a5 /repository/node.go
parent9c0a6b21f8f8d9b725e1fe7f11da4532e04fd9f1 (diff)
initialv0.0.1
Diffstat (limited to 'repository/node.go')
-rw-r--r--repository/node.go58
1 files changed, 58 insertions, 0 deletions
diff --git a/repository/node.go b/repository/node.go
new file mode 100644
index 0000000..d5794b6
--- /dev/null
+++ b/repository/node.go
@@ -0,0 +1,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
+}