diff --git a/controllers/chatHistoryMessages 2.js b/controllers/chatHistoryMessages 2.js new file mode 100644 index 0000000..130a185 --- /dev/null +++ b/controllers/chatHistoryMessages 2.js @@ -0,0 +1,10 @@ +const chatMesssagesControler = (req, res) => { + const { chatid } = req.params; + const chatData = usersId[chatid]; + if (chatData) { + res.status(200).json(chatData); + } else { + res.status(404).send("Chat not found"); + } +}; +export default chatMesssagesControler; \ No newline at end of file diff --git a/controllers/saveMessages 2.js b/controllers/saveMessages 2.js new file mode 100644 index 0000000..7096bde --- /dev/null +++ b/controllers/saveMessages 2.js @@ -0,0 +1,34 @@ +/** + * An Express middleware function that saves a chat message to the database. + * + * @param {Object} req - The Express request object. + * @param {Object} res - The Express response object. + * + * The request object should have the following structure: + * - params: An object with the following properties: + * - senderId: The ID of the sender of the message. + * - gettingId: The ID of the recipient of the message. + * - body: An object with the following properties: + * - mas: The message to be saved. + * + * If the message is saved successfully, the function sends a response with a status code of 200 and a message of "Message saved to DB". + * If there is an error, the function sends a response with a status code of 500 and a message of "Error saving message to DB". + */ +import saveChatToDatabase from "../servises/savingMessage.js"; +import socketController from "./socketController.js"; + +const saveMessage = async (req, res) => { + const { senderId, gettingId } = req.params; + // TODO: need check if the the message is not empty + const { mas } = req.body; + try { + await saveChatToDatabase(senderId, gettingId, mas); + // Listens for new connections to the server. + // io.on("connection", socketController); + res.status(200).send("Message saved to DB"); + } catch (error) { + res.status(500).send("Error saving message to DB"); + } +}; + +export default saveMessage; diff --git a/servises/getHistoryMessages 2.js b/servises/getHistoryMessages 2.js new file mode 100644 index 0000000..9c5dffe --- /dev/null +++ b/servises/getHistoryMessages 2.js @@ -0,0 +1,20 @@ +import { chat } from "../models/chatSchema.js"; +import connectDB from "./connectDB.js"; +import mongoose from "mongoose"; + +const getChatHistory = async (user1, user2) => { + const uri = await connectDB(); + await mongoose.connect(uri); + const chatHistory = await chat + .find({ + $or: [ + { sender: user1, getting: user2 }, + { sender: user2, getting: user1 }, + ], + }) + .sort({ timestamp: 1 }); + mongoose.connection.close(); + return chatHistory; +}; + +export default getChatHistory; diff --git a/servises/savingMessage 2.js b/servises/savingMessage 2.js new file mode 100644 index 0000000..a08ad13 --- /dev/null +++ b/servises/savingMessage 2.js @@ -0,0 +1,25 @@ +import { chat } from "../models/chatSchema.js"; +import connectDB from "./connectDB.js"; +import mongoose from "mongoose"; + +const saveChatToDatabase = async (sender, recipient, content) => { + try { + const uri = await connectDB(); + await mongoose.connect(uri); + + const newChat = new chat({ + sender: sender, + getting: recipient, + content: content, + }); + + const result = await newChat.save(); + console.log("Saved new chat to MongoDB:", result); + } catch (error) { + console.error("Error connecting to MongoDB:", error); + } finally { + mongoose.connection.close(); + } +}; + +export default saveChatToDatabase; \ No newline at end of file