Merge pull request 'enterfront' (#28) from enterfront into master
Reviewed-on: #28
This commit is contained in:
commit
6fb3f3f921
@ -68,6 +68,7 @@ app.use('/kazan-explore', require('./routers/kazan-explore'))
|
||||
app.use('/epja-2023-2', require('./routers/epja-2023-2'))
|
||||
require('./routers/hub-video')
|
||||
app.use('/school-stage', require('./routers/school-stage'))
|
||||
app.use('/epja-2024-1', require('/routers/epja-2024-1'))
|
||||
|
||||
app.use(require('./error'))
|
||||
|
||||
|
73
server/routers/epja-2024-1/enterfront/auth/index.js
Normal file
73
server/routers/epja-2024-1/enterfront/auth/index.js
Normal file
@ -0,0 +1,73 @@
|
||||
const authRouter = require('express').Router();
|
||||
|
||||
// For creating tokens
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const { TOKEN_KEY } = require('../key')
|
||||
|
||||
|
||||
module.exports = authRouter;
|
||||
|
||||
const { addUserToDB, getUserFromDB } = require('../db');
|
||||
|
||||
|
||||
// Get a user by its id
|
||||
authRouter.get('/:id', (req, res) => {
|
||||
const user = getUserFromDB(req.params.id);
|
||||
|
||||
if (user) {
|
||||
res.status(200).send({user});
|
||||
} else {
|
||||
res.status(404).send({message: 'User was not found'});
|
||||
}
|
||||
})
|
||||
|
||||
// For login (authorization)
|
||||
authRouter.post('/login', (req, res) => {
|
||||
const { name, password } = req.body;
|
||||
|
||||
const user = getUserFromDB(name);
|
||||
|
||||
// Invalid identification
|
||||
if (!user) {
|
||||
res.status(401).send({message: 'Invalid credentials (id)'});
|
||||
return;
|
||||
}
|
||||
|
||||
// Invalid authentication
|
||||
if (!password || password !== user.password) {
|
||||
res.status(401).send({message: 'Invalid credentials (password)'});
|
||||
return;
|
||||
}
|
||||
|
||||
// Now, authorization
|
||||
const token = jwt.sign({id: name}, TOKEN_KEY, {
|
||||
expiresIn: '1h'
|
||||
})
|
||||
|
||||
res.status(200).send({token});
|
||||
})
|
||||
|
||||
|
||||
authRouter.post('/reg', (req, res) => {
|
||||
const { name, password, nickname } = req.body;
|
||||
|
||||
const user = getUserFromDB(name);
|
||||
|
||||
// Invalid identification
|
||||
if (user) {
|
||||
res.status(409).send({message: 'Such id already exists'});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!name || !password || !nickname) {
|
||||
res.status(401).send({message: 'Empty or invalid fields'});
|
||||
return;
|
||||
}
|
||||
|
||||
// Add to 'DB'
|
||||
const newUser = {id: name, password: password, nickname: nickname};
|
||||
addUserToDB(newUser)
|
||||
|
||||
res.status(200).send({user: newUser});
|
||||
})
|
52
server/routers/epja-2024-1/enterfront/auth/users.json
Normal file
52
server/routers/epja-2024-1/enterfront/auth/users.json
Normal file
@ -0,0 +1,52 @@
|
||||
[
|
||||
{
|
||||
"nickname": "Alice Johnson",
|
||||
"password": "1234",
|
||||
"id": "alice"
|
||||
},
|
||||
{
|
||||
"nickname": "Bob Smith",
|
||||
"password": "1234",
|
||||
"id": "bobsm"
|
||||
},
|
||||
{
|
||||
"nickname": "Charlie Brown",
|
||||
"password": "1234",
|
||||
"id": "charl"
|
||||
},
|
||||
{
|
||||
"nickname": "David Clark",
|
||||
"password": "1234",
|
||||
"id": "david"
|
||||
},
|
||||
{
|
||||
"nickname": "Eve Adams",
|
||||
"password": "1234",
|
||||
"id": "evead"
|
||||
},
|
||||
{
|
||||
"nickname": "Frank Wright",
|
||||
"password": "1234",
|
||||
"id": "frank"
|
||||
},
|
||||
{
|
||||
"nickname": "Grace Lee",
|
||||
"password": "1234",
|
||||
"id": "grace"
|
||||
},
|
||||
{
|
||||
"nickname": "Hannah Scott",
|
||||
"password": "1234",
|
||||
"id": "hanna"
|
||||
},
|
||||
{
|
||||
"nickname": "Ian Davis",
|
||||
"password": "1234",
|
||||
"id": "ianda"
|
||||
},
|
||||
{
|
||||
"nickname": "Jill Thompson",
|
||||
"password": "1234",
|
||||
"id": "jillt"
|
||||
}
|
||||
]
|
64
server/routers/epja-2024-1/enterfront/change/index.js
Normal file
64
server/routers/epja-2024-1/enterfront/change/index.js
Normal file
@ -0,0 +1,64 @@
|
||||
const changeRouter = require('express').Router();
|
||||
|
||||
module.exports = changeRouter;
|
||||
|
||||
const { getUserFromDB, deleteUserFromDB, addUserToDB } = require('../db');
|
||||
|
||||
|
||||
changeRouter.post('/nickname', (req, res) => {
|
||||
const { id, newNickname } = req.body;
|
||||
|
||||
const user = getUserFromDB(id);
|
||||
|
||||
// Invalid identification
|
||||
if (!user) {
|
||||
res.status(401).send({message: 'Invalid credentials (id)'});
|
||||
return;
|
||||
}
|
||||
|
||||
const updatedUser = {
|
||||
"nickname": newNickname,
|
||||
"password": user.password,
|
||||
"id": user.id
|
||||
};
|
||||
|
||||
// Delete the old one
|
||||
deleteUserFromDB(id)
|
||||
|
||||
// Insert updated
|
||||
addUserToDB(updatedUser);
|
||||
|
||||
res.status(200).send({});
|
||||
});
|
||||
|
||||
changeRouter.post('/password', (req, res) => {
|
||||
const { id, newPassword } = req.body;
|
||||
|
||||
const user = getUserFromDB(id);
|
||||
|
||||
// Invalid identification
|
||||
if (!user) {
|
||||
res.status(401).send({message: 'Invalid credentials (id)'});
|
||||
return;
|
||||
}
|
||||
|
||||
// Delete the old one
|
||||
deleteUserFromDB(id)
|
||||
|
||||
// Insert updated
|
||||
const updatedUser = {
|
||||
"nickname": user.nickname,
|
||||
"password": newPassword,
|
||||
"id": user.id
|
||||
};
|
||||
addUserToDB(updatedUser);
|
||||
|
||||
res.status(200).send({});
|
||||
});
|
||||
|
||||
changeRouter.delete('/:id', (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
deleteUserFromDB(id);
|
||||
});
|
||||
|
662
server/routers/epja-2024-1/enterfront/chat/chats.json
Normal file
662
server/routers/epja-2024-1/enterfront/chat/chats.json
Normal file
@ -0,0 +1,662 @@
|
||||
[
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "bobsm",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hello Bob!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 07:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Hey Alice, how are you?",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:05:00"
|
||||
},
|
||||
{
|
||||
"data": "I'm good, thanks for asking.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 07:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear!",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "charl",
|
||||
"messages": [
|
||||
{
|
||||
"data": "How's the project going?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 07:20:00"
|
||||
},
|
||||
{
|
||||
"data": "It's coming along, almost done!",
|
||||
"senderId": "charl",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:25:00"
|
||||
},
|
||||
{
|
||||
"data": "That's great to hear!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 07:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks for checking in.",
|
||||
"senderId": "charl",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "david",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Did you get the files?",
|
||||
"senderId": "david",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I did. Thank you!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 07:45:00"
|
||||
},
|
||||
{
|
||||
"data": "You're welcome.",
|
||||
"senderId": "david",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 07:50:00"
|
||||
},
|
||||
{
|
||||
"data": "Let me know if you need anything else.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 07:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "frank",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Can you review this document for me?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 08:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Sure, I'll take a look.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, much appreciated!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 08:30:00"
|
||||
},
|
||||
{
|
||||
"data": "No problem.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "grace",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hey Grace, let's meet up for coffee!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 08:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Sounds good, when are you free?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:45:00"
|
||||
},
|
||||
{
|
||||
"data": "How about tomorrow afternoon?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 08:50:00"
|
||||
},
|
||||
{
|
||||
"data": "Works for me!",
|
||||
"senderId": "grace",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 08:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "hanna",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hannah, do you have a moment?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 09:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Sure, what's up?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Just wanted to check on the report.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 09:10:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll send it soon.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "ianda",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Ian, have you completed the review?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 09:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I sent my feedback.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks for that.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 09:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Anytime!",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "jillt",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Jill, let's schedule a catch-up meeting.",
|
||||
"senderId": "alice",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 09:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Sounds good, when works for you?",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Tomorrow afternoon?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 09:50:00"
|
||||
},
|
||||
{
|
||||
"data": "That works for me!",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 09:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "alice",
|
||||
"id2": "evead",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Eve, did you send the schedule?",
|
||||
"senderId": "alice",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 10:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, just sent it.",
|
||||
"senderId": "evead",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 10:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, much appreciated!",
|
||||
"senderId": "alice",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 10:10:00"
|
||||
},
|
||||
{
|
||||
"data": "No problem!",
|
||||
"senderId": "evead",
|
||||
"recipientId": "alice",
|
||||
"timestamp": "09.10.2024 10:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "bobsm",
|
||||
"id2": "charl",
|
||||
"messages": [
|
||||
{
|
||||
"data": "How's everything going?",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 10:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Pretty good, how about you?",
|
||||
"senderId": "charl",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Can't complain!",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 10:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear that.",
|
||||
"senderId": "charl",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "bobsm",
|
||||
"id2": "david",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Can you send the report?",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 10:40:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll send it in an hour.",
|
||||
"senderId": "david",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Perfect, thanks.",
|
||||
"senderId": "bobsm",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 10:50:00"
|
||||
},
|
||||
{
|
||||
"data": "No problem.",
|
||||
"senderId": "david",
|
||||
"recipientId": "bobsm",
|
||||
"timestamp": "09.10.2024 10:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "charl",
|
||||
"id2": "evead",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Hey Eve, how's it going?",
|
||||
"senderId": "charl",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 11:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Good, how about you?",
|
||||
"senderId": "evead",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Can't complain!",
|
||||
"senderId": "charl",
|
||||
"recipientId": "evead",
|
||||
"timestamp": "09.10.2024 11:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear.",
|
||||
"senderId": "evead",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "charl",
|
||||
"id2": "frank",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Do you have time to talk today?",
|
||||
"senderId": "charl",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:20:00"
|
||||
},
|
||||
{
|
||||
"data": "I have a meeting, but I can chat afterward.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Sounds good.",
|
||||
"senderId": "charl",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:30:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll message you after.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "charl",
|
||||
"timestamp": "09.10.2024 11:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "david",
|
||||
"id2": "frank",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Did you review the document?",
|
||||
"senderId": "david",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, it's all good.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 11:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Great, thanks for the quick turnaround!",
|
||||
"senderId": "david",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 11:50:00"
|
||||
},
|
||||
{
|
||||
"data": "No worries!",
|
||||
"senderId": "frank",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 11:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "david",
|
||||
"id2": "grace",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Grace, can you send the updated schedule?",
|
||||
"senderId": "david",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I'll send it in a few minutes.",
|
||||
"senderId": "grace",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 12:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, much appreciated!",
|
||||
"senderId": "david",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:10:00"
|
||||
},
|
||||
{
|
||||
"data": "You're welcome!",
|
||||
"senderId": "grace",
|
||||
"recipientId": "david",
|
||||
"timestamp": "09.10.2024 12:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "frank",
|
||||
"id2": "grace",
|
||||
"messages": [
|
||||
{
|
||||
"data": "How are you today?",
|
||||
"senderId": "frank",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:20:00"
|
||||
},
|
||||
{
|
||||
"data": "I'm doing well, thanks for asking.",
|
||||
"senderId": "grace",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Glad to hear that.",
|
||||
"senderId": "frank",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 12:30:00"
|
||||
},
|
||||
{
|
||||
"data": "How about you?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "frank",
|
||||
"id2": "hanna",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Did you attend the meeting?",
|
||||
"senderId": "frank",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 12:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, it was productive.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Good to hear!",
|
||||
"senderId": "frank",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 12:50:00"
|
||||
},
|
||||
{
|
||||
"data": "Indeed, lots to follow up on.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "frank",
|
||||
"timestamp": "09.10.2024 12:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "grace",
|
||||
"id2": "hanna",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Can we meet later today?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Sure, what's a good time?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:05:00"
|
||||
},
|
||||
{
|
||||
"data": "How about 3?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Works for me.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "grace",
|
||||
"id2": "ianda",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Ian, did you get the message I sent?",
|
||||
"senderId": "grace",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I'll respond soon.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:25:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, appreciate it!",
|
||||
"senderId": "grace",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:30:00"
|
||||
},
|
||||
{
|
||||
"data": "You're welcome!",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "grace",
|
||||
"timestamp": "09.10.2024 01:35:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "hanna",
|
||||
"id2": "ianda",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Ian, do you have a minute?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:40:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, what do you need?",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:45:00"
|
||||
},
|
||||
{
|
||||
"data": "Just a quick update on the project.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 01:50:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll email you the details.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 01:55:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "hanna",
|
||||
"id2": "jillt",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Jill, can we talk tomorrow?",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:00:00"
|
||||
},
|
||||
{
|
||||
"data": "Yes, I'm free after 2.",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 02:05:00"
|
||||
},
|
||||
{
|
||||
"data": "Perfect, see you then.",
|
||||
"senderId": "hanna",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:10:00"
|
||||
},
|
||||
{
|
||||
"data": "Looking forward to it.",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "hanna",
|
||||
"timestamp": "09.10.2024 02:15:00"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"id1": "ianda",
|
||||
"id2": "jillt",
|
||||
"messages": [
|
||||
{
|
||||
"data": "Jill, I have the files you requested.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:20:00"
|
||||
},
|
||||
{
|
||||
"data": "Thanks, please send them over.",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 02:25:00"
|
||||
},
|
||||
{
|
||||
"data": "I'll send them right now.",
|
||||
"senderId": "ianda",
|
||||
"recipientId": "jillt",
|
||||
"timestamp": "09.10.2024 02:30:00"
|
||||
},
|
||||
{
|
||||
"data": "Great, thanks again!",
|
||||
"senderId": "jillt",
|
||||
"recipientId": "ianda",
|
||||
"timestamp": "09.10.2024 02:35:00"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
86
server/routers/epja-2024-1/enterfront/chat/index.js
Normal file
86
server/routers/epja-2024-1/enterfront/chat/index.js
Normal file
@ -0,0 +1,86 @@
|
||||
const chatRouter = require('express').Router();
|
||||
|
||||
module.exports = chatRouter;
|
||||
|
||||
const { getChatFromDB, getUsersChats, addChatToDB, getUserFromDB,
|
||||
addMessageToChat} = require('../db');
|
||||
|
||||
chatRouter.get('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params;
|
||||
|
||||
if (id1 === id2) {
|
||||
res.status(400).send({message: 'Ids should be different'});
|
||||
return;
|
||||
}
|
||||
|
||||
const chat = getChatFromDB(id1, id2);
|
||||
|
||||
if (chat) {
|
||||
res.status(200).send({chat});
|
||||
} else {
|
||||
res.status(404).send({message: 'Chat was not found'});
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.post('/item/:id1/:id2', (req, res) => {
|
||||
const { id1, id2 } = req.params;
|
||||
|
||||
if (id1 === id2) {
|
||||
res.status(400).send({message: 'Ids should be different'});
|
||||
return;
|
||||
}
|
||||
|
||||
const chat = getChatFromDB(id1, id2);
|
||||
|
||||
if (chat) {
|
||||
// Chat already exists
|
||||
res.status(200).send({chat});
|
||||
} else {
|
||||
if (!getUserFromDB(id1) || !getUserFromDB(id2)) {
|
||||
res.status(404).send({message: 'Such interlocutor does not exist'});
|
||||
} 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;
|
||||
|
||||
const userChats = getUsersChats(id);
|
||||
|
||||
if (!userChats) {
|
||||
res.status(404).send({message: 'Error with retrieving chats'});
|
||||
} else {
|
||||
res.status(200).send({chats: userChats});
|
||||
}
|
||||
})
|
||||
|
||||
chatRouter.post('/message/:sender/:receiver', (req, res) => {
|
||||
const { sender, receiver } = req.params;
|
||||
const { message } = req.body;
|
||||
|
||||
const chat = getChatFromDB(sender, receiver);
|
||||
|
||||
if (!chat) {
|
||||
// Chat already exists
|
||||
res.status(400).send({message: "Such chat does not exist"});
|
||||
} else {
|
||||
if (!getUserFromDB(sender) || !getUserFromDB(receiver)) {
|
||||
res.status(404).send({message: 'Such people do not exist'});
|
||||
} else {
|
||||
// Add new message
|
||||
addMessageToChat(chat, message);
|
||||
res.status(200).send({});
|
||||
}
|
||||
}
|
||||
})
|
74
server/routers/epja-2024-1/enterfront/db.js
Normal file
74
server/routers/epja-2024-1/enterfront/db.js
Normal file
@ -0,0 +1,74 @@
|
||||
// Read already defined users (pseudo-DB)
|
||||
const users = require('./auth/users.json');
|
||||
const chats = require('./chat/chats.json');
|
||||
|
||||
const getUserFromDB = (userID) => {
|
||||
if (!userID) {return false;}
|
||||
|
||||
// Accessing 'DB'
|
||||
const user = users.find((user) => user.id === userID);
|
||||
|
||||
if (user) {
|
||||
return user;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const deleteUserFromDB = (userID) => {
|
||||
const index = users.findIndex(item => item.id === userID);
|
||||
if (index !== -1) {
|
||||
users.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
const addUserToDB = (user) => {
|
||||
users.push(user);
|
||||
}
|
||||
|
||||
const getChatFromDB = (firstID, secondID) => {
|
||||
if (!firstID || !secondID) {return false;}
|
||||
|
||||
// Accessing 'DB'
|
||||
const chat = chats.find((item) =>
|
||||
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID));
|
||||
|
||||
if (chat) {
|
||||
return chat;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const getUsersChats = (userID) => {
|
||||
if (!userID) {return false;}
|
||||
|
||||
const userChats = chats.filter((chat) => (chat.id1 === userID || chat.id2 === userID));
|
||||
|
||||
if (userChats) {
|
||||
return userChats;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const addMessageToChat = (chat, msg) => {
|
||||
chat.messages.push(msg);
|
||||
}
|
||||
|
||||
const deleteChatFromDB = (firstID, secondID) => {
|
||||
const index = chats.findIndex(item =>
|
||||
(item.id1 === firstID && item.id2 === secondID) || (item.id1 === secondID && item.id2 === firstID));
|
||||
|
||||
if (index !== -1) {
|
||||
chats.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
const addChatToDB = (chat) => {
|
||||
chats.push(chat);
|
||||
}
|
||||
|
||||
|
||||
module.exports = {users, chats, getUserFromDB, getChatFromDB, addUserToDB,
|
||||
deleteUserFromDB, addChatToDB, deleteChatFromDB, getUsersChats, addMessageToChat}
|
17
server/routers/epja-2024-1/enterfront/index.js
Normal file
17
server/routers/epja-2024-1/enterfront/index.js
Normal file
@ -0,0 +1,17 @@
|
||||
const changeRouter = require("./change");
|
||||
const authRouter = require("./auth");
|
||||
const chatRouter = require("./chat");
|
||||
|
||||
const router = require('express').Router();
|
||||
|
||||
const delay = require('./middlewares/delay');
|
||||
const verify = require('./middlewares/verify');
|
||||
|
||||
module.exports = router;
|
||||
|
||||
// router.use(delay(300));
|
||||
// router.use('/books', delay, booksRouter);
|
||||
|
||||
router.use('/auth', authRouter);
|
||||
router.use('/change', verify, changeRouter);
|
||||
router.use('/chat', verify, chatRouter)
|
3
server/routers/epja-2024-1/enterfront/key.js
Normal file
3
server/routers/epja-2024-1/enterfront/key.js
Normal file
@ -0,0 +1,3 @@
|
||||
const TOKEN_KEY = '5frv12e4few3r';
|
||||
|
||||
module.exports = { TOKEN_KEY }
|
@ -0,0 +1,5 @@
|
||||
const delay = (ms = 1000) => (req, res, next) => {
|
||||
setTimeout(next, ms)
|
||||
}
|
||||
|
||||
module.exports = delay
|
22
server/routers/epja-2024-1/enterfront/middlewares/verify.js
Normal file
22
server/routers/epja-2024-1/enterfront/middlewares/verify.js
Normal file
@ -0,0 +1,22 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const { TOKEN_KEY } = require('../key')
|
||||
|
||||
function verifyToken(req, res, next) {
|
||||
const token = req.headers['authorization']?.split(' ')[1];
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).send({ message: 'No token provided' });
|
||||
}
|
||||
|
||||
// Verify token
|
||||
jwt.verify(token, TOKEN_KEY, (err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(401).send({ message: 'Unauthorized' });
|
||||
}
|
||||
|
||||
next(); // Proceed to the next middleware or route
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = verifyToken;
|
7
server/routers/epja-2024-1/index.js
Normal file
7
server/routers/epja-2024-1/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
const express = require('express')
|
||||
const router = express.Router()
|
||||
|
||||
|
||||
router.use('/enterfront', require('./enterfront/index'))
|
||||
|
||||
module.exports = router
|
Loading…
Reference in New Issue
Block a user