-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
48 lines (39 loc) · 1.21 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import fs from "fs";
let users = loadUsersData();
function loadUsersData() {
try {
return JSON.parse(fs.readFileSync('users_data.json', 'utf-8'));
} catch (error) {
console.error('Error reading file:', error);
return [];
}
}
export function saveToFile(fileName) {
fs.writeFileSync(fileName, JSON.stringify(users, null, 2));
}
export function getAllUsers() {
return users;
}
export function getUser(req) {
const user = users.find((user) => user.id === Number(req.params.userId));
if (!user) console.log(`User with id ${req.params.userId} was not found`);
return user;
}
function handleBirthdate(updatedUserData) {
if (updatedUserData.birthdate) {
updatedUserData.birthdate = new Date(updatedUserData.birthdate).toLocaleDateString('ru-RU');
}
}
function updateUserData(userId, updatedUserData) {
for (const prop in updatedUserData) {
users[userId][prop] = updatedUserData[prop];
}
}
export function editUser(req) {
const userId = req.params.userId - 1;
const updatedUserData = req.body;
console.log('alive')
handleBirthdate(updatedUserData);
updateUserData(userId, updatedUserData);
saveToFile('users_data.json');
}