aboutsummaryrefslogtreecommitdiff
path: root/models
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 /models
parent9c0a6b21f8f8d9b725e1fe7f11da4532e04fd9f1 (diff)
initialv0.0.1
Diffstat (limited to 'models')
-rw-r--r--models/errors.go9
-rw-r--r--models/node.go53
-rw-r--r--models/user.go21
3 files changed, 83 insertions, 0 deletions
diff --git a/models/errors.go b/models/errors.go
new file mode 100644
index 0000000..660fe85
--- /dev/null
+++ b/models/errors.go
@@ -0,0 +1,9 @@
+package models
+
+import "errors"
+
+var (
+ ErrInvalidUserOrPassword = errors.New("invalid user or password")
+ ErrUserAlreadyExists = errors.New("user already exists")
+ ErrInvalidPassword = errors.New("invalid password")
+)
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
+)
diff --git a/models/user.go b/models/user.go
new file mode 100644
index 0000000..73afdce
--- /dev/null
+++ b/models/user.go
@@ -0,0 +1,21 @@
+package models
+
+import (
+ "encoding/gob"
+
+ "github.com/uptrace/bun"
+)
+
+func init() {
+ gob.Register(User{})
+}
+
+type User struct {
+ bun.BaseModel `bun:"table:users,alias:u"`
+
+ ID int `bun:"id,pk,autoincrement"`
+ Email string
+ Password string
+ Username string
+ Photo *string
+}