new dashboard route
This commit is contained in:
parent
c216a92474
commit
586074ef43
|
@ -0,0 +1,119 @@
|
||||||
|
import { prisma } from "@/lib/api/db";
|
||||||
|
import { LinkRequestQuery, Sort } from "@/types/global";
|
||||||
|
|
||||||
|
type Response<D> =
|
||||||
|
| {
|
||||||
|
data: D;
|
||||||
|
message: string;
|
||||||
|
status: number;
|
||||||
|
}
|
||||||
|
| {
|
||||||
|
data: D;
|
||||||
|
message: string;
|
||||||
|
status: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function getDashboardData(
|
||||||
|
userId: number,
|
||||||
|
query: LinkRequestQuery
|
||||||
|
): Promise<Response<any>> {
|
||||||
|
let order: any;
|
||||||
|
if (query.sort === Sort.DateNewestFirst) order = { id: "desc" };
|
||||||
|
else if (query.sort === Sort.DateOldestFirst) order = { id: "asc" };
|
||||||
|
else if (query.sort === Sort.NameAZ) order = { name: "asc" };
|
||||||
|
else if (query.sort === Sort.NameZA) order = { name: "desc" };
|
||||||
|
else if (query.sort === Sort.DescriptionAZ) order = { description: "asc" };
|
||||||
|
else if (query.sort === Sort.DescriptionZA) order = { description: "desc" };
|
||||||
|
|
||||||
|
const numberOfPinnedLinks = await prisma.link.count({
|
||||||
|
where: {
|
||||||
|
AND: [
|
||||||
|
{
|
||||||
|
collection: {
|
||||||
|
OR: [
|
||||||
|
{ ownerId: userId },
|
||||||
|
{
|
||||||
|
members: {
|
||||||
|
some: { userId },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pinnedBy: { some: { id: userId } },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const pinnedLinks = await prisma.link.findMany({
|
||||||
|
take: 10,
|
||||||
|
where: {
|
||||||
|
AND: [
|
||||||
|
{
|
||||||
|
collection: {
|
||||||
|
OR: [
|
||||||
|
{ ownerId: userId },
|
||||||
|
{
|
||||||
|
members: {
|
||||||
|
some: { userId },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pinnedBy: { some: { id: userId } },
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
tags: true,
|
||||||
|
collection: true,
|
||||||
|
pinnedBy: {
|
||||||
|
where: { id: userId },
|
||||||
|
select: { id: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: order || { id: "desc" },
|
||||||
|
});
|
||||||
|
|
||||||
|
const recentlyAddedLinks = await prisma.link.findMany({
|
||||||
|
take: 10,
|
||||||
|
where: {
|
||||||
|
collection: {
|
||||||
|
OR: [
|
||||||
|
{ ownerId: userId },
|
||||||
|
{
|
||||||
|
members: {
|
||||||
|
some: { userId },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
tags: true,
|
||||||
|
collection: true,
|
||||||
|
pinnedBy: {
|
||||||
|
where: { id: userId },
|
||||||
|
select: { id: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
orderBy: order || { id: "desc" },
|
||||||
|
});
|
||||||
|
|
||||||
|
const links = [...recentlyAddedLinks, ...pinnedLinks].sort(
|
||||||
|
(a, b) => (new Date(b.id) as any) - (new Date(a.id) as any)
|
||||||
|
);
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: {
|
||||||
|
links,
|
||||||
|
numberOfPinnedLinks,
|
||||||
|
},
|
||||||
|
message: "Dashboard data fetched successfully.",
|
||||||
|
status: 200,
|
||||||
|
};
|
||||||
|
}
|
|
@ -3,7 +3,10 @@ import { LinkRequestQuery } from "@/types/global";
|
||||||
import getDashboardData from "@/lib/api/controllers/dashboard/getDashboardData";
|
import getDashboardData from "@/lib/api/controllers/dashboard/getDashboardData";
|
||||||
import verifyUser from "@/lib/api/verifyUser";
|
import verifyUser from "@/lib/api/verifyUser";
|
||||||
|
|
||||||
export default async function links(req: NextApiRequest, res: NextApiResponse) {
|
export default async function dashboard(
|
||||||
|
req: NextApiRequest,
|
||||||
|
res: NextApiResponse
|
||||||
|
) {
|
||||||
const user = await verifyUser({ req, res });
|
const user = await verifyUser({ req, res });
|
||||||
if (!user) return;
|
if (!user) return;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
|
import { LinkRequestQuery } from "@/types/global";
|
||||||
|
import getDashboardDataV2 from "@/lib/api/controllers/dashboard/getDashboardDataV2";
|
||||||
|
import verifyUser from "@/lib/api/verifyUser";
|
||||||
|
|
||||||
|
export default async function dashboard(
|
||||||
|
req: NextApiRequest,
|
||||||
|
res: NextApiResponse
|
||||||
|
) {
|
||||||
|
const user = await verifyUser({ req, res });
|
||||||
|
if (!user) return;
|
||||||
|
|
||||||
|
if (req.method === "GET") {
|
||||||
|
const convertedData: LinkRequestQuery = {
|
||||||
|
sort: Number(req.query.sort as string),
|
||||||
|
cursor: req.query.cursor ? Number(req.query.cursor as string) : undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
const data = await getDashboardDataV2(user.id, convertedData);
|
||||||
|
return res.status(data.status).json({
|
||||||
|
data: {
|
||||||
|
links: data.data.links,
|
||||||
|
numberOfPinnedLinks: data.data.numberOfPinnedLinks,
|
||||||
|
},
|
||||||
|
message: data.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Ŝarĝante…
Reference in New Issue