-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e316c7d
commit b08b2de
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import Comment from "@/model/comment.model"; | ||
import { connect } from "@/database/db"; | ||
import { Error } from "@/types/ErrorTypes"; | ||
import { getDataFromJwt } from "@/helper/jwtData"; | ||
import User from "@/model/user.model"; | ||
connect(); | ||
|
||
export async function POST(request: NextRequest) { | ||
try { | ||
const userID = getDataFromJwt(request); | ||
const reqBody = await request.json(); | ||
const { animeId, text, userId } = reqBody; | ||
const user = await User.findOne({ _id: userID }).select("-password"); | ||
if (!user) { | ||
return NextResponse.json( | ||
{ | ||
error: "No user found", | ||
}, | ||
{ | ||
status: 401, | ||
}, | ||
); | ||
} | ||
const newComment = new Comment({ | ||
userId, | ||
animeId, | ||
text, | ||
}); | ||
await newComment.save(); | ||
return NextResponse.json({ | ||
message: "Comment added", | ||
}); | ||
} catch (error: unknown) { | ||
const ErrorMsg = error as Error; | ||
return NextResponse.json({ error: ErrorMsg.message }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const commentSchema = new mongoose.Schema({ | ||
userId: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: "User", | ||
required: [true, "Please provide a user ID"], | ||
}, | ||
animeId: { | ||
type: String, | ||
required: [true, "Please provide an anime ID"], | ||
}, | ||
text: { | ||
type: String, | ||
required: [true, "Please provide a comment text"], | ||
}, | ||
timestamp: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
likes: { | ||
type: Number, | ||
default: 0, | ||
}, | ||
}); | ||
|
||
const Comment = mongoose.model("Comment", commentSchema); | ||
|
||
export default Comment; |