Skip to content

Commit

Permalink
Merge pull request #94 from emil-webkom/admin-event
Browse files Browse the repository at this point in the history
Admin event
  • Loading branch information
Mauritzskog authored Sep 27, 2024
2 parents f151013 + 4c065ef commit c2ea119
Show file tree
Hide file tree
Showing 13 changed files with 1,240 additions and 110 deletions.
17 changes: 9 additions & 8 deletions frontend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ model User {
role UserRole @default(USER)
nummer Int?
isActive Boolean @default(true)
kontigent Kontigent @default(UBETALT)
username String? @unique
kontigent Kontigent @default(UBETALT)
accounts Account[]
paameldinger ArrangementPaamelding[]
bookings Booking[]
Expand Down Expand Up @@ -99,11 +99,11 @@ model Arrangement {
bilde String?
kapasitet Int?
beskrivelse String
arrangorID String
arrangorID String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
trinn Int[] @default([])
arrangor komite @relation(fields: [arrangorID], references: [id])
arrangor komite? @relation(fields: [arrangorID], references: [id])
paameldinger ArrangementPaamelding[]
}

Expand Down Expand Up @@ -171,9 +171,10 @@ model aeresEmiler {
navn String
aar Int
}
model Kontigentpris{
id String @id @default(cuid())
aar String

model Kontigentpris {
id String @id @default(cuid())
aar String
pris Int
}

Expand All @@ -200,8 +201,8 @@ enum UserRole {
SUPER_USER
}

enum Kontigent{
enum Kontigent {
BETALT
UBETALT
AVVENTER_BEKREFTELSE
}
}
90 changes: 40 additions & 50 deletions frontend/src/app/api/arrangementer/[id]/paamelding/route.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { getUserById } from "@/data/user";
import { db } from "@/lib/db";
import {
ArrangementPaamelding,
ArrangementPaameldingSchema,
} from "@/schemas/arrangement";
import { id } from "date-fns/locale";
import { NextRequest, NextResponse } from "next/server";

export const revalidate = 0;
Expand All @@ -20,57 +15,52 @@ export async function GET(
},
});

return NextResponse.json({data: arrangementPaameldinger }, { status: 200 });
return NextResponse.json(
{ data: arrangementPaameldinger },
{ status: 200 },
);
} catch (error) {
return NextResponse.json({ error: error }, { status: 500 });
}
}

// export async function DELETE(
// req: NextRequest,
// { params }: { params: { id: string } },
// ) {
// try {
// const { searchParams } = new URL(req.url);
// const userID = searchParams.get("userID");
export async function DELETE(
req: NextRequest,
{ params }: { params: { id: string } },
) {
try {
const { id: eventId } = params;
const { userId } = await req.json();

// if (!userID) {
// return NextResponse.json(
// { error: "UserID is required" },
// { status: 400 },
// );
// }
if (!userId || !eventId) {
return NextResponse.json(
{ error: "Missing eventId or userId" },
{ status: 400 },
);
}

// const arrangementPaamelding = await db.arrangementPaamelding.findFirst({
// where: {
// arrangementID: params.id,
// userID: userID,
// },
// include: {
// arrangement: true,
// },
// });
const deletedSignup = await db.arrangementPaamelding.delete({
where: {
userID_arrangementID: {
userID: userId,
arrangementID: eventId,
},
},
});

// if (arrangementPaamelding) {
// await db.arrangementPaamelding.delete({
// where: {
// id: arrangementPaamelding.id,
// },
// });
if (!deletedSignup) {
return NextResponse.json({ error: "Signup not found" }, { status: 404 });
}

// return NextResponse.json(
// {
// success: `Du har meldt deg av: ${arrangementPaamelding.arrangement.navn}`,
// },
// { status: 200 },
// );
// } else {
// return NextResponse.json(
// { error: "ArrangementPaamelding not found." },
// { status: 404 },
// );
// }
// } catch (error) {
// return NextResponse.json({ error: error }, { status: 500 });
// }
// }
return NextResponse.json(
{ message: "Signup deleted successfully" },
{ status: 200 },
);
} catch (error) {
console.error("Error deleting signup:", error);
return NextResponse.json(
{ error: "An error occurred while deleting the signup" },
{ status: 500 },
);
}
}
45 changes: 43 additions & 2 deletions frontend/src/app/api/arrangementer/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { NextRequest, NextResponse } from "next/server";
import {
updateArrangementSchema,
deleteArrangementSchema,
createArrangementSchema,
} from "@/schemas/arrangement";
import { z } from "zod";

export const revalidate = 0;

Expand Down Expand Up @@ -37,11 +39,50 @@ export async function DELETE(
) {
try {
const parsedData = deleteArrangementSchema.parse({ id: params.id });
await db.booking.delete({
await db.arrangement.delete({
where: { id: parsedData.id },
});
return NextResponse.json({ message: "Booking deleted" }, { status: 200 });
return NextResponse.json(
{ message: "Arrangement deleted" },
{ status: 200 },
);
} catch (error) {
return NextResponse.json({ error: error }, { status: 400 });
}
}

export async function PUT(
req: NextRequest,
{ params }: { params: { id: string } },
) {
try {
const { id } = params;
const body = await req.json();
// console.log(body);
// const validatedData = createArrangementSchema.parse({
// ...body,
// });
const updatedEvent = await db.arrangement.update({
where: { id },
data: body,
});

if (!updatedEvent) {
return NextResponse.json({ error: "Event not found" }, { status: 404 });
}

return NextResponse.json(updatedEvent, { status: 200 });
} catch (error) {
if (error instanceof z.ZodError) {
return NextResponse.json(
{ error: "Something went wrong" },
{ status: 400 },
);
}
console.error("Error updating event:", error);
return NextResponse.json(
{ error: "Internal Server Error" },
{ status: 500 },
);
}
}
32 changes: 32 additions & 0 deletions frontend/src/app/api/arrangementer/[id]/users/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getUserById } from "@/data/user";
import { db } from "@/lib/db";
import { NextRequest, NextResponse } from "next/server";

export async function GET(
req: NextRequest,
{ params }: { params: { id: string } },
) {
try {
const paameldinger = await db.arrangementPaamelding.findMany({
where: {
arrangementID: params.id,
},
});

if (!paameldinger || paameldinger.length === 0) {
return NextResponse.json(
{ error: "Ingen paameldinger funnet" },
{ status: 404 },
);
}
const userPromises = paameldinger.map((p) => getUserById(p.userID));
const users = await Promise.all(userPromises);

return NextResponse.json(users);
} catch (error) {
return NextResponse.json(
{ error: "En feil oppstod ved henting av brukere" },
{ status: 500 },
);
}
}
54 changes: 54 additions & 0 deletions frontend/src/app/api/arrangementer/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { db } from "@/lib/db";
import { createArrangementSchema } from "@/schemas/arrangement";
import { NextRequest, NextResponse } from "next/server";
import { z, ZodError } from "zod";

export const revalidate = 0;

Expand All @@ -24,3 +26,55 @@ export async function GET(req: NextRequest) {
return NextResponse.json({ error: error }, { status: 400 });
}
}

export async function POST(req: NextRequest) {
try {
const body = await req.json();
console.log("Received body:", body);

const parsedBody = {
...body,
dato: body.dato ? new Date(body.dato) : undefined,
};

const validatedData = createArrangementSchema.parse(parsedBody);
console.log("Validated data:", validatedData);

const newEvent = await db.arrangement.create({
data: {
...validatedData,
paameldinger: {
create: [],
},
},
});

console.log("New event created:", newEvent);

return NextResponse.json(
{ message: "Arrangement created successfully", data: newEvent },
{ status: 201 },
);
} catch (error) {
console.error("Error in POST /api/arrangementer:", error);

if (error instanceof ZodError) {
return NextResponse.json(
{ error: "Validation error", details: error.errors },
{ status: 400 },
);
}

if (error instanceof Error) {
return NextResponse.json(
{ error: "Database error", message: error.message },
{ status: 500 },
);
}

return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 },
);
}
}
7 changes: 5 additions & 2 deletions frontend/src/components/admin/admin-dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import BookingComponent from "./booking";
import KomiteComponent from "./komiteComponent";
import HovedstyreComponent from "./hovedstyret";
import LavterskelarrangementComponent from "./lavterskelarrangement";
import ArrangementComponent from "./arrangement";
import ArrangementComponentNew from "./arrangementForm";

export function Dashboard() {
return (
Expand Down Expand Up @@ -67,8 +69,9 @@ export function Dashboard() {
<KomiteComponent />
</TabsContent>

<TabsContent value="Arrangementer">
<div>Arrangementer</div>
<TabsContent value="Arrangement">
{/* <ArrangementComponent /> */}
<ArrangementComponentNew />
</TabsContent>

<TabsContent value="Lavterskelarrangement">
Expand Down
Loading

0 comments on commit c2ea119

Please sign in to comment.