diff options
author | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-29 02:47:35 +0300 |
---|---|---|
committer | Alexander NeonXP Kiryukhin <i@neonxp.ru> | 2024-07-29 02:47:35 +0300 |
commit | 96e2ce2e9d363a6296f9411ecb00168520258874 (patch) | |
tree | 09aa7fffe10eab84ae0edd39e570355984ba0148 /models | |
parent | 12ed72e4e1da181a6c87319a50d3b4142788b4c0 (diff) |
Отказ от echo
Diffstat (limited to 'models')
-rw-r--r-- | models/errors.go | 4 | ||||
-rw-r--r-- | models/node.go | 59 | ||||
-rw-r--r-- | models/user.go | 15 |
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 |