-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from nimit9/feat/bookmarks
Feat/bookmarks
- Loading branch information
Showing
25 changed files
with
573 additions
and
32 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
prisma/migrations/20240318200949_add_bookmark/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- CreateTable | ||
CREATE TABLE "Bookmark" ( | ||
"id" SERIAL NOT NULL, | ||
"userId" TEXT NOT NULL, | ||
"contentId" INTEGER NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"courseId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "Bookmark_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "Bookmark_contentId_key" ON "Bookmark"("contentId"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Bookmark" ADD CONSTRAINT "Bookmark_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Bookmark" ADD CONSTRAINT "Bookmark_contentId_fkey" FOREIGN KEY ("contentId") REFERENCES "Content"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Bookmark" ADD CONSTRAINT "Bookmark_courseId_fkey" FOREIGN KEY ("courseId") REFERENCES "Course"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use server'; | ||
import { createSafeAction } from '@/lib/create-safe-action'; | ||
import { BookmarkCreateSchema, BookmarkDeleteSchema } from './schema'; | ||
import { getServerSession } from 'next-auth'; | ||
import { authOptions } from '@/lib/auth'; | ||
import { rateLimit } from '@/lib/utils'; | ||
import db from '@/db'; | ||
import { | ||
InputTypeCreateBookmark, | ||
InputTypeDeleteBookmark, | ||
ReturnTypeCreateBookmark, | ||
} from './types'; | ||
import { revalidatePath } from 'next/cache'; | ||
|
||
const reloadBookmarkPage = (courseId: number) => { | ||
revalidatePath(`/courses/${courseId}/bookmarks`); | ||
}; | ||
|
||
const createBookmarkHandler = async ( | ||
data: InputTypeCreateBookmark, | ||
): Promise<ReturnTypeCreateBookmark> => { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (!session || !session.user) { | ||
return { error: 'Unauthorized or insufficient permissions' }; | ||
} | ||
|
||
const { contentId, courseId } = data; | ||
const userId = session.user.id; | ||
|
||
if (!rateLimit(userId)) { | ||
return { error: 'Rate limit exceeded. Please try again later.' }; | ||
} | ||
|
||
try { | ||
const addedBookmark = await db.bookmark.create({ | ||
data: { contentId, userId, courseId }, | ||
}); | ||
reloadBookmarkPage(courseId); | ||
return { data: addedBookmark }; | ||
} catch (error: any) { | ||
return { error: error.message || 'Failed to create comment.' }; | ||
} | ||
}; | ||
|
||
const deleteBookmarkHandler = async ( | ||
data: InputTypeDeleteBookmark, | ||
): Promise<any> => { | ||
const session = await getServerSession(authOptions); | ||
|
||
if (!session || !session.user) { | ||
return { error: 'Unauthorized or insufficient permissions' }; | ||
} | ||
|
||
const { id, courseId } = data; | ||
const userId = session.user.id; | ||
|
||
if (!rateLimit(userId)) { | ||
return { error: 'Rate limit exceeded. Please try again later.' }; | ||
} | ||
|
||
try { | ||
const deletedBookmark = await db.bookmark.delete({ | ||
where: { id }, | ||
}); | ||
reloadBookmarkPage(courseId); | ||
return { data: deletedBookmark }; | ||
} catch (error: any) { | ||
return { error: error.message || 'Failed to create comment.' }; | ||
} | ||
}; | ||
|
||
export const createBookmark = createSafeAction( | ||
BookmarkCreateSchema, | ||
createBookmarkHandler, | ||
); | ||
export const deleteBookmark = createSafeAction( | ||
BookmarkDeleteSchema, | ||
deleteBookmarkHandler, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { z } from 'zod'; | ||
|
||
export const BookmarkCreateSchema = z.object({ | ||
contentId: z.number(), | ||
courseId: z.number(), | ||
}); | ||
export const BookmarkDeleteSchema = z.object({ | ||
id: z.number(), | ||
courseId: z.number(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { z } from 'zod'; | ||
import { BookmarkCreateSchema, BookmarkDeleteSchema } from './schema'; | ||
import { ActionState } from '@/lib/create-safe-action'; | ||
import { Bookmark, Content } from '@prisma/client'; | ||
|
||
export type InputTypeCreateBookmark = z.infer<typeof BookmarkCreateSchema>; | ||
export type ReturnTypeCreateBookmark = ActionState< | ||
InputTypeCreateBookmark, | ||
Bookmark | ||
>; | ||
export type InputTypeDeleteBookmark = z.infer<typeof BookmarkDeleteSchema>; | ||
export type ReturnTypeDeleteBookmark = ActionState< | ||
InputTypeDeleteBookmark, | ||
Bookmark | ||
>; | ||
|
||
export type TBookmarkWithContent = Bookmark & { | ||
content: Content & { parent?: Content | null }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -93,4 +93,4 @@ | |
|
||
.vjs-duration { | ||
display: block !important; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.