-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #458 from AKSHITHA-CHILUKA/patch-1
Update feedbackController.js
- Loading branch information
Showing
1 changed file
with
22 additions
and
14 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 |
---|---|---|
@@ -1,18 +1,26 @@ | ||
const asyncHandler = require('express-async-handler'); | ||
const Feedback = require("../models/studentfeeback"); | ||
|
||
const Feedback = require('../models/studentfeedback'); | ||
const { validationResult } = require('express-validator'); // For input validation | ||
// Middleware for input validation (example using express-validator) | ||
const validateFeedback = [ | ||
check('message').notEmpty().withMessage('Message is required'), | ||
check('canteenId').notEmpty().withMessage('Canteen ID is required'), | ||
check('userId').notEmpty().withMessage('User ID is required') | ||
]; | ||
const submitFeedback = asyncHandler(async (req, res) => { | ||
const { message, canteenId ,userId} = req.body; | ||
|
||
|
||
if (!message || !canteenId) { | ||
res.status(400); | ||
throw new Error('Message and Canteen ID are required'); | ||
// Validate request inputs | ||
const errors = validationResult(req); | ||
if (!errors.isEmpty()) { | ||
return res.status(400).json({ errors: errors.array() }); | ||
} | ||
const { message, canteenId, userId } = req.body; | ||
try { | ||
const feedback = new Feedback({ message, canteenId, userId }); | ||
await feedback.save(); | ||
res.status(201).json({ message: 'Feedback submitted successfully' }); | ||
} catch (error) { | ||
console.error('Error submitting feedback:', error); // Log the error for debugging | ||
res.status(500).json({ error: 'Server error, could not submit feedback' }); | ||
} | ||
|
||
const feedback = new Feedback({ message, canteenId, userId }); | ||
await feedback.save(); | ||
res.status(201).json({ message: 'Feedback submitted successfully' }); | ||
}); | ||
|
||
module.exports = { submitFeedback }; | ||
module.exports = { submitFeedback, validateFeedback }; |