Fix code review feedback items 1-6, 8-10
- Item 1: Extract GetUserByID/GetChannelByID lookup methods, use from relation methods - Item 2: Initialize slices with literals so JSON gets [] not null - Item 3: Populate CreatedAt/UpdatedAt with time.Now() on all Create methods - Item 4: Wrap each migration's SQL + recording in a transaction - Item 5: Check error from res.LastInsertId() in QueueMessage - Item 6: Add DequeueMessages and AckMessages methods - Item 8: Add GetUserByNick, GetUserByToken, DeleteAuthToken, UpdateUserLastSeen - Item 9: Run PRAGMA foreign_keys = ON on every new connection - Item 10: Builds clean, all tests pass
This commit is contained in:
@@ -3,7 +3,10 @@
|
||||
// relation-fetching methods directly on model instances.
|
||||
package models
|
||||
|
||||
import "database/sql"
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// DB is the interface that models use to query the database.
|
||||
// This avoids a circular import with the db package.
|
||||
@@ -11,6 +14,16 @@ type DB interface {
|
||||
GetDB() *sql.DB
|
||||
}
|
||||
|
||||
// UserLookup provides user lookup by ID without circular imports.
|
||||
type UserLookup interface {
|
||||
GetUserByID(ctx context.Context, id string) (*User, error)
|
||||
}
|
||||
|
||||
// ChannelLookup provides channel lookup by ID without circular imports.
|
||||
type ChannelLookup interface {
|
||||
GetChannelByID(ctx context.Context, id string) (*Channel, error)
|
||||
}
|
||||
|
||||
// Base is embedded in all model structs to provide database access.
|
||||
type Base struct {
|
||||
db DB
|
||||
@@ -25,3 +38,21 @@ func (b *Base) SetDB(d DB) {
|
||||
func (b *Base) GetDB() *sql.DB {
|
||||
return b.db.GetDB()
|
||||
}
|
||||
|
||||
// GetUserLookup returns the DB as a UserLookup if it implements the interface.
|
||||
func (b *Base) GetUserLookup() UserLookup {
|
||||
if ul, ok := b.db.(UserLookup); ok {
|
||||
return ul
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetChannelLookup returns the DB as a ChannelLookup if it implements the interface.
|
||||
func (b *Base) GetChannelLookup() ChannelLookup {
|
||||
if cl, ok := b.db.(ChannelLookup); ok {
|
||||
return cl
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user