✨ Added stats & SQL order by on get points
This commit is contained in:
@@ -37,7 +37,7 @@ export const GET = async (req: NextRequest) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const { rows } = await pool.query(
|
const { rows } = await pool.query(
|
||||||
`SELECT * FROM "point" WHERE "userid"=$1;`,
|
`SELECT * FROM "point" WHERE "userid"=$1 ORDER BY createdat DESC;`,
|
||||||
[userID]
|
[userID]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -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,
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -38,6 +38,7 @@ export const useActions = ({
|
|||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
await refreshDataset();
|
await refreshDataset();
|
||||||
setLoveToDelete(null);
|
setLoveToDelete(null);
|
||||||
|
setIsLoading(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -58,7 +58,9 @@ const MyLoves = () => {
|
|||||||
<div className='infos'>
|
<div className='infos'>
|
||||||
<p className='location'>{love.location}</p>
|
<p className='location'>{love.location}</p>
|
||||||
<p className='comment'>{love.comment || '-'}</p>
|
<p className='comment'>{love.comment || '-'}</p>
|
||||||
<p className='date'>Le: {love.createdat}</p>
|
<p className='date'>
|
||||||
|
Créé le: {love.createdat}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='actionButtons'>
|
<div className='actionButtons'>
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export const useActions = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const updateUsername = async (ev: FormEvent<HTMLFormElement>) => {
|
const updateUsername = async (ev: FormEvent<HTMLFormElement>) => {
|
||||||
|
ev.preventDefault();
|
||||||
resetMessage();
|
resetMessage();
|
||||||
|
|
||||||
if (!ev.currentTarget.reportValidity()) {
|
if (!ev.currentTarget.reportValidity()) {
|
||||||
|
|||||||
@@ -8,6 +8,12 @@ import { useEffect, useState } from 'react';
|
|||||||
|
|
||||||
export const useData = () => {
|
export const useData = () => {
|
||||||
const [userData, setUserData] = useState(null);
|
const [userData, setUserData] = useState(null);
|
||||||
|
const [stats, setStats] = useState({
|
||||||
|
year: '00',
|
||||||
|
month: '00',
|
||||||
|
week: '00',
|
||||||
|
total: '00',
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
@@ -31,9 +37,30 @@ export const useData = () => {
|
|||||||
|
|
||||||
setUserData(formattedResponse);
|
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 {
|
return {
|
||||||
userData,
|
userData,
|
||||||
|
stats,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,7 +22,7 @@ import './styles.scss';
|
|||||||
|
|
||||||
const Statistics = () => {
|
const Statistics = () => {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { userData } = useData();
|
const { userData, stats } = useData();
|
||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
localStorage.clear();
|
localStorage.clear();
|
||||||
@@ -63,7 +63,7 @@ const Statistics = () => {
|
|||||||
<div className='statistics'>
|
<div className='statistics'>
|
||||||
<div className='statistic'>
|
<div className='statistic'>
|
||||||
<SlotCounter
|
<SlotCounter
|
||||||
value={'00'}
|
value={stats.week}
|
||||||
startValue={'00'}
|
startValue={'00'}
|
||||||
startValueOnce
|
startValueOnce
|
||||||
dummyCharacterCount={10}
|
dummyCharacterCount={10}
|
||||||
@@ -74,7 +74,7 @@ const Statistics = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className='statistic'>
|
<div className='statistic'>
|
||||||
<SlotCounter
|
<SlotCounter
|
||||||
value={'00'}
|
value={stats.month}
|
||||||
startValue={'00'}
|
startValue={'00'}
|
||||||
startValueOnce
|
startValueOnce
|
||||||
dummyCharacterCount={10}
|
dummyCharacterCount={10}
|
||||||
@@ -85,7 +85,7 @@ const Statistics = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className='statistic'>
|
<div className='statistic'>
|
||||||
<SlotCounter
|
<SlotCounter
|
||||||
value={'00'}
|
value={stats.year}
|
||||||
startValue={'00'}
|
startValue={'00'}
|
||||||
startValueOnce
|
startValueOnce
|
||||||
dummyCharacterCount={10}
|
dummyCharacterCount={10}
|
||||||
@@ -96,7 +96,7 @@ const Statistics = () => {
|
|||||||
</div>
|
</div>
|
||||||
<div className='statistic'>
|
<div className='statistic'>
|
||||||
<SlotCounter
|
<SlotCounter
|
||||||
value={'00'}
|
value={stats.total}
|
||||||
startValue={'00'}
|
startValue={'00'}
|
||||||
startValueOnce
|
startValueOnce
|
||||||
dummyCharacterCount={10}
|
dummyCharacterCount={10}
|
||||||
|
|||||||
Reference in New Issue
Block a user