Skip to content

Commit

Permalink
feat: add projects api
Browse files Browse the repository at this point in the history
  • Loading branch information
moonbamijam committed Feb 15, 2024
1 parent d74eb18 commit bbe032d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
29 changes: 29 additions & 0 deletions app/api/projects/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import connectMongoDB from "@utilities/mongodb";
import Project from "@models/project";
import { Params } from "next/dist/shared/lib/router/utils/route-matcher";
import { NextResponse } from "next/server";

export async function PUT(request: Request, { params }: Params) {
const { id } = params;
const {
src: src,
projectTitle: projectTitle,
projectDesc: projectDesc,
href: href,
} = await request.json();
await connectMongoDB();
await Project.findByIdAndUpdate(id, {
src,
projectTitle,
projectDesc,
href,
});
return NextResponse.json({ message: "Project me updated" }, { status: 200 });
};

export async function GET(request: Request, { params }: Params ) {
const { id } = params;
await connectMongoDB();
const projects = await Project.findOne({ _id: id });
return NextResponse.json({ projects }, { status: 200 });
};
26 changes: 26 additions & 0 deletions app/api/projects/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import connectMongoDB from "@utilities/mongodb";
import Project from "@models/project";
import { NextResponse } from "next/server";

export async function POST(request: Request, response: Response) {
const {
src,
projectTitle,
projectDesc,
href,
} = await request.json();
await connectMongoDB();
await Project.create({
src,
projectTitle,
projectDesc,
href,
});
return NextResponse.json({message: "Project created"})
};

export async function GET() {
await connectMongoDB();
const projects = await Project.find();
return NextResponse.json({ projects })
};
9 changes: 9 additions & 0 deletions customs/project.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Url } from "next/dist/shared/lib/router/router"

export type ProjectType = {
_id?: string
src: Url,
projectTitle: string,
projectDesc: string,
href: string,
}
16 changes: 16 additions & 0 deletions models/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import mongoose, { Schema } from "mongoose";

const projectSchema = new Schema(
{
src: String,
projectTitle: String,
projectDesc: String,
href: String,
}, {
timestamps: true,
}
);

const Project = mongoose.models.Project || mongoose.model("Project", projectSchema);

export default Project;

0 comments on commit bbe032d

Please sign in to comment.