Skip to content

Commit

Permalink
fix: add edit apis
Browse files Browse the repository at this point in the history
  • Loading branch information
amjed-ali-k committed Oct 28, 2023
1 parent b7af6fc commit 7fa098d
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 24 deletions.
49 changes: 48 additions & 1 deletion src/app/api/secure/exam-seating/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const schema = z.object({
.min(1),
});

export async function PUT(request: NextRequest) {
export async function POST(request: NextRequest) {
const userId = await getUserId(request);
if (!userId)
return NextResponse.json({ message: "Unauthenticated" }, { status: 401 });
Expand Down Expand Up @@ -55,6 +55,53 @@ export async function PUT(request: NextRequest) {
return NextResponse.json(results);
}

const updateSchema = z.object({
name: z.string(),
id: z.string().uuid(),
structure: z
.array(
z
.array(
z.object({
name: z.string(),
seatCount: z.number().positive(),
structure: z.array(z.number().nonnegative()),
})
)
.min(1)
)
.min(1),
});
export async function PUT(request: NextRequest) {
const userId = await getUserId(request);
if (!userId)
return NextResponse.json({ message: "Unauthenticated" }, { status: 401 });

const body = updateSchema.safeParse(await request.json());
if (!body.success) {
const { errors } = body.error;
return NextResponse.json(
{ message: "Invalid request", errors },
{ status: 400 }
);
}

const results = await prisma.examHall.update({
where: {
id: body.data.id,
},
data: {
name: body.data.name,
structure: body.data.structure,
theoryOnlySeats: getCount(body.data.structure, SeatType.THEORY),
drawingOnlySeats: getCount(body.data.structure, SeatType.DRAWING),
commonSeats: getCount(body.data.structure, SeatType.COMMON),
},
});

return NextResponse.json(results);
}

export type SeatObjectType = {
name: string;
seatCount: number;
Expand Down
46 changes: 45 additions & 1 deletion src/app/api/secure/exam-seating/student-batches/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const schema = z.object({
.min(1),
});

export async function PUT(request: NextRequest) {
export async function POST(request: NextRequest) {
const userId = await getUserId(request);
if (!userId)
return NextResponse.json({ message: "Unauthenticated" }, { status: 401 });
Expand All @@ -44,6 +44,50 @@ export async function PUT(request: NextRequest) {
return NextResponse.json(results);
}

const updateSchema = z.object({
id: z.string().uuid(),
name: z.string().min(3, "Minimum 3 characters required"),
students: z
.array(
z.object({
name: z.string().optional(),
primaryNumber: z.string(),
rollNumber: z.string().optional(),
regNumber: z.string().optional(),
admnNumber: z.string().optional(),
})
)
.min(1),
});

export async function PUT(request: NextRequest) {
const userId = await getUserId(request);
if (!userId)
return NextResponse.json({ message: "Unauthenticated" }, { status: 401 });

const body = updateSchema.safeParse(await request.json());
if (!body.success) {
const { errors } = body.error;
return NextResponse.json(
{ message: "Invalid request", errors },
{ status: 400 }
);
}

const results = await prisma.studentBatchForExam.update({
where: {
id: body.data.id,
},
data: {
name: body.data.name,
students: body.data.students,
studentsCount: body.data.students.length,
},
});

return NextResponse.json(results);
}

const deleteSchema = z.object({
id: z.string(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
import * as React from "react";
import dayjs from "dayjs";

import {
CaretSortIcon,
ChevronDownIcon,
DotsHorizontalIcon,
} from "@radix-ui/react-icons";
import { CaretSortIcon, DotsHorizontalIcon } from "@radix-ui/react-icons";
import {
ColumnDef,
ColumnFiltersState,
SortingState,
VisibilityState,
flexRender,
getCoreRowModel,
getPaginationRowModel,
Expand All @@ -23,7 +17,6 @@ import {
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuCheckboxItem,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
Expand All @@ -43,17 +36,6 @@ import axios from "axios";
import { mutate } from "swr";
import { Skeleton } from "@/components/ui/skeleton";
import { ExamHall } from "@prisma/client";
import { useToast } from "@/components/ui/use-toast";

type ClassLayoutApiType = {
id: string;
createdAt: string;
commonSeats: number;
theoryOnlySeats: number;
drawingOnlySeats: number;
name: string;
createdBy: string;
};

export const columns: ColumnDef<ExamHall>[] = [
{
Expand Down Expand Up @@ -148,7 +130,6 @@ export function ClassListTable() {

const [sorting, setSorting] = React.useState<SortingState>([]);
const [rowSelection, setRowSelection] = React.useState({});
console.log(data);
const table = useReactTable({
data,
columns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function NewClassComponent() {
});
console.log(structure);
axios
.put<ExamHall>("/api/secure/exam-seating", { name: hallName, structure })
.post<ExamHall>("/api/secure/exam-seating", { name: hallName, structure })
.then((res) => {
toast({
title: "Well done!",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function NewStudentBatchComponent() {

const onSubmit = (data: z.infer<typeof formSchema>) => {
axios
.put<StudentBatchForExam>(
.post<StudentBatchForExam>(
"/api/secure/exam-seating/student-batches",
data
)
Expand Down

0 comments on commit 7fa098d

Please sign in to comment.