mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-12 18:43:45 +00:00
fix(#64): Address PR comments
This commit is contained in:
@@ -9,12 +9,14 @@ RETURNS TABLE (
|
||||
updated_at TIMESTAMPTZ
|
||||
)
|
||||
SECURITY DEFINER
|
||||
SET search_path = public, pg_temp
|
||||
LANGUAGE plpgsql
|
||||
AS $$
|
||||
DECLARE
|
||||
v_pokemon_caught BIGINT;
|
||||
v_total_users BIGINT;
|
||||
v_completed_pokedexes BIGINT;
|
||||
v_updated_at TIMESTAMPTZ := NOW();
|
||||
BEGIN
|
||||
-- Calculate current stats: count of caught Pokémon
|
||||
SELECT COUNT(*) INTO v_pokemon_caught
|
||||
@@ -29,23 +31,23 @@ BEGIN
|
||||
-- A pokedex is completed when all its entries have caught = true
|
||||
SELECT COUNT(*) INTO v_completed_pokedexes
|
||||
FROM (
|
||||
SELECT pokedexId
|
||||
SELECT "pokedexId"
|
||||
FROM catch_records
|
||||
GROUP BY pokedexId
|
||||
GROUP BY "pokedexId"
|
||||
HAVING COUNT(*) = COUNT(*) FILTER (WHERE caught = true)
|
||||
) completed;
|
||||
|
||||
-- Update cache using UPSERT pattern
|
||||
INSERT INTO stats_cache (pokemon_caught, total_users, completed_pokedexes, updated_at)
|
||||
VALUES (v_pokemon_caught, v_total_users, v_completed_pokedexes, NOW())
|
||||
INSERT INTO stats_cache (id, pokemon_caught, total_users, completed_pokedexes, updated_at)
|
||||
VALUES (1, v_pokemon_caught, v_total_users, v_completed_pokedexes, v_updated_at)
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET pokemon_caught = EXCLUDED.pokemon_caught,
|
||||
total_users = EXCLUDED.total_users,
|
||||
completed_pokedexes = EXCLUDED.completed_pokedexes,
|
||||
updated_at = NOW();
|
||||
updated_at = v_updated_at;
|
||||
|
||||
-- Return the stats
|
||||
RETURN QUERY
|
||||
SELECT v_pokemon_caught, v_total_users, v_completed_pokedexes, NOW() AS updated_at;
|
||||
SELECT v_pokemon_caught, v_total_users, v_completed_pokedexes, v_updated_at AS updated_at;
|
||||
END;
|
||||
$$;
|
||||
|
||||
@@ -18,6 +18,7 @@ DECLARE
|
||||
v_pokemon_caught BIGINT;
|
||||
v_total_users BIGINT;
|
||||
v_completed_pokedexes BIGINT;
|
||||
v_updated_at TIMESTAMPTZ := NOW();
|
||||
BEGIN
|
||||
-- Calculate current stats: count of caught Pokémon
|
||||
SELECT COUNT(*) INTO v_pokemon_caught
|
||||
@@ -39,17 +40,17 @@ BEGIN
|
||||
) completed;
|
||||
|
||||
-- Update cache using UPSERT pattern
|
||||
INSERT INTO stats_cache (pokemon_caught, total_users, completed_pokedexes, updated_at)
|
||||
VALUES (v_pokemon_caught, v_total_users, v_completed_pokedexes, NOW())
|
||||
INSERT INTO stats_cache (id, pokemon_caught, total_users, completed_pokedexes, updated_at)
|
||||
VALUES (1, v_pokemon_caught, v_total_users, v_completed_pokedexes, v_updated_at)
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET pokemon_caught = EXCLUDED.pokemon_caught,
|
||||
total_users = EXCLUDED.total_users,
|
||||
completed_pokedexes = EXCLUDED.completed_pokedexes,
|
||||
updated_at = NOW();
|
||||
updated_at = v_updated_at;
|
||||
|
||||
-- Return stats
|
||||
RETURN QUERY
|
||||
SELECT v_pokemon_caught, v_total_users, v_completed_pokedexes, NOW() AS updated_at;
|
||||
SELECT v_pokemon_caught, v_total_users, v_completed_pokedexes, v_updated_at AS updated_at;
|
||||
END;
|
||||
$$;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user