Skip to content

Commit

Permalink
Merge pull request #458 from AKSHITHA-CHILUKA/patch-1
Browse files Browse the repository at this point in the history
Update feedbackController.js
  • Loading branch information
hustlerZzZ authored Jul 30, 2024
2 parents 7c29c34 + 32b87f8 commit b05159e
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions server/controllers/feedbackController.js
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 };

0 comments on commit b05159e

Please sign in to comment.