-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added api modal route and function for saving user feeback
- Loading branch information
1 parent
08e8c6f
commit 185b7d8
Showing
5 changed files
with
115 additions
and
2 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
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,24 @@ | ||
import feedback from "../models/feedback.js"; | ||
|
||
export async function saveFeedback(req, resp) { | ||
try { | ||
const { name, userfeedback, email, rating } = req.body; | ||
|
||
if (!name || !userfeedback || !email || typeof rating !== "number") { | ||
return resp.status(400).json({ message: "All fields are required." }); | ||
} | ||
|
||
const newfeedback = new feedback({ name, userfeedback, email, rating }); | ||
|
||
// Save feedback to the database | ||
await newfeedback.save(); | ||
|
||
// Respond with success message | ||
resp | ||
.status(201) | ||
.json({ message: "Feedback saved successfully!", newfeedback }); | ||
} catch (error) { | ||
console.error("Error saving feedback:", error); | ||
resp.status(500).json({ message: "Failed to save feedback.", error }); | ||
} | ||
} |
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 feedbackSchema = new mongoose.Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
}, | ||
userfeedback: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
}, | ||
rating: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}, | ||
{ timestamps: true } | ||
); | ||
|
||
const feedback = mongoose.model(" feedback", feedbackSchema); | ||
|
||
export default feedback; |
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,7 @@ | ||
import express from "express"; | ||
const router = express.Router(); | ||
import { saveFeedback } from "../controllers/feedbackcontroller.js"; | ||
|
||
router.post("/savefeedback", saveFeedback); | ||
|
||
export default router; |
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