Toplist

GoodSync is the new home
for Allway Sync

Claim your 12 months of GoodSync
and 10GB of GoodSync Storage for FREE
Start Free Upgrade (for Allway Sync license holders)
New to Allway Sync? Click Here

Toplist

export default Toplist; Database Schema (PostgreSQL example) CREATE TABLE items ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, category VARCHAR(100), thumbnail TEXT, created_at TIMESTAMP DEFAULT NOW() ); CREATE TABLE interactions ( id SERIAL PRIMARY KEY, item_id INT REFERENCES items(id) ON DELETE CASCADE, type VARCHAR(50) CHECK (type IN ('view', 'like', 'vote', 'purchase')), created_at TIMESTAMP DEFAULT NOW() ); API Endpoint app.get('/api/toplist', async (req, res) => const limit = 10, timeframe = 'week', metric = 'views' = req.query; let interval; switch(timeframe) case 'day': interval = '1 day'; break; case 'week': interval = '7 days'; break; case 'month': interval = '30 days'; break; default: interval = null;

const query = SELECT items.id, items.name, items.category, items.thumbnail, COUNT(interactions.id) as score, '$metric' as metric_label FROM items LEFT JOIN interactions ON items.id = interactions.item_id AND interactions.type = $1 $timeCondition GROUP BY items.id ORDER BY score DESC LIMIT $2 ; toplist

try const result = await db.query(query, [metric, parseInt(limit)]); res.json(result.rows); catch (err) res.status(500).json( error: err.message ); AND i.created_at &gt

A toplist displays ranked items based on a specific metric (e.g., most popular, highest rated, most viewed). 1. Frontend Component (React + Tailwind) import useState, useEffect from 'react'; const Toplist = ( endpoint, limit = 10, title ) => const [items, setItems] = useState([]); const [loading, setLoading] = useState(true); const [timeframe, setTimeframe] = useState('week'); NOW() - INTERVAL '$interval' : ''

const timeCondition = interval ? AND i.created_at > NOW() - INTERVAL '$interval' : '';

useEffect(() => fetch( $endpoint?limit=$limit&timeframe=$timeframe ) .then(res => res.json()) .then(data => setItems(data); setLoading(false); ); , [timeframe, endpoint, limit]);