mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-22 15:43:49 +01:00
Move scan loops from handlers into repository methods so that interfaces return typed slices instead of leaking database cursors. This fixes resource leaks (7 of 12 call sites never closed rows) and makes all 12 methods mockable for unit tests. Affected repos: CafeRepo, ShopRepo, EventRepo, RengokuRepo, DivaRepo, ScenarioRepo, MiscRepo, MercenaryRepo. New structs: DivaEvent, MercenaryLoan, GuildHuntCatUsage. EventRepo.GetEventQuests left as-is (requires broader Server refactor).
43 lines
1.3 KiB
Go
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 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
|
|
}
|