Merge pull request 'kazan-explore multy stub changes' (#66) from kazan-explore into master

Reviewed-on: #66
This commit is contained in:
Kamila Nazirova 2025-01-24 22:42:50 +03:00
commit a86eb0d4ef
3 changed files with 280 additions and 211 deletions

View File

@ -0,0 +1,3 @@
exports.KAZAN_EXPLORE_RESULTS_MODEL_NAME = 'KAZAN_EXPLORE_RESULTS'
exports.TOKEN_KEY = "KAZAN_EXPLORE_TOP_SECRET_TOKEN_KEY"

View File

@ -1,211 +1,250 @@
const router = require('express').Router(); const router = require('express').Router();
const { ResultsModel } = require('./model/results')
// First page
router.get('/getInfoAboutKazan', (request, response) => {
const lang = request.query.lang || 'ru'; // Получаем язык из параметров запроса router.get('/getQuizResults/:userId', async (request, response) => {
try { const { userId } = request.params;
const data = require('./json/first/info-about-kazan/success.json'); // Загружаем весь JSON
const translatedData = data[lang] || data['ru']; // Выбираем перевод по языку или дефолтный try {
response.send(translatedData); // Отправляем перевод клиенту const results = await ResultsModel.findOne({ userId : userId }).exec();
} catch (error) {
response.status(500).send({ message: 'Internal server error' }); // Ошибка в случае проблем с JSON if (!results) {
} return response.status(404).send({ message: 'Quiz results not found' });
}); }
router.get('/getNews', (request, response) => { response.send(results.items);
const lang = request.query.lang || 'ru'; } catch (error) {
try { response.status(500).send({ message: 'An error occurred while fetching quiz results' });
const data = require(`./json/first/news/${lang}/success.json`); }
response.send(data); });
} catch (error) {
response.status(404).send({ message: 'Language not found' }); router.post('/addQuizResult', async (request, response) => {
} const { userId, quizId, result } = request.body;
})
if (!userId || !quizId || !result) {
// Sport page return response.status(400).send({ message: 'Invalid input data' });
router.get('/getFirstText', (request, response) => { }
const lang = request.query.lang || 'ru'; // Получаем язык из параметров try {
try { let userResults = await ResultsModel.findOne({ userId : userId }).exec();
const data = require('./json/sport/first-text/success.json'); // Загружаем JSON if (!userResults) {
const translatedData = data[lang] || data['ru']; // Берём перевод или дефолтный userResults = new ResultsModel({ userId, items: [] });
response.send(translatedData); }
} catch (error) { userResults.items.push({ quizId, result });
response.status(404).send({ message: 'Language not found' }); // Обработка ошибки await userResults.save();
}
}); response.status(200).send({ message: 'Quiz result added successfully', data: userResults });
} catch (error) {
router.get('/getSecondText', (request, response) => { response.status(500).send({ message: 'An error occurred while adding quiz result' });
const lang = request.query.lang || 'ru'; }
try { });
const data = require('./json/sport/second-text/success.json');
const translatedData = data[lang] || data['ru'];
response.send(translatedData); // First page
} catch (error) { router.get('/getInfoAboutKazan', (request, response) => {
response.status(404).send({ message: 'Language not found' }); const lang = request.query.lang || 'ru'; // Получаем язык из параметров запроса
} try {
}); const data = require('../json/first/info-about-kazan/success.json'); // Загружаем весь JSON
const translatedData = data[lang] || data['ru']; // Выбираем перевод по языку или дефолтный
router.get('/getSportData', (request, response) => { response.send(translatedData); // Отправляем перевод клиенту
const lang = request.query.lang || 'ru'; } catch (error) {
try { response.status(500).send({ message: 'Internal server error' }); // Ошибка в случае проблем с JSON
const data = require(`./json/sport/sport-list/${lang}/success.json`); }
response.send(data); });
} catch (error) {
response.status(404).send({ message: 'Language not found' }); router.get('/getNews', (request, response) => {
} const lang = request.query.lang || 'ru';
}) try {
const data = require(`../json/first/news/${lang}/success.json`);
router.get('/getSportQuiz', (request, response) => { response.send(data);
const lang = request.query.lang || 'ru'; } catch (error) {
try { response.status(404).send({ message: 'Language not found' });
const data = require(`./json/sport/quiz/${lang}/success.json`); }
response.send(data); })
} catch (error) {
response.status(404).send({ message: 'Language not found' }); // Sport page
} router.get('/getFirstText', (request, response) => {
}) const lang = request.query.lang || 'ru'; // Получаем язык из параметров
try {
// Places page const data = require('../json/sport/first-text/success.json'); // Загружаем JSON
router.get('/getPlacesData', (request, response) => { const translatedData = data[lang] || data['ru']; // Берём перевод или дефолтный
const lang = request.query.lang || 'ru'; response.send(translatedData);
try { } catch (error) {
const data = require(`./json/places/${lang}/success.json`); response.status(404).send({ message: 'Language not found' }); // Обработка ошибки
response.send(data); }
} catch (error) { });
response.status(404).send({ message: 'Language not found' });
} router.get('/getSecondText', (request, response) => {
}) const lang = request.query.lang || 'ru';
try {
// Transport page const data = require('../json/sport/second-text/success.json');
router.get('/getInfoAboutTransportPage', (request, response) => { const translatedData = data[lang] || data['ru'];
const lang = request.query.lang || 'ru'; response.send(translatedData);
try { } catch (error) {
const data = require('./json/transport/info-about-page/success.json'); response.status(404).send({ message: 'Language not found' });
const translatedData = data[lang] || data['ru']; }
response.send(translatedData); });
} catch (error) {
response.status(404).send({ message: 'Language not found' }); router.get('/getSportData', (request, response) => {
} const lang = request.query.lang || 'ru';
}) try {
const data = require(`../json/sport/sport-list/${lang}/success.json`);
router.get('/getBus', (request, response) => { response.send(data);
response.send(require('./json/transport/bus-numbers/success.json')) } catch (error) {
}) response.status(404).send({ message: 'Language not found' });
}
router.get('/getTral', (request, response) => { })
response.send(require('./json/transport/tral-numbers/success.json'))
}) router.get('/getSportQuiz', (request, response) => {
const lang = request.query.lang || 'ru';
router.get('/getEvents', (request, response) => { try {
response.send(require('./json/transport/events-calendar/success.json')) const data = require(`../json/sport/quiz/${lang}/success.json`);
}) response.send(data);
} catch (error) {
router.get('/getTripSchedule', (request, response) => { response.status(404).send({ message: 'Language not found' });
const lang = request.query.lang || 'ru'; }
try { })
const data = require(`./json/transport/trip-schedule/${lang}/success.json`);
response.send(data); // Places page
} catch (error) { router.get('/getPlacesData', (request, response) => {
response.status(404).send({ message: 'Language not found' }); const lang = request.query.lang || 'ru';
} try {
}) const data = require(`../json/places/${lang}/success.json`);
response.send(data);
// History page } catch (error) {
router.get('/getHistoryText', (request, response) => { response.status(404).send({ message: 'Language not found' });
const lang = request.query.lang || 'ru'; }
try { })
const data = require(`./json/history/text/${lang}/success.json`);
response.send(data); // Transport page
} catch (error) { router.get('/getInfoAboutTransportPage', (request, response) => {
response.status(404).send({ message: 'Language not found' }); const lang = request.query.lang || 'ru';
} try {
}) const data = require('../json/transport/info-about-page/success.json');
router.get('/getHistoryList', (request, response) => { const translatedData = data[lang] || data['ru'];
const lang = request.query.lang || 'ru'; response.send(translatedData);
try { } catch (error) {
const data = require(`./json/history/list/${lang}/success.json`); response.status(404).send({ message: 'Language not found' });
response.send(data); }
} catch (error) { })
response.status(404).send({ message: 'Language not found' });
} router.get('/getBus', (request, response) => {
}) response.send(require('../json/transport/bus-numbers/success.json'))
})
// Education page
router.get('/getInfoAboutEducation', (request, response) => { router.get('/getTral', (request, response) => {
const lang = request.query.lang || 'ru'; response.send(require('../json/transport/tral-numbers/success.json'))
try { })
const data = require('./json/education/text/success.json');
const translatedData = data[lang] || data['ru']; router.get('/getEvents', (request, response) => {
response.send(translatedData); response.send(require('../json/transport/events-calendar/success.json'))
} catch (error) { })
response.status(404).send({ message: 'Language not found' });
} router.get('/getTripSchedule', (request, response) => {
}) const lang = request.query.lang || 'ru';
router.get('/getEducationList', (request, response) => { try {
const lang = request.query.lang || 'ru'; const data = require(`../json/transport/trip-schedule/${lang}/success.json`);
try { response.send(data);
const data = require(`./json/education/cards/${lang}/success.json`); } catch (error) {
response.send(data); response.status(404).send({ message: 'Language not found' });
} catch (error) { }
response.status(404).send({ message: 'Language not found' }); })
}
}) // History page
router.get('/getInfoAboutKFU', (request, response) => { router.get('/getHistoryText', (request, response) => {
const lang = request.query.lang || 'ru'; const lang = request.query.lang || 'ru';
try { try {
const data = require('./json/education/kfu/success.json'); const data = require(`../json/history/text/${lang}/success.json`);
const translatedData = data[lang] || data['ru']; response.send(data);
response.send(translatedData); } catch (error) {
} catch (error) { response.status(404).send({ message: 'Language not found' });
response.status(404).send({ message: 'Language not found' }); }
} })
}) router.get('/getHistoryList', (request, response) => {
const lang = request.query.lang || 'ru';
try {
// Login const data = require(`../json/history/list/${lang}/success.json`);
router.post('/entrance', (request, response) => { response.send(data);
const { email, password } = request.body.entranceData; } catch (error) {
response.status(404).send({ message: 'Language not found' });
try { }
const users = require('./json/users-information/success.json'); })
const user = users.data.find(user => user.email === email && user.password === password);
// Education page
if (!user) { router.get('/getInfoAboutEducation', (request, response) => {
return response.status(401).send('Неверные учетные данные'); const lang = request.query.lang || 'ru';
} try {
const data = require('../json/education/text/success.json');
const responseObject = { const translatedData = data[lang] || data['ru'];
email: user.email, response.send(translatedData);
} } catch (error) {
response.status(404).send({ message: 'Language not found' });
return response.json(responseObject); }
} catch (error) { })
console.error('Ошибка чтения файла:', error); router.get('/getEducationList', (request, response) => {
response.status(500).send('Внутренняя ошибка сервера'); const lang = request.query.lang || 'ru';
} try {
}) const data = require(`../json/education/cards/${lang}/success.json`);
response.send(data);
router.post('/registration', async (request, response) => { } catch (error) {
const { email, password, confirmPassword } = request.body.registerData; response.status(404).send({ message: 'Language not found' });
}
try { })
if (password !== confirmPassword) { router.get('/getInfoAboutKFU', (request, response) => {
return response.status(400).send('Пароли не совпадают!'); const lang = request.query.lang || 'ru';
} try {
const users = require('./json/users-information/success.json'); const data = require('../json/education/kfu/success.json');
const translatedData = data[lang] || data['ru'];
const existingUser = users.data.find(user => user.email === email); response.send(translatedData);
} catch (error) {
if (existingUser) { response.status(404).send({ message: 'Language not found' });
return response.status(400).send('Пользователь с такой почтой уже существует!'); }
} })
return response.json({ email: email });
} catch (error) { // Login
console.error('Ошибка регистрации пользователя:', error); router.post('/entrance', (request, response) => {
response.status(500).send('Внутренняя ошибка сервера'); const { email, password } = request.body.entranceData;
}
}); try {
const users = require('../json/users-information/success.json');
module.exports = router; const user = users.data.find(user => user.email === email && user.password === password);
if (!user) {
return response.status(401).send('Неверные учетные данные');
}
const responseObject = {
email: user.email,
}
return response.json(responseObject);
} catch (error) {
console.error('Ошибка чтения файла:', error);
response.status(500).send('Внутренняя ошибка сервера');
}
})
router.post('/registration', async (request, response) => {
const { email, password, confirmPassword } = request.body.registerData;
try {
if (password !== confirmPassword) {
return response.status(400).send('Пароли не совпадают!');
}
const users = require('../json/users-information/success.json');
const existingUser = users.data.find(user => user.email === email);
if (existingUser) {
return response.status(400).send('Пользователь с такой почтой уже существует!');
}
return response.json({ email: email });
} catch (error) {
console.error('Ошибка регистрации пользователя:', error);
response.status(500).send('Внутренняя ошибка сервера');
}
});
module.exports = router;

View File

@ -0,0 +1,27 @@
const { Schema, model } = require('mongoose')
const { KAZAN_EXPLORE_RESULTS_MODEL_NAME } = require('../const')
const schema = new Schema({
userId: { type: String },
items: [
{
quizId: { type: String },
result: { type: Number }
}
]
})
schema.set('toJSON', {
virtuals: true,
versionKey: false,
transform: function (doc, ret) {
delete ret._id
}
})
schema.virtual('id').get(function () {
return this._id.toHexString()
})
exports.ResultsModel = model(KAZAN_EXPLORE_RESULTS_MODEL_NAME, schema)