added cursor based pagination for links

This commit is contained in:
Daniel
2023-06-15 02:04:54 +03:30
parent 6323badbaf
commit 1b6d902c75
29 changed files with 507 additions and 182 deletions
+2 -2
View File
@@ -1,11 +1,11 @@
import { prisma } from "@/lib/api/db";
import { LinkIncludingCollectionAndTags } from "@/types/global";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import fs from "fs";
import { Link, UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
export default async function deleteLink(
link: LinkIncludingCollectionAndTags,
link: LinkIncludingShortenedCollectionAndTags,
userId: number
) {
if (!link || !link.collectionId)
+222 -24
View File
@@ -1,31 +1,229 @@
import { prisma } from "@/lib/api/db";
export default async function getLink(userId: number) {
const links = await prisma.link.findMany({
where: {
collection: {
OR: [
{
ownerId: userId,
},
{
members: {
some: {
userId,
import { LinkRequestQuery, LinkSearchFilter, Sort } from "@/types/global";
export default async function getLink(userId: number, query: LinkRequestQuery) {
query.sort = Number(query.sort) || 0;
query.pinnedOnly = query.pinnedOnly
? JSON.parse(query.pinnedOnly as unknown as string)
: undefined;
if (query.searchFilter) {
const filterParams = (query.searchFilter as unknown as string).split("-");
query.searchFilter = {} as LinkSearchFilter;
query.searchFilter.name = JSON.parse(filterParams[0]);
query.searchFilter.url = JSON.parse(filterParams[1]);
query.searchFilter.description = JSON.parse(filterParams[2]);
query.searchFilter.collection = JSON.parse(filterParams[3]);
query.searchFilter.tags = JSON.parse(filterParams[4]);
}
console.log(query.searchFilter);
// Sorting logic
let order: any;
if (query.sort === Sort.DateNewestFirst)
order = {
createdAt: "desc",
};
else if (query.sort === Sort.DateOldestFirst)
order = {
createdAt: "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 = {
name: "asc",
};
else if (query.sort === Sort.DescriptionZA)
order = {
name: "desc",
};
const links =
// Searching logic
query.searchFilter && query.searchQuery
? await prisma.link.findMany({
take: Number(process.env.PAGINATION_TAKE_COUNT),
skip: query.cursor !== "undefined" ? 1 : undefined,
cursor:
query.cursor !== "undefined"
? {
id: Number(query.cursor),
}
: undefined,
where: {
OR: [
{
name: {
contains: query.searchFilter?.name
? query.searchQuery
: undefined,
mode: "insensitive",
},
},
{
url: {
contains: query.searchFilter?.url
? query.searchQuery
: undefined,
mode: "insensitive",
},
},
{
description: {
contains: query.searchFilter?.description
? query.searchQuery
: undefined,
mode: "insensitive",
},
},
{
collection: {
name: {
contains: query.searchFilter?.collection
? query.searchQuery
: undefined,
mode: "insensitive",
},
OR: [
{
ownerId: userId,
},
{
members: {
some: {
userId,
},
},
},
],
},
},
{
tags: {
// If tagId was defined, search by tag
some: {
name: {
contains: query.searchFilter?.tags
? query.searchQuery
: undefined,
mode: "insensitive",
},
OR: [
{ ownerId: userId }, // Tags owned by the user
{
links: {
some: {
name: {
contains: query.searchFilter?.tags
? query.searchQuery
: undefined,
mode: "insensitive",
},
collection: {
members: {
some: {
userId, // Tags from collections where the user is a member
},
},
},
},
},
},
],
},
},
},
],
},
include: {
tags: true,
collection: true,
pinnedBy: {
where: { id: userId },
select: { id: true },
},
},
],
},
},
include: {
tags: true,
collection: true,
pinnedBy: {
where: { id: userId },
select: { id: true },
},
},
});
orderBy: order || undefined,
})
: // If not searching
await prisma.link.findMany({
take: Number(process.env.PAGINATION_TAKE_COUNT),
skip: query.cursor !== "undefined" ? 1 : undefined,
cursor:
query.cursor !== "undefined"
? {
id: Number(query.cursor),
}
: undefined,
where: {
pinnedBy: query.pinnedOnly ? { some: { id: userId } } : undefined,
collection: {
id: query.collectionId && Number(query.collectionId), // If collectionId was defined, search by collection
OR: [
{
ownerId: userId,
},
{
members: {
some: {
userId,
},
},
},
],
},
tags: {
some: query.tagId // If tagId was defined, search by tag
? {
id: Number(query.tagId),
OR: [
{ ownerId: userId }, // Tags owned by the user
{
links: {
some: {
collection: {
members: {
some: {
userId, // Tags from collections where the user is a member
},
},
},
},
},
},
],
}
: undefined,
},
},
include: {
tags: true,
collection: {
select: {
id: true,
ownerId: true,
name: true,
color: true,
},
},
pinnedBy: {
where: { id: userId },
select: { id: true },
},
},
orderBy: order || undefined,
});
return { response: links, status: 200 };
}
+2 -2
View File
@@ -1,5 +1,5 @@
import { prisma } from "@/lib/api/db";
import { LinkIncludingCollectionAndTags } from "@/types/global";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import getTitle from "../../getTitle";
import archive from "../../archive";
import { Link, UsersAndCollections } from "@prisma/client";
@@ -7,7 +7,7 @@ import getPermission from "@/lib/api/getPermission";
import { existsSync, mkdirSync } from "fs";
export default async function postLink(
link: LinkIncludingCollectionAndTags,
link: LinkIncludingShortenedCollectionAndTags,
userId: number
) {
link.collection.name = link.collection.name.trim();
+2 -2
View File
@@ -1,10 +1,10 @@
import { prisma } from "@/lib/api/db";
import { LinkIncludingCollectionAndTags } from "@/types/global";
import { LinkIncludingShortenedCollectionAndTags } from "@/types/global";
import { UsersAndCollections } from "@prisma/client";
import getPermission from "@/lib/api/getPermission";
export default async function updateLink(
link: LinkIncludingCollectionAndTags,
link: LinkIncludingShortenedCollectionAndTags,
userId: number
) {
if (!link) return { response: "Please choose a valid link.", status: 401 };
+22 -17
View File
@@ -1,35 +1,40 @@
import { prisma } from "@/lib/api/db";
import { PublicLinkRequestQuery } from "@/types/global";
export default async function getCollection(collectionId: number) {
export default async function getCollection(query: PublicLinkRequestQuery) {
let data;
const collection = await prisma.collection.findFirst({
where: {
id: collectionId,
id: Number(query.collectionId),
isPublic: true,
},
include: {
links: {
select: {
id: true,
name: true,
url: true,
description: true,
collectionId: true,
createdAt: true,
},
},
},
});
if (collection) {
const user = await prisma.user.findUnique({
const links = await prisma.link.findMany({
take: Number(process.env.PAGINATION_TAKE_COUNT),
skip: query.cursor !== "undefined" ? 1 : undefined,
cursor:
query.cursor !== "undefined"
? {
id: Number(query.cursor),
}
: undefined,
where: {
id: collection.ownerId,
collection: {
id: Number(query.collectionId),
},
},
include: {
tags: true,
},
orderBy: {
createdAt: "desc",
},
});
data = { ownerName: user?.name, ...collection };
data = { ...collection, links: [...links] };
return { response: data, status: 200 };
} else {