Skip to content

Commit

Permalink
feat(API): Added Post and Delete requests
Browse files Browse the repository at this point in the history
  • Loading branch information
andy1uu committed May 6, 2024
1 parent dbc4c7d commit bde9e0d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/app/api/education/[educationID]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 14 additions & 18 deletions src/app/api/education/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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: {
Expand Down Expand Up @@ -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",
},
Expand Down Expand Up @@ -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: {
Expand All @@ -129,4 +125,4 @@ export const DELETE = async (request: NextRequest, response: NextResponse) => {
},
});
}
};
};
20 changes: 20 additions & 0 deletions src/app/education/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import baseAPI from "../api/api";

const create = () => {
return baseAPI.post("/education");
};

const getEducation = () => {
return baseAPI.get("/education");
};
Expand All @@ -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;
17 changes: 17 additions & 0 deletions src/app/education/interfaces.tsx
Original file line number Diff line number Diff line change
@@ -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;
}
19 changes: 1 addition & 18 deletions src/app/education/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,14 @@ 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) => {
return response.data;
});
}

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.",
Expand Down

0 comments on commit bde9e0d

Please sign in to comment.