From bde9e0dc074020a59978040b6e16bd1405a370ef Mon Sep 17 00:00:00 2001 From: Andy Luu Date: Mon, 6 May 2024 15:10:23 -0400 Subject: [PATCH] feat(API): Added Post and Delete requests --- src/app/api/education/[educationID]/route.ts | 2 +- src/app/api/education/route.ts | 32 +++++++++----------- src/app/education/api.ts | 20 ++++++++++++ src/app/education/interfaces.tsx | 17 +++++++++++ src/app/education/page.tsx | 19 +----------- 5 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 src/app/education/interfaces.tsx diff --git a/src/app/api/education/[educationID]/route.ts b/src/app/api/education/[educationID]/route.ts index e1960375..edfd0be2 100644 --- a/src/app/api/education/[educationID]/route.ts +++ b/src/app/api/education/[educationID]/route.ts @@ -45,7 +45,7 @@ export const GET = async (request: NextRequest, response: NextResponse) => { } }; -export const UPDATE = async (request: NextRequest, response: NextResponse) => { +export const PUT = async (request: NextRequest, response: NextResponse) => { try { const remaining = await limiter.removeTokens(1); console.log("Remaining Tokens: " + remaining); diff --git a/src/app/api/education/route.ts b/src/app/api/education/route.ts index a130bea8..c6eb4de2 100644 --- a/src/app/api/education/route.ts +++ b/src/app/api/education/route.ts @@ -2,10 +2,10 @@ import clientPromise from "../../../../lib/mongodb"; import { limiter } from "@/app/limiter"; import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; +import type { Education } from "@/app/education/interfaces"; export const GET = async (request: NextRequest, response: NextResponse) => { try { - const remaining = await limiter.removeTokens(1); console.log("Remaining Tokens: " + remaining); @@ -22,12 +22,12 @@ export const GET = async (request: NextRequest, response: NextResponse) => { const client = await clientPromise; const database = client.db(process.env.DATABASE_NAME); - const education = await database + const result = await database .collection(process.env.EDUCATION_DATABASE_NAME!) .find({}) .toArray(); - return new NextResponse(JSON.stringify(education), { + return new NextResponse(JSON.stringify(result), { status: 200, statusText: "OK", headers: { @@ -61,19 +61,17 @@ export const POST = async (request: NextRequest, response: NextResponse) => { }); } - // Change this to post logic + const newEducation: Education = request.body as unknown as Education; + const client = await clientPromise; const database = client.db(process.env.DATABASE_NAME); - const education = await database + const result = await database .collection(process.env.EDUCATION_DATABASE_NAME!) - .find({}) - .toArray(); - - + .insertOne(newEducation); - return new NextResponse(JSON.stringify(education), { - status: 200, - statusText: "OK", + return new NextResponse(JSON.stringify(result), { + status: 201, + statusText: "Created", headers: { "Content-Type": "application/json", }, @@ -105,15 +103,13 @@ export const DELETE = async (request: NextRequest, response: NextResponse) => { }); } - // Change this to delete logic const client = await clientPromise; const database = client.db(process.env.DATABASE_NAME); - const education = await database + const result = await database .collection(process.env.EDUCATION_DATABASE_NAME!) - .find({}) - .toArray(); + .deleteMany({}); - return new NextResponse(JSON.stringify(education), { + return new NextResponse(JSON.stringify(result), { status: 200, statusText: "OK", headers: { @@ -129,4 +125,4 @@ export const DELETE = async (request: NextRequest, response: NextResponse) => { }, }); } -}; \ No newline at end of file +}; diff --git a/src/app/education/api.ts b/src/app/education/api.ts index 282a3996..e7ddd1a3 100644 --- a/src/app/education/api.ts +++ b/src/app/education/api.ts @@ -1,5 +1,9 @@ import baseAPI from "../api/api"; +const create = () => { + return baseAPI.post("/education"); +}; + const getEducation = () => { return baseAPI.get("/education"); }; @@ -8,9 +12,25 @@ const getEducationByID = (educationID: string) => { return baseAPI.get(`/education/${educationID}`); }; +const updateEducationByID = (educationID: string) => { + return baseAPI.put(`/education/${educationID}`); +}; + +const deleteEducationByID = (educationID: string) => { + return baseAPI.delete(`/education/${educationID}`); +}; + +const deleteEducation = () => { + return baseAPI.delete(`/education`); +}; + const EducationAPI = { + create, getEducation, getEducationByID, + updateEducationByID, + deleteEducationByID, + deleteEducation, }; export default EducationAPI; diff --git a/src/app/education/interfaces.tsx b/src/app/education/interfaces.tsx new file mode 100644 index 00000000..bc569737 --- /dev/null +++ b/src/app/education/interfaces.tsx @@ -0,0 +1,17 @@ +interface CourseWork { + semester: string; + courses: string[]; +} + +export interface Education { + _id: ObjectId; + institution: string; + degreeType: string; + gpa: number; + location: string; + major: string; + courseWork: CourseWork[]; + extracirriculars: string[]; + endTime: string; + startTime: string; +} diff --git a/src/app/education/page.tsx b/src/app/education/page.tsx index 9a656c1a..e58b3885 100644 --- a/src/app/education/page.tsx +++ b/src/app/education/page.tsx @@ -2,6 +2,7 @@ import React from "react"; import { ObjectId } from "mongodb"; import type { Metadata } from "next"; import EducationAPI from "./api"; +import type { Education } from "./interfaces"; async function getData() { return await EducationAPI.getEducation().then((response) => { @@ -9,24 +10,6 @@ async function getData() { }); } -interface CourseWork { - semester: string; - courses: string[]; -} - -interface Education { - _id: ObjectId; - institution: string; - degreeType: string; - gpa: number; - location: string; - major: string; - courseWork: CourseWork[]; - extracirriculars: string[]; - endTime: string; - startTime: string; -} - export const metadata: Metadata = { title: "Create Next App", description: "This is a base site for Andy to create his websites.",