Files
Erupe/server/api/repo_interfaces.go
Houmgaor 82b967b715 refactor: replace raw SQL with repository interfaces in entranceserver and API server
Extract all direct database calls from entranceserver (2 calls) and
API server (17 calls) into typed repository interfaces with PostgreSQL
implementations, matching the pattern established in signserver and
channelserver.

Entranceserver: EntranceServerRepo, EntranceSessionRepo
API server: APIUserRepo, APICharacterRepo, APISessionRepo

Also fix the 3 remaining fmt.Sprintf calls inside logger invocations
in handlers_commands.go and handlers_stage.go, replacing them with
structured zap fields.

Unskip 5 TestNewAuthData* tests that previously required a real
database — they now run with mock repos.
2026-02-22 17:04:58 +01:00

54 lines
2.5 KiB
Go

package api
import (
"context"
"time"
)
// Repository interfaces decouple API server business logic from concrete
// PostgreSQL implementations, enabling mock/stub injection for unit tests.
// APIUserRepo defines the contract for user-related data access.
type APIUserRepo interface {
// Register creates a new user and returns their ID and rights.
Register(ctx context.Context, username, passwordHash string, returnExpires time.Time) (id uint32, rights uint32, err error)
// GetCredentials returns the user's ID, password hash, and rights.
GetCredentials(ctx context.Context, username string) (id uint32, passwordHash string, rights uint32, err error)
// GetLastLogin returns the user's last login time.
GetLastLogin(uid uint32) (time.Time, error)
// GetReturnExpiry returns the user's return expiry time.
GetReturnExpiry(uid uint32) (time.Time, error)
// UpdateReturnExpiry sets the user's return expiry time.
UpdateReturnExpiry(uid uint32, expiry time.Time) error
// UpdateLastLogin sets the user's last login time.
UpdateLastLogin(uid uint32, loginTime time.Time) error
}
// APICharacterRepo defines the contract for character-related data access.
type APICharacterRepo interface {
// GetNewCharacter returns an existing new (unfinished) character for a user.
GetNewCharacter(ctx context.Context, userID uint32) (Character, error)
// CountForUser returns the total number of characters for a user.
CountForUser(ctx context.Context, userID uint32) (int, error)
// Create inserts a new character and returns it.
Create(ctx context.Context, userID uint32, lastLogin uint32) (Character, error)
// IsNew returns whether a character is a new (unfinished) character.
IsNew(charID uint32) (bool, error)
// HardDelete permanently removes a character.
HardDelete(charID uint32) error
// SoftDelete marks a character as deleted.
SoftDelete(charID uint32) error
// GetForUser returns all finalized (non-deleted) characters for a user.
GetForUser(ctx context.Context, userID uint32) ([]Character, error)
// ExportSave returns the full character row as a map.
ExportSave(ctx context.Context, userID, charID uint32) (map[string]interface{}, error)
}
// APISessionRepo defines the contract for session/token data access.
type APISessionRepo interface {
// CreateToken inserts a new sign session and returns its ID and token.
CreateToken(ctx context.Context, uid uint32, token string) (tokenID uint32, err error)
// GetUserIDByToken returns the user ID for a given session token.
GetUserIDByToken(ctx context.Context, token string) (uint32, error)
}