2024-10-03 22:35:39 +03:00
|
|
|
const changeRouter = require('express').Router();
|
|
|
|
|
|
|
|
module.exports = changeRouter;
|
|
|
|
|
2024-10-12 12:23:25 +03:00
|
|
|
const { getUserFromDB, deleteUserFromDB, addUserToDB } = require('../db');
|
2024-10-03 22:35:39 +03:00
|
|
|
|
|
|
|
|
|
|
|
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)'});
|
2024-10-04 15:53:50 +03:00
|
|
|
return;
|
2024-10-03 22:35:39 +03:00
|
|
|
}
|
|
|
|
|
2024-10-04 11:21:21 +03:00
|
|
|
const updatedUser = {
|
2024-10-03 22:35:39 +03:00
|
|
|
"nickname": newNickname,
|
|
|
|
"password": user.password,
|
|
|
|
"id": user.id
|
2024-10-04 11:21:21 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Delete the old one
|
|
|
|
deleteUserFromDB(id)
|
|
|
|
|
|
|
|
// Insert updated
|
|
|
|
addUserToDB(updatedUser);
|
2024-10-03 22:35:39 +03:00
|
|
|
|
|
|
|
res.status(200).send({});
|
|
|
|
});
|
|
|
|
|
|
|
|
changeRouter.post('/password', (req, res) => {
|
|
|
|
const { id, newPassword } = req.body;
|
2024-10-04 11:21:21 +03:00
|
|
|
|
|
|
|
const user = getUserFromDB(id);
|
|
|
|
|
|
|
|
// Invalid identification
|
|
|
|
if (!user) {
|
|
|
|
res.status(401).send({message: 'Invalid credentials (id)'});
|
2024-10-04 15:53:50 +03:00
|
|
|
return;
|
2024-10-04 11:21:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the old one
|
|
|
|
deleteUserFromDB(id)
|
|
|
|
|
|
|
|
// Insert updated
|
|
|
|
const updatedUser = {
|
|
|
|
"nickname": user.nickname,
|
|
|
|
"password": newPassword,
|
|
|
|
"id": user.id
|
|
|
|
};
|
|
|
|
addUserToDB(updatedUser);
|
|
|
|
|
|
|
|
res.status(200).send({});
|
2024-10-03 22:35:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
changeRouter.delete('/:id', (req, res) => {
|
|
|
|
const { id } = req.params;
|
2024-10-04 11:21:21 +03:00
|
|
|
|
|
|
|
deleteUserFromDB(id);
|
2024-10-03 22:35:39 +03:00
|
|
|
});
|
|
|
|
|