- Resolve duplicate method declarations (CreateUser, GetUserByToken,
GetUserByNick) between db.go and queries.go by renaming queries.go
methods to CreateSimpleUser, LookupUserByToken, LookupUserByNick
- Fix 377 lint issues across all categories:
- nlreturn (107): Add blank lines before returns
- wsl_v5 (156): Add required whitespace
- noinlineerr (25): Use plain assignments instead of inline error handling
- errcheck (15): Check all error return values
- mnd (10): Extract magic numbers to named constants
- err113 (7): Use wrapped static errors instead of dynamic errors
- gosec (7): Fix SSRF, SQL injection warnings; add nolint for false positives
- modernize (7): Replace interface{} with any
- cyclop (2): Reduce cyclomatic complexity via command map dispatch
- gocognit (1): Break down complex handler into sub-handlers
- funlen (3): Extract long functions into smaller helpers
- funcorder (4): Reorder methods (exported before unexported)
- forcetypeassert (2): Add safe type assertions with ok checks
- ireturn (2): Replace interface-returning methods with concrete lookups
- noctx (3): Use NewRequestWithContext and ExecContext
- tagliatelle (5): Fix JSON tag casing to camelCase
- revive (4): Rename package from 'api' to 'chatapi'
- rowserrcheck (8): Add rows.Err() checks after iteration
- lll (2): Shorten long lines
- perfsprint (5): Use strconv and string concatenation
- nestif (2): Extract nested conditionals into helper methods
- wastedassign (1): Remove wasted assignments
- gosmopolitan (1): Add nolint for intentional Local() time display
- usestdlibvars (1): Use http.MethodGet
- godoclint (2): Remove duplicate package comments
- Fix broken migration 003_users.sql that conflicted with 002_schema.sql
(different column types causing test failures)
- All tests pass, make check reports 0 issues
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
// Package models defines the data models used by the chat application.
|
|
// All model structs embed Base, which provides database access for
|
|
// relation-fetching methods directly on model instances.
|
|
package models
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
// DB is the interface that models use to query the database.
|
|
// This avoids a circular import with the db package.
|
|
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
|
|
}
|
|
|
|
// SetDB injects the database reference into a model.
|
|
func (b *Base) SetDB(d DB) {
|
|
b.db = d
|
|
}
|
|
|
|
// GetDB returns the database interface for use in model methods.
|
|
func (b *Base) GetDB() *sql.DB {
|
|
return b.db.GetDB()
|
|
}
|
|
|
|
// LookupUser looks up a user by ID if the database supports it.
|
|
func (b *Base) LookupUser(ctx context.Context, id string) (*User, error) {
|
|
ul, ok := b.db.(UserLookup)
|
|
if !ok {
|
|
return nil, ErrUserLookupNotAvailable
|
|
}
|
|
|
|
return ul.GetUserByID(ctx, id)
|
|
}
|
|
|
|
// LookupChannel looks up a channel by ID if the database supports it.
|
|
func (b *Base) LookupChannel(ctx context.Context, id string) (*Channel, error) {
|
|
cl, ok := b.db.(ChannelLookup)
|
|
if !ok {
|
|
return nil, ErrChannelLookupNotAvailable
|
|
}
|
|
|
|
return cl.GetChannelByID(ctx, id)
|
|
}
|