aboutsummaryrefslogtreecommitdiff
path: root/models
diff options
context:
space:
mode:
Diffstat (limited to 'models')
-rw-r--r--models/errors.go4
-rw-r--r--models/node.go59
-rw-r--r--models/user.go15
3 files changed, 32 insertions, 46 deletions
diff --git a/models/errors.go b/models/errors.go
index 660fe85..9b514e9 100644
--- a/models/errors.go
+++ b/models/errors.go
@@ -6,4 +6,8 @@ var (
ErrInvalidUserOrPassword = errors.New("invalid user or password")
ErrUserAlreadyExists = errors.New("user already exists")
ErrInvalidPassword = errors.New("invalid password")
+ ErrTopicNotFound = errors.New("topic not found")
+ ErrPostNotFound = errors.New("post not found")
+ ErrUserNotFound = errors.New("user not found")
+ ErrDBNotInitialized = errors.New("db is not initialized")
)
diff --git a/models/node.go b/models/node.go
index 98a8d4e..7d3f719 100644
--- a/models/node.go
+++ b/models/node.go
@@ -1,49 +1,36 @@
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
- Children []*Node `bun:"rel:has-many,join:id=parent_id"`
+type Topic struct {
+ ID uint64 `json:"id"`
+ Topic string `json:"topic"`
+ Text string `json:"text"`
+ AuthorID string `json:"author_id"`
+ Author *User `json:"-"`
+ ParentID uint64 `json:"parent_id"`
+ Parent *Topic `json:"-"`
+ Permission int `json:"permission"`
+ CreatedAt time.Time `json:"created_at"`
+ UpdatedAt time.Time `json:"updated_at"`
+ DeletedAt *time.Time `json:"deleted_at"`
}
-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 Post struct {
+ ID uint64 `json:"id"`
+ Text string `json:"text"`
+ AuthorID string `json:"author_id"`
+ Author *User `json:"-"`
+ ParentID uint64 `json:"parent_id"`
+ Parent *Post `json:"-"`
+ CreatedAt time.Time `json:"created_at"`
+ UpdatedAt time.Time `json:"updated_at"`
+ DeletedAt *time.Time `json:"deleted_at"`
+ Children []*Post `json:"-"`
}
-type NodeType int
-
-const (
- TopicType NodeType = iota
- PostType
-)
-
type Permission int
const (
diff --git a/models/user.go b/models/user.go
index 73358fe..e8ae3cc 100644
--- a/models/user.go
+++ b/models/user.go
@@ -2,8 +2,6 @@ package models
import (
"encoding/gob"
-
- "github.com/uptrace/bun"
)
func init() {
@@ -11,14 +9,11 @@ func init() {
}
type User struct {
- bun.BaseModel `bun:"table:users,alias:u"`
-
- ID int `bun:"id,pk,autoincrement"`
- Email string
- Password string
- Username string
- Photo *string
- Role UserRole
+ Email string `json:"email,omitempty"`
+ Password string `json:"password,omitempty"`
+ Username string `json:"username,omitempty"`
+ Photo *string `json:"photo,omitempty"`
+ Role UserRole `json:"role,omitempty"`
}
type UserRole int