Skip to content

Commit

Permalink
feat/search: Fixed search api result
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsFlash10 committed Sep 10, 2024
1 parent 18ce148 commit 3026e33
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/app/api/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import db from '@/db';
import { CourseContent } from '@prisma/client';
import Fuse from 'fuse.js';
import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';

export type TSearchedVideos = {
id: number;
Expand All @@ -24,6 +26,7 @@ const fuzzySearch = (videos: TSearchedVideos[], searchQuery: string) => {
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const searchQuery = searchParams.get('q');
const session = await getServerSession(authOptions);

if (searchQuery && searchQuery.length > 2) {
const value: TSearchedVideos[] = await cache.get(
Expand All @@ -39,14 +42,32 @@ export async function GET(request: NextRequest) {
where: {
type: 'video',
hidden: false,
parent: {
courses: {
some: {
course: {
purchasedBy: {
some: {
userId: session?.user?.id,
},
},
},
},
},
},
},
select: {
id: true,
parentId: true,
title: true,
parent: {
select: {
courses: true,
courses: {
select: {
courseId: true,
contentId: true,
},
},
},
},
},
Expand Down

0 comments on commit 3026e33

Please sign in to comment.