From 60a7570ca29c0e7b1d527dd99b2df313115e216b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Un=20=C3=89picier?= Date: Wed, 16 Oct 2024 09:33:10 +0200 Subject: [PATCH] :sparkles: Added stats & SQL order by on get points --- app/api/point/route.ts | 2 +- app/api/stats/route.ts | 99 ++++++++++++++++++++++++++++++++ app/my-loves/hooks/useActions.ts | 1 + app/my-loves/page.tsx | 4 +- app/settings/hooks/useActions.ts | 1 + app/statistics/hooks/useData.ts | 27 +++++++++ app/statistics/page.tsx | 10 ++-- 7 files changed, 137 insertions(+), 7 deletions(-) create mode 100644 app/api/stats/route.ts diff --git a/app/api/point/route.ts b/app/api/point/route.ts index c550d5d..8837f2c 100644 --- a/app/api/point/route.ts +++ b/app/api/point/route.ts @@ -37,7 +37,7 @@ export const GET = async (req: NextRequest) => { } const { rows } = await pool.query( - `SELECT * FROM "point" WHERE "userid"=$1;`, + `SELECT * FROM "point" WHERE "userid"=$1 ORDER BY createdat DESC;`, [userID] ); diff --git a/app/api/stats/route.ts b/app/api/stats/route.ts new file mode 100644 index 0000000..34121c1 --- /dev/null +++ b/app/api/stats/route.ts @@ -0,0 +1,99 @@ +import { NextRequest, NextResponse } from 'next/server'; +import { getDBConnexion } from '../utils'; + +export const GET = async (req: NextRequest) => { + const userID = req.headers.get('Authorization'); + + if (!userID) { + return NextResponse.json( + { + error: true, + message: 'Unauthorized request', + }, + { + status: 403, + } + ); + } + + try { + const pool = getDBConnexion(); + + const { rows: userCount } = await pool.query( + `SELECT * FROM "user" WHERE "id"=$1 LIMIT 1;`, + [userID] + ); + + if (userCount.length != 1) { + return NextResponse.json( + { + error: true, + message: 'User not found', + }, + { + status: 404, + } + ); + } + + const { rows } = await pool.query( + ` +SELECT ( + SELECT AVG(entries) + FROM ( + SELECT COUNT(*) AS entries + FROM point + WHERE userid=$1 + GROUP BY date_part('year', createdat) + ) +) AS avgPerYear, +( + SELECT AVG(entries) + FROM ( + SELECT COUNT(*) AS entries + FROM point + WHERE userid=$1 + GROUP BY date_part('year', createdat), date_part('month', createdat) + ) +) AS avgPerMonth, +( + SELECT AVG(entries) + FROM ( + SELECT COUNT(*) AS entries + FROM point + WHERE userid=$1 + GROUP BY date_part('year', createdat), date_part('month', createdat), date_part('week', createdat) + ) +) AS avgPerWeek, +( + SELECT COUNT(*) + FROM point + WHERE userid=$1 +) AS total;`, + [userID] + ); + + const result = { + year: Math.round(parseFloat(rows[0].avgperyear)), + month: Math.round(parseFloat(rows[0].avgpermonth)), + week: Math.round(parseFloat(rows[0].avgperweek)), + total: parseInt(rows[0].total), + }; + + await pool.end(); + return NextResponse.json(result, { + status: 200, + }); + } catch (error) { + console.error(error); + return NextResponse.json( + { + error: true, + message: 'Internal error', + }, + { + status: 500, + } + ); + } +}; diff --git a/app/my-loves/hooks/useActions.ts b/app/my-loves/hooks/useActions.ts index 7c0b1fb..ccdaf5e 100644 --- a/app/my-loves/hooks/useActions.ts +++ b/app/my-loves/hooks/useActions.ts @@ -38,6 +38,7 @@ export const useActions = ({ if (response.ok) { await refreshDataset(); setLoveToDelete(null); + setIsLoading(false); return; } } catch (error) { diff --git a/app/my-loves/page.tsx b/app/my-loves/page.tsx index d5dc3af..e308371 100644 --- a/app/my-loves/page.tsx +++ b/app/my-loves/page.tsx @@ -58,7 +58,9 @@ const MyLoves = () => {

{love.location}

{love.comment || '-'}

-

Le: {love.createdat}

+

+ Créé le: {love.createdat} +

diff --git a/app/settings/hooks/useActions.ts b/app/settings/hooks/useActions.ts index f9e4d3b..bb2f7e2 100644 --- a/app/settings/hooks/useActions.ts +++ b/app/settings/hooks/useActions.ts @@ -21,6 +21,7 @@ export const useActions = ({ }; const updateUsername = async (ev: FormEvent) => { + ev.preventDefault(); resetMessage(); if (!ev.currentTarget.reportValidity()) { diff --git a/app/statistics/hooks/useData.ts b/app/statistics/hooks/useData.ts index 3d5370b..3daa804 100644 --- a/app/statistics/hooks/useData.ts +++ b/app/statistics/hooks/useData.ts @@ -8,6 +8,12 @@ import { useEffect, useState } from 'react'; export const useData = () => { const [userData, setUserData] = useState(null); + const [stats, setStats] = useState({ + year: '00', + month: '00', + week: '00', + total: '00', + }); useEffect(() => { (async () => { @@ -31,9 +37,30 @@ export const useData = () => { setUserData(formattedResponse); })(); + + (async () => { + const token = localStorage.getItem('token'); + + const request = await fetch('/api/stats', { + method: 'GET', + headers: { + Authorization: token, + }, + }); + + const response = await request.json(); + + setStats({ + year: `${response.year}`.padStart(2, '0'), + month: `${response.month}`.padStart(2, '0'), + week: `${response.week}`.padStart(2, '0'), + total: `${response.total}`.padStart(2, '0'), + }); + })(); }, []); return { userData, + stats, }; }; diff --git a/app/statistics/page.tsx b/app/statistics/page.tsx index d3b4625..b13d23a 100644 --- a/app/statistics/page.tsx +++ b/app/statistics/page.tsx @@ -22,7 +22,7 @@ import './styles.scss'; const Statistics = () => { const router = useRouter(); - const { userData } = useData(); + const { userData, stats } = useData(); const logout = () => { localStorage.clear(); @@ -63,7 +63,7 @@ const Statistics = () => {
{
{
{