Merge pull request 'small fixes in kfu-m-24-1/eng-it-lean' (#88) from kfu-m-24-1/eng-it-lean into master

Reviewed-on: #88
This commit is contained in:
nekitboy1998 2025-02-07 12:26:56 +03:00
commit 11b1d670d0
3 changed files with 15 additions and 28 deletions

View File

@ -1,25 +1,24 @@
[ [
{ "id": 1, "description": "1000 часто используемых", "imageFilename": "kart1.jpg", "words": [0, 1] },
{ {
"id": 2, "id": 1,
"description": "10 слов в Data Science", "description": "10 слов в Data Science",
"imageFilename": "kart1.jpg", "imageFilename": "kart1.jpg",
"words": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] "words": [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
}, },
{ {
"id": 3, "id": 2,
"description": "Job Interview", "description": "Job Interview",
"imageFilename": "kart1.jpg", "imageFilename": "kart1.jpg",
"words": [13, 14, 15, 16, 17, 18, 19, 20, 21, 22] "words": [13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
}, },
{ {
"id": 4, "id": 3,
"description": "ReactJS", "description": "ReactJS",
"imageFilename": "kart1.jpg", "imageFilename": "kart1.jpg",
"words": [23, 24, 25, 26, 27, 28, 29, 30, 31, 32] "words": [23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
}, },
{ {
"id": 5, "id": 4,
"description": "NodeJS", "description": "NodeJS",
"imageFilename": "kart1.jpg", "imageFilename": "kart1.jpg",
"words": [33, 34, 35, 36, 37, 38, 39, 40, 41, 42] "words": [33, 34, 35, 36, 37, 38, 39, 40, 41, 42]

View File

@ -5,15 +5,16 @@ const router = require('express').Router();
module.exports = router; module.exports = router;
const data = require('./units.json'); const data = require('./units.json');
const users = require('../users/users.json');
router.get('/', (req, res) => { router.get('/', (req, res) => {
// for every data set author from users and save it to authoredData variable // for every data set author from users and save it to authoredData variable
const users = require('../users/users.json');
const authoredData = data.map((unit) => { const authoredData = data.map((unit) => {
const user = users.find((user) => user.public_id == unit.author); const user = users.find((user) => user.public_id == unit.author);
let authoredUnit = undefined;
if (user) { if (user) {
unit.author = user; authoredUnit = { ...unit, author: user };
} }
return unit; return authoredUnit;
}); });
res.send(authoredData); res.send(authoredData);
@ -39,9 +40,8 @@ router.post('/:id', (req, res) => {
data.splice(index, 1); data.splice(index, 1);
data.push(updatedUnit); data.push({...updatedUnit, author: updatedUnit.author.public_id});
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
res.status(200).send(data); res.status(200).send(data);
}); });
@ -61,12 +61,8 @@ router.put('/', (req, res) => {
} }
const newId = data.length + 1; const newId = data.length + 1;
// const filename = newUnit.name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(); data.push({ ...newUnit, id: newId });
// fs.writeFileSync(path.join(__dirname, 'data', `${filename}.md`), newUnit.content);
data.push({ ...unit, id: newId });
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
res.status(200).send(data); res.status(200).send(data);
}); });
@ -79,11 +75,11 @@ router.delete('/:id', (req, res) => {
} }
data.splice(index, 1); data.splice(index, 1);
fs.writeFileSync(path.join(__dirname, 'units.json'), JSON.stringify(data));
res.send({ message: `Unit with ID ${id} deleted` }); res.send({ message: `Unit with ID ${id} deleted` });
}); });
router.get('/:id', (req, res) => { router.get('/:id', (req, res) => {
const users = require('../users/users.json');
const id = parseInt(req.params.id); const id = parseInt(req.params.id);
const unit = data.find((unit) => unit.id === id); const unit = data.find((unit) => unit.id === id);

View File

@ -3,7 +3,7 @@ const fs = require('fs');
module.exports = router; module.exports = router;
const data = require('./users.json'); let data = require('./users.json');
const path = require('path'); const path = require('path');
router.get('/', (req, res) => { router.get('/', (req, res) => {
res.send(data); res.send(data);
@ -11,20 +11,15 @@ router.get('/', (req, res) => {
router.post('/', (req, res) => { router.post('/', (req, res) => {
const newUser = req.body; const newUser = req.body;
const updatedData = [...data, newUser];
console.log(updatedData); data.push(newUser);
fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(updatedData)); fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(data));
res.send(data);
res.send(updatedData);
}); });
router.post('/login', (req, res) => { router.post('/login', (req, res) => {
const { email, password } = req.body; const { email, password } = req.body;
console.log(email);
console.log(req.body);
const user = data.find((user) => user.email === email && user.password === password); const user = data.find((user) => user.email === email && user.password === password);
console.log(user);
if (!user) { if (!user) {
res.status(404).send('Пользователь не найден'); res.status(404).send('Пользователь не найден');
@ -34,13 +29,11 @@ router.post('/login', (req, res) => {
router.get('/account', (req, res) => { router.get('/account', (req, res) => {
const { public_id } = req.query; const { public_id } = req.query;
console.log(public_id);
const user = data.find((user) => user.public_id == public_id); const user = data.find((user) => user.public_id == public_id);
if (!user) { if (!user) {
res.status(404).send('Пользователь не найден'); res.status(404).send('Пользователь не найден');
} }
console.log(user);
res.send({ ...user, id: -1 }); res.send({ ...user, id: -1 });
}); });
@ -54,7 +47,6 @@ router.post('/account/save', (req, res) => {
} }
data[index] = { ...data[index], ...updatedUser, id: data[index].id, password: data[index].password }; data[index] = { ...data[index], ...updatedUser, id: data[index].id, password: data[index].password };
fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(data)); fs.writeFileSync(path.join(__dirname, 'users.json'), JSON.stringify(data));
res.status(200); res.status(200);