Files
Erupe/server/channelserver/repo_misc.go
Houmgaor 35d8471d59 fix(channelserver): resolve all golangci-lint issues and add handler tests
Fix errcheck violations across 11 repo files by wrapping deferred
rows.Close() and tx.Rollback() calls to discard the error return.
Fix unchecked Scan/Exec calls in guild store tests. Fix staticcheck
SA9003 empty branch in test helpers.

Add 6 mock-based unit tests for GetCharacterSaveData covering nil
savedata, sql.ErrNoRows, DB errors, compressed round-trip,
new-character skip, and config mode/pointer propagation.
2026-02-21 14:47:25 +01:00

43 lines
1.3 KiB
Go

package channelserver
import (
"fmt"
"github.com/jmoiron/sqlx"
)
// MiscRepository centralizes database access for miscellaneous game tables.
type MiscRepository struct {
db *sqlx.DB
}
// NewMiscRepository creates a new MiscRepository.
func NewMiscRepository(db *sqlx.DB) *MiscRepository {
return &MiscRepository{db: db}
}
// GetTrendWeapons returns the top 3 weapon IDs for a given weapon type, ordered by count descending.
func (r *MiscRepository) GetTrendWeapons(weaponType uint8) ([]uint16, error) {
rows, err := r.db.Query("SELECT weapon_id FROM trend_weapons WHERE weapon_type=$1 ORDER BY count DESC LIMIT 3", weaponType)
if err != nil {
return nil, fmt.Errorf("query trend_weapons: %w", err)
}
defer func() { _ = rows.Close() }()
var result []uint16
for rows.Next() {
var id uint16
if err := rows.Scan(&id); err != nil {
return nil, fmt.Errorf("scan trend_weapons: %w", err)
}
result = append(result, id)
}
return result, rows.Err()
}
// UpsertTrendWeapon increments the count for a weapon, inserting it if it doesn't exist.
func (r *MiscRepository) UpsertTrendWeapon(weaponID uint16, weaponType uint8) error {
_, err := r.db.Exec(`INSERT INTO trend_weapons (weapon_id, weapon_type, count) VALUES ($1, $2, 1) ON CONFLICT (weapon_id) DO
UPDATE SET count = trend_weapons.count+1`, weaponID, weaponType)
return err
}