retrieving chats

This commit is contained in:
Nikolai Petukhov
2024-10-04 11:21:21 +03:00
parent 073c61977f
commit d1e824ab77
11 changed files with 338 additions and 88 deletions

View File

@@ -65,6 +65,160 @@
}
]
},
{
"id1": "alice",
"id2": "evead",
"messages": [
{
"data": "Eve, do you have the meeting details?",
"senderId": "alice"
},
{
"data": "Yes, I just sent them to you.",
"senderId": "evead"
},
{
"data": "Got it, thanks!",
"senderId": "alice"
},
{
"data": "You're welcome.",
"senderId": "evead"
}
]
},
{
"id1": "alice",
"id2": "frank",
"messages": [
{
"data": "Can you review this document for me?",
"senderId": "alice"
},
{
"data": "Sure, I'll take a look.",
"senderId": "frank"
},
{
"data": "Thanks, much appreciated!",
"senderId": "alice"
},
{
"data": "No problem.",
"senderId": "frank"
}
]
},
{
"id1": "alice",
"id2": "grace",
"messages": [
{
"data": "Hey Grace, let's meet up for coffee!",
"senderId": "alice"
},
{
"data": "Sounds good, when are you free?",
"senderId": "grace"
},
{
"data": "How about tomorrow afternoon?",
"senderId": "alice"
},
{
"data": "Works for me!",
"senderId": "grace"
}
]
},
{
"id1": "alice",
"id2": "hanna",
"messages": [
{
"data": "Hannah, do you have a moment?",
"senderId": "alice"
},
{
"data": "Sure, what's up?",
"senderId": "hanna"
},
{
"data": "Just wanted to check on the report.",
"senderId": "alice"
},
{
"data": "I'll send it soon.",
"senderId": "hanna"
}
]
},
{
"id1": "alice",
"id2": "ianda",
"messages": [
{
"data": "Ian, have you completed the review?",
"senderId": "alice"
},
{
"data": "Yes, I sent my feedback.",
"senderId": "ianda"
},
{
"data": "Thanks for that.",
"senderId": "alice"
},
{
"data": "Anytime!",
"senderId": "ianda"
}
]
},
{
"id1": "alice",
"id2": "jillt",
"messages": [
{
"data": "Jill, let's schedule a catch-up meeting.",
"senderId": "alice"
},
{
"data": "Sounds good, when works for you?",
"senderId": "jillt"
},
{
"data": "Tomorrow afternoon?",
"senderId": "alice"
},
{
"data": "That works for me!",
"senderId": "jillt"
}
]
},
{
"id1": "alice",
"id2": "evead",
"messages": [
{
"data": "Eve, did you send the schedule?",
"senderId": "alice"
},
{
"data": "Yes, just sent it.",
"senderId": "evead"
},
{
"data": "Thanks, much appreciated!",
"senderId": "alice"
},
{
"data": "No problem!",
"senderId": "evead"
}
]
},
{
"id1": "bobsm",
"id2": "charl",

View File

@@ -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});
}
})