Refactor monster_catches from table to view and add overwrite option
- Convert monster_catches from table to view for automatic calculation - Add overwrite checkbox to monster import UI - Remove manual INSERT/UPDATE logic for catches (now handled by view) - Simplify API endpoints to query view instead of managing state - Add confirmation dialog for overwrite operation Benefits: - No data duplication - Always accurate catch status based on current steps - Simpler codebase with less state management - Easier to reset steps without orphaned catch records 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -55,18 +55,24 @@ function initializeDatabase() {
|
||||
)
|
||||
`);
|
||||
|
||||
// Monster catches table
|
||||
// Monster catches view - derived from actual step data
|
||||
// Drop the old table if it exists and recreate as a view
|
||||
db.run(`DROP TABLE IF EXISTS monster_catches`);
|
||||
db.run(`
|
||||
CREATE TABLE IF NOT EXISTS monster_catches (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
team_id INTEGER NOT NULL,
|
||||
monster_id INTEGER NOT NULL,
|
||||
caught_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
final_steps INTEGER NOT NULL,
|
||||
FOREIGN KEY (team_id) REFERENCES teams(id),
|
||||
FOREIGN KEY (monster_id) REFERENCES daily_monsters(id),
|
||||
UNIQUE(team_id, monster_id)
|
||||
)
|
||||
CREATE VIEW IF NOT EXISTS monster_catches AS
|
||||
SELECT
|
||||
dm.id as monster_id,
|
||||
dm.date,
|
||||
t.id as team_id,
|
||||
t.name as team_name,
|
||||
COALESCE(SUM(ds.steps), 0) as final_steps,
|
||||
dm.step_goal
|
||||
FROM daily_monsters dm
|
||||
CROSS JOIN teams t
|
||||
LEFT JOIN participants p ON t.id = p.team_id
|
||||
LEFT JOIN daily_steps ds ON p.id = ds.participant_id AND ds.date = dm.date
|
||||
GROUP BY dm.id, dm.date, t.id, t.name, dm.step_goal
|
||||
HAVING final_steps >= dm.step_goal
|
||||
`);
|
||||
|
||||
// Create indexes for better query performance
|
||||
@@ -74,8 +80,6 @@ function initializeDatabase() {
|
||||
db.run(`CREATE INDEX IF NOT EXISTS idx_daily_steps_participant ON daily_steps(participant_id)`);
|
||||
db.run(`CREATE INDEX IF NOT EXISTS idx_participants_team ON participants(team_id)`);
|
||||
db.run(`CREATE INDEX IF NOT EXISTS idx_daily_monsters_date ON daily_monsters(date)`);
|
||||
db.run(`CREATE INDEX IF NOT EXISTS idx_monster_catches_team ON monster_catches(team_id)`);
|
||||
db.run(`CREATE INDEX IF NOT EXISTS idx_monster_catches_monster ON monster_catches(monster_id)`);
|
||||
|
||||
console.log('Database initialized successfully');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user