Skip to content

Commit

Permalink
feat: add api to topics
Browse files Browse the repository at this point in the history
  • Loading branch information
moonbamijam committed Feb 2, 2024
1 parent 16d00dc commit 0d10aef
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 52 deletions.
26 changes: 26 additions & 0 deletions app/api/topics/[id]/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
};
22 changes: 22 additions & 0 deletions app/api/topics/route.ts
Original file line number Diff line number Diff line change
@@ -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 })
};
17 changes: 0 additions & 17 deletions customs/about.d.ts

This file was deleted.

5 changes: 5 additions & 0 deletions customs/topic.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type TopicType = {
_id: string
title: string,
desc: string,
}
26 changes: 0 additions & 26 deletions models/about.ts

This file was deleted.

14 changes: 14 additions & 0 deletions models/topic.ts
Original file line number Diff line number Diff line change
@@ -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;
18 changes: 9 additions & 9 deletions utilities/mongodb.ts
Original file line number Diff line number Diff line change
@@ -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;
export default connectMongoDB;

0 comments on commit 0d10aef

Please sign in to comment.