Add models package with embedded DB interface pattern
- internal/models/model.go: DB interface + Base struct for all models - internal/models/channel.go: Channel model with DB access for relation queries - Database.NewChannel() factory injects db reference into model instances - Uses interface to avoid circular imports (models -> db)
This commit is contained in:
21
internal/models/channel.go
Normal file
21
internal/models/channel.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Channel represents a chat channel.
|
||||
type Channel struct {
|
||||
Base
|
||||
ID int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Topic string `json:"topic"`
|
||||
Modes string `json:"modes"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Example relation method — will be fleshed out when we add channel_members:
|
||||
// func (c *Channel) Members(ctx context.Context) ([]*User, error) {
|
||||
// return c.DB().GetChannelMembers(ctx, c.ID)
|
||||
// }
|
||||
18
internal/models/model.go
Normal file
18
internal/models/model.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// DB is the interface that models use to query relations.
|
||||
// This avoids a circular import with the db package.
|
||||
type DB interface {
|
||||
GetDB() *sql.DB
|
||||
}
|
||||
|
||||
// Base is embedded in all model structs to provide database access.
|
||||
type Base struct {
|
||||
db DB
|
||||
}
|
||||
|
||||
func (b *Base) SetDB(d DB) {
|
||||
b.db = d
|
||||
}
|
||||
Reference in New Issue
Block a user