Skip to content

Commit

Permalink
update routing structure
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 10, 2024
1 parent bdaa654 commit d4e5e12
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 17 deletions.
43 changes: 43 additions & 0 deletions nextjs/app/api/party/[id]/join/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth";

export async function GET(_: Request, { params }: { params: { id: string } }) {
const session = await getServerSession();
if (!session || !session.user || !session.user.email) {
return Response.json(null, { status: 401 });
}

const party = await prisma.party.findUnique({
where: {
code: params.id,
},
});

if (!party) {
return Response.json(party, { status: 400 });
}

if (party.started) {
return Response.json(party, { status: 403 });
}

await prisma.user.update({
where: {
email: session.user.email,
},
data: {
partyId: party.id,
},
});

const res = await prisma.party.findUnique({
where: {
code: params.id,
},
include: {
players: true,
},
});

return Response.json(res);
}
13 changes: 0 additions & 13 deletions nextjs/app/api/party/join/[id]/route.ts

This file was deleted.

25 changes: 21 additions & 4 deletions nextjs/app/api/party/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,34 @@ export async function GET() {

export async function POST() {
const session = await getServerSession();
if (!session || !session.user) {
if (!session || !session.user || !session.user.email) {
return Response.json(null, { status: 401 });
}

const code = nanoid();
const party = await prisma.party.create({
data: {
code: code,
code: nanoid(),
mode: Mode.CLASSIC,
} as Party,
});

return Response.json(party);
await prisma.user.update({
where: {
email: session.user.email,
},
data: {
partyId: party.id,
},
});

const res = await prisma.party.findUnique({
where: {
id: party.id,
},
include: {
players: true,
},
});

return Response.json(res);
}

0 comments on commit d4e5e12

Please sign in to comment.