Skip to content

Commit

Permalink
add nev virsoin
Browse files Browse the repository at this point in the history
  • Loading branch information
Yosi Miller authored and Yosi Miller committed Dec 28, 2023
1 parent b909cc1 commit 465914e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
10 changes: 10 additions & 0 deletions controllers/chatHistoryMessages 2.js
Original file line number Diff line number Diff line change
@@ -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;
34 changes: 34 additions & 0 deletions controllers/saveMessages 2.js
Original file line number Diff line number Diff line change
@@ -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;
20 changes: 20 additions & 0 deletions servises/getHistoryMessages 2.js
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions servises/savingMessage 2.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 465914e

Please sign in to comment.