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:
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -18,20 +19,9 @@ type AuthToken struct {
|
||||
|
||||
// User returns the user who owns this token.
|
||||
func (t *AuthToken) User(ctx context.Context) (*User, error) {
|
||||
u := &User{}
|
||||
u.SetDB(t.db)
|
||||
|
||||
err := t.GetDB().QueryRowContext(ctx, `
|
||||
SELECT id, nick, password_hash, created_at, updated_at, last_seen_at
|
||||
FROM users WHERE id = ?`,
|
||||
t.UserID,
|
||||
).Scan(
|
||||
&u.ID, &u.Nick, &u.PasswordHash,
|
||||
&u.CreatedAt, &u.UpdatedAt, &u.LastSeenAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if ul := t.GetUserLookup(); ul != nil {
|
||||
return ul.GetUserByID(ctx, t.UserID)
|
||||
}
|
||||
|
||||
return u, nil
|
||||
return nil, fmt.Errorf("user lookup not available")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user