retrieving chats
This commit is contained in:
+40
-2
@@ -2,10 +2,11 @@ const chatRouter = require('express').Router();
|
||||
|
||||
module.exports = chatRouter;
|
||||
|
||||
const { users, chats, getUserFromDB, getChatFromDB } = require('../db');
|
||||
const { getChatFromDB, getUsersChats, addChatToDB } = require('../db');
|
||||
|
||||
chatRouter.get('/:id1/:id2', (req, res) => {
|
||||
chatRouter.get('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params;
|
||||
console.log("Request get in /chat:", id1, id2)
|
||||
|
||||
const chat = getChatFromDB(id1, id2);
|
||||
|
||||
@@ -15,3 +16,40 @@ chatRouter.get('/:id1/:id2', (req, res) => {
|
||||
res.status(404).send({message: 'Chat was not found'});
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.post('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params;
|
||||
console.log("Request post in /chat:", id1, id2)
|
||||
|
||||
const chat = getChatFromDB(id1, id2);
|
||||
|
||||
if (chat) {
|
||||
// Chat already exists
|
||||
res.status(200).send({chat});
|
||||
} else {
|
||||
// Creating new chat
|
||||
const newChat = {
|
||||
id1: id1,
|
||||
id2: id2,
|
||||
messages: []
|
||||
}
|
||||
|
||||
addChatToDB(newChat);
|
||||
|
||||
res.status(200).send({newChat});
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.get('/list/:id', (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
console.log("Request get /list in /chat:", id);
|
||||
|
||||
const userChats = getUsersChats(id);
|
||||
|
||||
if (!userChats) {
|
||||
res.status(404).send({message: 'Error with retrieving chats'});
|
||||
} else {
|
||||
res.status(200).send({chats: userChats});
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user