Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update feedbackController.js #458

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 };