multy-stub/server/routers/epja-2024-1/enterfront/chat/index.js

87 lines
2.1 KiB
JavaScript
Raw Normal View History

2024-10-16 11:06:23 +03:00
const chatRouter = require('express').Router()
2024-10-10 12:02:52 +03:00
2024-10-16 11:06:23 +03:00
module.exports = chatRouter
2024-10-10 12:02:52 +03:00
const { getChatFromDB, getUsersChats, addChatToDB, getUserFromDB,
2024-10-16 11:06:23 +03:00
addMessageToChat} = require('../db')
2024-10-10 12:02:52 +03:00
chatRouter.get('/item/:id1/:id2', (req, res) => {
2024-10-16 11:06:23 +03:00
const { id1, id2 } = req.params
2024-10-10 12:02:52 +03:00
if (id1 === id2) {
2024-10-16 11:06:23 +03:00
res.status(400).send({message: 'Ids should be different'})
return
2024-10-10 12:02:52 +03:00
}
2024-10-16 11:06:23 +03:00
const chat = getChatFromDB(id1, id2)
2024-10-10 12:02:52 +03:00
if (chat) {
2024-10-16 11:06:23 +03:00
res.status(200).send({chat})
2024-10-10 12:02:52 +03:00
} else {
2024-10-16 11:06:23 +03:00
res.status(404).send({message: 'Chat was not found'})
2024-10-10 12:02:52 +03:00
}
})
chatRouter.post('/item/:id1/:id2', (req, res) => {
2024-10-16 11:06:23 +03:00
const { id1, id2 } = req.params
2024-10-10 12:02:52 +03:00
if (id1 === id2) {
2024-10-16 11:06:23 +03:00
res.status(400).send({message: 'Ids should be different'})
return
2024-10-10 12:02:52 +03:00
}
2024-10-16 11:06:23 +03:00
const chat = getChatFromDB(id1, id2)
2024-10-10 12:02:52 +03:00
if (chat) {
// Chat already exists
2024-10-16 11:06:23 +03:00
res.status(200).send({chat})
2024-10-10 12:02:52 +03:00
} else {
if (!getUserFromDB(id1) || !getUserFromDB(id2)) {
2024-10-16 11:06:23 +03:00
res.status(404).send({message: 'Such interlocutor does not exist'})
2024-10-10 12:02:52 +03:00
} else {
// Creating new chat
const newChat = {
id1: id1,
id2: id2,
messages: []
}
2024-10-16 11:06:23 +03:00
addChatToDB(newChat)
2024-10-10 12:02:52 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({newChat})
2024-10-10 12:02:52 +03:00
}
}
})
chatRouter.get('/list/:id', (req, res) => {
2024-10-16 11:06:23 +03:00
const { id } = req.params
2024-10-10 12:02:52 +03:00
2024-10-16 11:06:23 +03:00
const userChats = getUsersChats(id)
2024-10-10 12:02:52 +03:00
if (!userChats) {
2024-10-16 11:06:23 +03:00
res.status(404).send({message: 'Error with retrieving chats'})
2024-10-10 12:02:52 +03:00
} else {
2024-10-16 11:06:23 +03:00
res.status(200).send({chats: userChats})
2024-10-10 12:02:52 +03:00
}
})
chatRouter.post('/message/:sender/:receiver', (req, res) => {
2024-10-16 11:06:23 +03:00
const { sender, receiver } = req.params
const { message } = req.body
2024-10-10 12:02:52 +03:00
2024-10-16 11:06:23 +03:00
const chat = getChatFromDB(sender, receiver)
2024-10-10 12:02:52 +03:00
if (!chat) {
// Chat already exists
2024-10-16 11:06:23 +03:00
res.status(400).send({message: "Such chat does not exist"})
2024-10-10 12:02:52 +03:00
} else {
if (!getUserFromDB(sender) || !getUserFromDB(receiver)) {
2024-10-16 11:06:23 +03:00
res.status(404).send({message: 'Such people do not exist'})
2024-10-10 12:02:52 +03:00
} else {
// Add new message
2024-10-16 11:06:23 +03:00
addMessageToChat(chat, message)
res.status(200).send({})
2024-10-10 12:02:52 +03:00
}
}
})