-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic comments api + db + basic form pg
- Loading branch information
Showing
8 changed files
with
165 additions
and
3 deletions.
There are no files selected for viewing
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,58 @@ | ||
import { cookies } from 'next/headers'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { createComment } from '../../../database/comments'; | ||
import { commentSchema } from '../../../migrations/00003-createTableComments'; | ||
|
||
export type CommentsResponseBodyPost = | ||
| { | ||
comment: { commentText: string }; | ||
} | ||
| { | ||
error: string; | ||
}; | ||
|
||
export async function POST( | ||
request: NextRequest, | ||
): Promise<NextResponse<CommentsResponseBodyPost>> { | ||
// Task: Create a note for the current logged in user | ||
|
||
// 1. Get the note data from the request | ||
const body = await request.json(); | ||
|
||
// 2. Validate notes data with zod | ||
const result = commentSchema.safeParse(body); | ||
|
||
if (!result.success) { | ||
return NextResponse.json( | ||
{ error: 'Request does not contain note object' }, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
// 3. Get the token from the cookie | ||
const sessionTokenCookie = cookies().get('sessionToken'); | ||
|
||
// 4. Create the comment | ||
const newComment = | ||
sessionTokenCookie && | ||
(await createComment(sessionTokenCookie.value, result.data.commentText)); | ||
|
||
// 5. If the note creation fails, return an error | ||
if (!newComment) { | ||
return NextResponse.json( | ||
{ error: 'Note not created or access denied creating note' }, | ||
{ | ||
status: 400, | ||
}, | ||
); | ||
} | ||
|
||
// 6. Return the text content of the note | ||
return NextResponse.json({ | ||
comment: { | ||
commentText: newComment.commentText, | ||
}, | ||
}); | ||
} |
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,3 @@ | ||
export default function CommentsForm() { | ||
return <h1>User Comments</h1>; | ||
} |
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,24 @@ | ||
import { cookies } from 'next/headers'; | ||
import { redirect } from 'next/navigation'; | ||
import { getComments } from '../../database/comments'; | ||
import { getUser } from '../../database/users'; | ||
import CommentsForm from './CommentsForm'; | ||
|
||
export default async function CommentsPage() { | ||
// Task: Restrict access to the notes page and only display notes belonging to the current logged in user | ||
// 1. Check if the sessionToken cookie exists | ||
const sessionCookie = cookies().get('sessionToken'); | ||
|
||
// 2. Query user with the sessionToken | ||
const user = sessionCookie && (await getUser(sessionCookie.value)); | ||
|
||
// 3. If the user does not exist, redirect to the login with the returnTo query parameter | ||
if (!user) redirect(`/login?returnTo=/${user}`); | ||
|
||
// 4. Display the notes for the current logged in user | ||
|
||
const comments = await getComments(sessionCookie.value); | ||
console.log(comments); | ||
|
||
return <CommentsForm />; | ||
} |
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
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,65 @@ | ||
import { cache } from 'react'; | ||
import { sql } from './connect'; | ||
|
||
export type Comment = { | ||
id: number; | ||
eventId: number; | ||
commentText: string; | ||
userId: number; | ||
}; | ||
|
||
export const getComments = cache(async (sessionToken: string) => { | ||
const comments = await sql<Comment[]>` | ||
SELECT | ||
comments.* | ||
FROM | ||
comments | ||
INNER JOIN sessions ON ( | ||
sessions.token = ${sessionToken} | ||
AND sessions.user_id = comments.user_id | ||
AND expiry_timestamp > now() | ||
) | ||
`; | ||
return comments; | ||
}); | ||
|
||
export const getComment = cache( | ||
async (sessionToken: string, commentId: number) => { | ||
const [comment] = await sql<Comment[]>` | ||
SELECT | ||
comments.* | ||
FROM | ||
comments | ||
INNER JOIN sessions ON ( | ||
sessions.token = ${sessionToken} | ||
AND sessions.user_id = comments.user_id | ||
AND expiry_timestamp > now() | ||
) | ||
WHERE | ||
comments.id = ${commentId} | ||
`; | ||
return comment; | ||
}, | ||
); | ||
|
||
export const createComment = cache( | ||
async (sessionToken: string, textContent: string) => { | ||
const [note] = await sql<Comment[]>` | ||
INSERT INTO | ||
comments (user_id, comment_text) ( | ||
SELECT | ||
user_id, | ||
${textContent} | ||
FROM | ||
sessions | ||
WHERE | ||
token = ${sessionToken} | ||
AND sessions.expiry_timestamp > now() | ||
) | ||
RETURNING | ||
comments.* | ||
`; | ||
|
||
return note; | ||
}, | ||
); |
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