Skip to content

Commit

Permalink
Add posted event api route and update eventschema
Browse files Browse the repository at this point in the history
  • Loading branch information
Draikth committed Jul 23, 2024
1 parent 77dc962 commit 987149b
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ PGHOST=localhost
PGDATABASE=xxxxxxxxxxxxxx
PGUSERNAME=xxxxxxxxxxxxxx
PGPASSWORD=xxxxxxxxxxxxxx

NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME="<Your Cloud Name>"
68 changes: 68 additions & 0 deletions app/api/postedEvents/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { cookies } from 'next/headers';
import { NextRequest, NextResponse } from 'next/server';
import { createEvent, SiteEvent } from '../../../database/events';
import { eventSchema } from '../../../migrations/00002-createTableEvents';

export type PostEventsResponseBodyPost =
| {
event: SiteEvent;
}
| {
error: string;
};

export async function POST(
request: NextRequest,
): Promise<NextResponse<PostEventsResponseBodyPost>> {
// 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 = eventSchema.safeParse(body);

if (!result.success) {
return NextResponse.json(
{ error: 'Request does not contain event object' },
{
status: 400,
},
);
}

// 3. Get the token from the cookie
const sessionTokenCookie = cookies().get('sessionToken');

// 4. Create the Event
const newEvent =
sessionTokenCookie &&
(await createEvent(sessionTokenCookie.value, {
name: result.data.name,
userId: result.data.userId,
type: result.data.type,
category: result.data.category,
date: result.data.date,
location: result.data.location,
entryFee: result.data.entryFee,
description: result.data.description,
organizerUrl: result.data.organizerUrl,
duration: result.data.duration,
ageRestriction: result.data.ageRestriction,
image: result.data.image,
archived: result.data.archived,
}));

// 5. If the note creation fails, return an error
if (!newEvent) {
return NextResponse.json(
{ error: 'Note not created or access denied creating note' },
{
status: 500,
},
);
}

// 6. Return the content of the event
return NextResponse.json({ event: newEvent });
}
17 changes: 17 additions & 0 deletions migrations/00002-createTableEvents.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import { Sql } from 'postgres';
import { z } from 'zod';

export const eventSchema = z.object({
name: z.string().min(1),
userId: z.number(),
type: z.string().min(3),
date: z.date(),
location: z.string(),
duration: z.number(),
entryFee: z.number(),
category: z.string(),
description: z.string(),
organizerUrl: z.string(),
image: z.string(),
ageRestriction: z.boolean(),
archived: z.boolean(),
});

export async function up(sql: Sql) {
await sql`
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"bcrypt": "^5.1.1",
"dotenv-safe": "^9.1.0",
"next": "15.0.0-canary.67",
"next-cloudinary": "^6.6.2",
"postgres": "^3.4.4",
"react": "19.0.0-rc.0",
"react-dom": "19.0.0-rc.0",
Expand Down
57 changes: 57 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 987149b

Please sign in to comment.