diff --git a/app/api/topics/[id]/route.ts b/app/api/topics/[id]/route.ts new file mode 100644 index 0000000..79d328f --- /dev/null +++ b/app/api/topics/[id]/route.ts @@ -0,0 +1,26 @@ +import connectMongoDB from "@utilities/mongodb"; +import About from "@models/topic"; +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 { + title: title, + desc: desc, + } = await request.json(); + await connectMongoDB(); + await About.findByIdAndUpdate(id, { + title, + desc, + name, + }); + return NextResponse.json({ message: "Updated" }, { status: 200 }); +} + +export async function GET(request: Request, { params }: Params ) { + const { id } = params; + await connectMongoDB(); + const about = await About.findOne({ _id: id }); + return NextResponse.json({ about }, { status: 200 }); +}; \ No newline at end of file diff --git a/app/api/topics/route.ts b/app/api/topics/route.ts new file mode 100644 index 0000000..4639e24 --- /dev/null +++ b/app/api/topics/route.ts @@ -0,0 +1,22 @@ +import connectMongoDB from "@utilities/mongodb"; +import Topic from "@models/topic"; +import { NextResponse } from "next/server"; + +export async function POST(request: Request, response: Response) { + const { + title, + desc, + } = await request.json(); + await connectMongoDB(); + await Topic.create({ + title, + desc, + }); + return NextResponse.json({message: "New topic created"}) +}; + +export async function GET() { + await connectMongoDB(); + const topics = await Topic.find(); + return NextResponse.json({ topics }) +}; \ No newline at end of file diff --git a/customs/about.d.ts b/customs/about.d.ts deleted file mode 100644 index 8b72839..0000000 --- a/customs/about.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -// export type AboutType = { -// _id: string -// image: { -// data: Buffer, -// contentType: string -// }, -// title: string, -// desc: string, -// name: string, -// nickname: string, -// age: string, -// birthday: string, -// sex: string, -// nationality: string, -// status: string, -// languages: string -// } \ No newline at end of file diff --git a/customs/topic.d.ts b/customs/topic.d.ts new file mode 100644 index 0000000..4cd7fb1 --- /dev/null +++ b/customs/topic.d.ts @@ -0,0 +1,5 @@ +export type TopicType = { + _id: string + title: string, + desc: string, +} \ No newline at end of file diff --git a/models/about.ts b/models/about.ts deleted file mode 100644 index 5935cd1..0000000 --- a/models/about.ts +++ /dev/null @@ -1,26 +0,0 @@ -// import mongoose, { Schema } from "mongoose"; - -// const aboutSchema = new Schema( -// { -// image: { -// data: Buffer, -// contentType: String -// }, -// title: String, -// desc: String, -// name: String, -// nickname: String, -// age: Number, -// birthday: String, -// sex: String, -// nationality: String, -// status: String, -// languages: String -// }, { -// timestamps: true, -// } -// ); - -// const About = mongoose.models.About || mongoose.model("About", aboutSchema); - -// export default About; \ No newline at end of file diff --git a/models/topic.ts b/models/topic.ts new file mode 100644 index 0000000..6030b45 --- /dev/null +++ b/models/topic.ts @@ -0,0 +1,14 @@ +import mongoose, { Schema } from "mongoose"; + +const topicSchema = new Schema( + { + title: String, + desc: String, + }, { + timestamps: true, + } +); + +const Topic = mongoose.models.Topic || mongoose.model("Topic", topicSchema); + +export default Topic; \ No newline at end of file diff --git a/utilities/mongodb.ts b/utilities/mongodb.ts index b7118fd..4d503b8 100644 --- a/utilities/mongodb.ts +++ b/utilities/mongodb.ts @@ -1,11 +1,11 @@ -// import mongoose from "mongoose"; +import mongoose from "mongoose"; -// const connectMongoDB = async () => { -// try { -// await mongoose.connect(process.env.MONGODB_URI as string); -// } catch (error) { -// console.log(error); -// } -// }; +const connectMongoDB = async () => { + try { + await mongoose.connect(process.env.MONGODB_URI as string); + } catch (error) { + console.log(error); + } +}; -// export default connectMongoDB; \ No newline at end of file +export default connectMongoDB; \ No newline at end of file