Skip to content
Open
Show file tree
Hide file tree
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
28 changes: 22 additions & 6 deletions api/controllers/infopostControllers.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const asyncHandler = require("express-async-handler");
const dotenv = require("dotenv");
const axios = require('axios');
dotenv.config();
const infopostModel = require("../models/infopostModel");
const imageModel = require("../models/imageModel");
const userModel = require("../models/userModel");
const { createNotification } = require("../controllers/notificationController");
const path = require("path");

// multer middleware for handling uploading images
const multer = require("multer");
const ROLES_LIST = require("../../config/roles_list");
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, "../html/uploads");
Expand Down Expand Up @@ -44,18 +49,29 @@ const postinfopost = asyncHandler(async (req, res) => {
savedImages.push(image.filename);
}
}
//defining tag for the question
const query = req.body.body;
// const tag_response = await axios.post('http://localhost:5001/tag', { query });
// const classified_tag = tag_response.data;
const infopost = new infopostModel({
body: req.body.body,
url: req.body.urls,
images: savedImages,
// tag: classified_tag
});
const savedInfopost = await infopost.save();
// Notify all students about the new infopost
const allStudents = await userModel.find({ role: ROLES_LIST.STUDENT });
const studentIds = allStudents.map(student => student.user_ID);
createNotification(1, studentIds, savedInfopost._id, req.body.body, false);



const message = "Infopost posted successfully";
await infopost.save().then((data) => {
res.json({data,message});
});
res.json({ data: savedInfopost, message });
});
} catch (err) {
res.status(400).res.json({ message: " An error occured while posting the infopost" });
res.status(400).json({ message: "An error occurred while posting the infopost" });
}
});

Expand Down Expand Up @@ -98,7 +114,7 @@ const hideinfopost = asyncHandler(async (req, res) => {
const message = `The infopost is ${pre}hidden now`;
await infopostModel
.updateOne({ _id: req.params.id }, { $set: { hidden: updatedHidden } })
.then((data) => res.json({data,message}));
.then((data) => res.json({ data, message }));
} catch (err) {
res.status(404).res.json({ message: "Error occured while hiding the infopost" });
}
Expand All @@ -119,7 +135,7 @@ const editinfopost = asyncHandler(async (req, res) => {
const message = "Successfully edited the infopost";
await infopostModel
.updateOne({ _id: req.params.id }, { $set: { body: body } })
.then((data) => res.json({data,message}));
.then((data) => res.json({ data, message }));
} catch (err) {
res.status(400).res.json({ message: "An error occured while editing the infopost" });
}
Expand Down
47 changes: 47 additions & 0 deletions api/controllers/notificationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const asyncHandler = require("express-async-handler");
const dotenv = require("dotenv");
dotenv.config();
const notificationModel = require("../models/notificationModel");
const User = require('../models/userModel');
const path = require("path");
const ROLES_LIST = require("../../config/roles_list");

//Function for creating notifications
const createNotification = asyncHandler(async (senderid, recipientlist, contentid, content, isquestion) => {
try {
const newNotification = new notificationModel({
senderid: senderid,
recipientlist: recipientlist,
contentid: contentid,
content: content,
isquestion: isquestion
});
await newNotification.save();
console.log("Notification created:", newNotification);


return newNotification;
} catch (err) {
throw new Error("An error occurred while creating notification" + err.message);
}
});


// Function to get all notifications of a particular student
const getNotificationsByStudent = async (req, res) => {
try {
const studentId = req.params.studentId;

const notifications = await notificationModel.find({ recipientlist: studentId });

res.json({ notifications });
} catch (error) {
console.error("Error retrieving notifications:", error);
res.status(500).json({ error: 'An error occurred while retrieving notifications' });
}
};

module.exports = {
createNotification,
getNotificationsByStudent
};
Loading