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