Skip to content

Commit

Permalink
Add api route for cloudinary
Browse files Browse the repository at this point in the history
  • Loading branch information
Draikth committed Jul 24, 2024
1 parent 3515d89 commit fd8e018
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 10 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ PGDATABASE=xxxxxxxxxxxxxx
PGUSERNAME=xxxxxxxxxxxxxx
PGPASSWORD=xxxxxxxxxxxxxx

# NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME="<Your Cloud Name>"
NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME=xxxxxx
NEXT_PUBLIC_CLOUDINARY_API_KEY=xxxxxx
CLOUDINARY_API_SECRET=xxxxxx
CLOUDINARY_URL=xxxxxx
18 changes: 18 additions & 0 deletions app/api/imageUpload/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { v2 as cloudinary } from 'cloudinary';
import { NextRequest, NextResponse } from 'next/server';

// eslint-disable-next-line no-restricted-syntax
export async function POST(request: NextRequest): Promise<NextResponse> {
const body = (await request.json()) as {
paramsToSign: Record<string, string>;
};

const { paramsToSign } = body;

const signature = cloudinary.utils.api_sign_request(
paramsToSign,
process.env.CLOUDINARY_API_SECRET as string,
);

return NextResponse.json({ signature });
}
44 changes: 44 additions & 0 deletions app/api/postedEvents/deleteEvent/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { NextRequest, NextResponse } from 'next/server';
import { deleteEvent } from '../../../../database/events';

type DeleteEventResponseBody =
| { message: string; event?: any }
| { message: string; error?: string };

export async function DELETE(
request: NextRequest,
): Promise<NextResponse<DeleteEventResponseBody>> {
const { searchParams } = new URL(request.url);
const eventId = searchParams.get('eventId');
const { sessionToken } = await request.json();

if (!sessionToken || !eventId) {
return NextResponse.json(
{
message: 'Invalid request',
error: 'Session token or eventId is missing',
},
{ status: 400 },
);
}

try {
const event = await deleteEvent(sessionToken, parseInt(eventId, 10));
if (event) {
return NextResponse.json(
{ message: 'Event deleted successfully', event },
{ status: 200 },
);
} else {
return NextResponse.json(
{ message: 'Event not found or session expired' },
{ status: 404 },
);
}
} catch (error) {
return NextResponse.json(
{ message: 'Internal server error', error: (error as Error).message },
{ status: 500 },
);
}
}
10 changes: 4 additions & 6 deletions app/api/postedEvents/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ export type PostEventsResponseBodyPost =
export async function POST(
request: Request,
): Promise<NextResponse<PostEventsResponseBodyPost>> {
// Task: Create a note for the current logged in user

// 1. Get the note data from the request
// 1. Get the event data from the request
const body = await request.json();

// 2. Validate notes data with zod
// 2. Validate events data with zod
const result = eventSchema.safeParse(body);

if (!result.success) {
Expand Down Expand Up @@ -55,10 +53,10 @@ export async function POST(
archived: result.data.archived,
}));

// 5. If the note creation fails, return an error
// 5. If the event creation fails, return an error
if (!postEvent) {
return NextResponse.json(
{ error: 'Note not created or access denied creating note' },
{ error: 'Event not created or access denied creating event' },
{
status: 500,
},
Expand Down
5 changes: 2 additions & 3 deletions app/profile/[username]/DeleteProfileForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function DeleteProfileForm({
const handleDelete = async (event: React.FormEvent) => {
event.preventDefault();

const response = await fetch('/api/user/delete', {
const response = await fetch('/api/postedEvents/deleteEvent', {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Expand All @@ -24,10 +24,9 @@ export default function DeleteProfileForm({
});

if (response.ok) {
router.push('/login'); // Redirect to login page after deletion
router.refresh();
} else {
console.error('Failed to delete user');
console.error('Failed to delete event');
}
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dependencies": {
"@upleveled/ley": "^0.8.6",
"bcrypt": "^5.1.1",
"cloudinary": "^2.3.0",
"dotenv-safe": "^9.1.0",
"next": "15.0.0-canary.67",
"next-cloudinary": "^6.6.2",
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd8e018

Please sign in to comment.