-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yosi Miller
authored and
Yosi Miller
committed
Dec 28, 2023
1 parent
b909cc1
commit 465914e
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |