From 7c3132c54b281573052b13a3df3f86d6dce5dd0e Mon Sep 17 00:00:00 2001 From: Deolys Date: Wed, 22 May 2024 15:11:46 +0300 Subject: [PATCH] webstar-project stubs api --- server/index.js | 1 + server/routers/webstar-project/index.js | 136 ++++++++++++++ .../json/cards-data/success.json | 172 ++++++++++++++++++ .../json/messages/success.json | 149 +++++++++++++++ .../json/users-data/success.json | 50 +++++ .../middleware/auth-middleware.js | 14 ++ 6 files changed, 522 insertions(+) create mode 100644 server/routers/webstar-project/index.js create mode 100644 server/routers/webstar-project/json/cards-data/success.json create mode 100644 server/routers/webstar-project/json/messages/success.json create mode 100644 server/routers/webstar-project/json/users-data/success.json create mode 100644 server/routers/webstar-project/middleware/auth-middleware.js diff --git a/server/index.js b/server/index.js index 41e79f0..c09b5ab 100644 --- a/server/index.js +++ b/server/index.js @@ -57,6 +57,7 @@ app.use('/example', require('./routers/example')) app.use('/zoom-bar', require('./routers/zoom-bar')) app.use('/r-and-m', require('./routers/r-and-m')) app.use('/edateam', require('./routers/edateam')) +app.use('/webstar-project', require('./routers/webstar-project')) //app.use('/basket', require('./routers/basket')) //app.use('/easy-project', require('./routers/easy-project')) //app.use('/sugarbun', require('./routers/sugarbun')) diff --git a/server/routers/webstar-project/index.js b/server/routers/webstar-project/index.js new file mode 100644 index 0000000..711ad2b --- /dev/null +++ b/server/routers/webstar-project/index.js @@ -0,0 +1,136 @@ +const router = require('express').Router(); +const authMiddleware = require('./middleware/auth-middleware'); + +router.get('/cards-data', (request, response) => { + response.send(require('./json/cards-data/success.json')) +}) + +router.get('/messages', (request, response) => { + response.send(require('./json/messages/success.json')) +}) + +router.get('/user', authMiddleware, (request, response) => { + const email = request.userEmail; + + try { + const usersData = require('./json/users-data/success.json'); + const userData = usersData.data.find(user => user.email === email); + + if (userData) { + return response.json(userData.favourites); + } + response.status(404).send('User not found'); + + } catch (error) { + console.error('Error reading file:', error); + response.status(500).send('Internal server error'); + } +}) + +router.get('/cards-data/:id', (request, response) => { + const cardId = request.params.id; + + try { + const cardsData = require('./json/cards-data/success.json'); + const cardData = cardsData.data.find(card => card.id === cardId); + + if (cardData) { + return response.json(cardData); + } + response.status(404).send('Card not found'); + + } catch (error) { + console.error('Error reading file:', error); + response.status(500).send('Internal server error'); + } +}) + +router.get('/messages/:id', (request, response) => { + const cardId = request.params.id; + + try { + const cardsData = require('./json/messages/success.json'); + const cardData = cardsData.data.find(card => card.id === cardId); + + if (cardData) { + return response.json(cardData); + } + response.status(404).send('Card not found'); + + } catch (error) { + console.error('Error reading file:', error); + response.status(500).send('Internal server error'); + } +}) + +router.post('/login', (request, response) => { + const { email, password } = request.body.loginData; + + try { + const users = require('./json/users-data/success.json'); + const user = users.data.find(user => user.email === email && user.password === password); + + if (!user) { + response.status(401).send('Invalid credentials'); + } + + const responseObject = { + email: user.email, + } + + if (user.cardId){ + responseObject.cardId = user.cardId || ""; + } + return response.json(responseObject); + } catch (error) { + console.error('Error reading file:', error); + response.status(500).send('Internal server error'); + } +}) + +router.post('/register', async (request, response) => { + const { _name, email, password, confirmPassword } = request.body.registerData; + + try { + if (password !== confirmPassword) { + return response.status(400).send('Passwords do not match'); + } + const users = require('./json/users-data/success.json'); + + const existingUser = users.data.find(user => user.email === email); + + if (existingUser) { + return response.status(400).send('User with this email already exists'); + } + + return response.json({ email: email}); + } catch (error) { + console.error('Error registering user:', error); + response.status(500).send('Internal server error'); + } +}); + +router.post('/favourite', async (request, response) => { + const { cardId, email } = request.body; + + try { + const users = require('./json/users-data/success.json'); + const user = users.data.find(user => user.email === email); + + if (!user) { + return response.status(404).send('User not found'); + } + + const index = user.favourites.indexOf(cardId); + if (index !== -1) { + return response.status(200).send('Card removed from favourites successfully'); + } + + response.status(200).send('Card added to favourites successfully'); + } catch (error) { + console.error('Error adding card to favourites:', error); + response.status(500).send('Internal server error'); + } +}); + +module.exports = router; diff --git a/server/routers/webstar-project/json/cards-data/success.json b/server/routers/webstar-project/json/cards-data/success.json new file mode 100644 index 0000000..3d8f061 --- /dev/null +++ b/server/routers/webstar-project/json/cards-data/success.json @@ -0,0 +1,172 @@ +{ + "data": [ + { + "id": "1", + "ownerId": "user1@gmail.com", + "sliderImages": ["site1", "site2", "site3"], + "title": "Верстка сайтов любой сложности. Мобильная разработка", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Yuji Itadori", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site1", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "2", + "ownerId": "admin@admin.ru", + "sliderImages": ["site2", "site1", "site3"], + "title": "Дизайн сайтов. Разработка сайтов под ключ", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Admin", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site2", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "3", + "ownerId": "user2@gmail.com", + "sliderImages": ["site3", "site1", "site3"], + "title": "FullStack разработка приложений на React", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Secret Guest", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site3", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "4", + "ownerId": "super@star.ru", + "sliderImages": ["site1", "site1", "site3"], + "title": "Верстка сайтов любой сложности. Мобильная разработка", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Hatsune Miku", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site3", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "5", + "ownerId": "sb@sb.ru", + "sliderImages": ["site2", "site1", "site3"], + "title": "Дизайн сайтов. Разработка сайтов под ключ", + "tags": ["Дизайн", "Под ключ"], + "profileData": { + "name": "СберТян", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site3", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "6", + "ownerId": "tin@tin.ru", + "sliderImages": ["site3", "site1", "site3"], + "title": "FullStack разработка приложений на React", + "tags": ["Сайты", "Под ключ", "Мобильные устройства"], + "profileData": { + "name": "Помощник Олег", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site3", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + } + ] +} \ No newline at end of file diff --git a/server/routers/webstar-project/json/messages/success.json b/server/routers/webstar-project/json/messages/success.json new file mode 100644 index 0000000..3643c0c --- /dev/null +++ b/server/routers/webstar-project/json/messages/success.json @@ -0,0 +1,149 @@ +{ + "data": [ + { + "id": "1", + "ownerId": "user1@gmail.ru", + "onModerating": true, + "sliderImages": ["site1", "site2", "site3"], + "title": "Верстка сайтов любой сложности. Мобильная разработка", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Yuji Itadori", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "site2", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "2", + "ownerId": "admin@admin.ru", + "onModerating": true, + "sliderImages": ["site2", "site1", "site3"], + "title": "Дизайн сайтов. Разработка сайтов под ключ", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Admin", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "profilePicture1", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "3", + "ownerId": "user2@gmail.com", + "onModerating": true, + "sliderImages": ["site3", "site1", "site3"], + "title": "FullStack разработка приложений на React", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Secret Guest", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "profilePicture1", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "4", + "ownerId": "super@star.ru", + "onModerating": true, + "sliderImages": ["site1", "site1", "site3"], + "title": "Верстка сайтов любой сложности. Мобильная разработка", + "tags": ["Сайты", "Под ключ", "Десктоп"], + "profileData": { + "name": "Hatsune Miku", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "profilePicture1", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + }, + { + "id": "5", + "ownerId": "sb@sb.ru", + "onModerating": false, + "sliderImages": ["site2", "site1", "site3"], + "title": "Дизайн сайтов. Разработка сайтов под ключ", + "tags": ["Дизайн", "Под ключ"], + "profileData": { + "name": "СберТян", + "geolocation": "г. Казань", + "description": "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Expedita nemo cumque, magni doloremque aspernatur illo nisi magnam! Perspiciatis, aspernatur excepturi dolores autem, ut sequi dolore vitae doloremque ratione expedita velit molestias quidem hic quod culpa corrupti vel ad.", + "profilePicture": "profilePicture1", + "backgroundImage": "backgroundImage1" + }, + "articles": [ + { + "id": "1", + "label": "Верстка статичного сайта", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site1" + }, + { + "id": "2", + "label": "Разработка простого мобильного приложения", + "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt", + "imageUrl": "site2" + } + ] + } + ] +} \ No newline at end of file diff --git a/server/routers/webstar-project/json/users-data/success.json b/server/routers/webstar-project/json/users-data/success.json new file mode 100644 index 0000000..12d966d --- /dev/null +++ b/server/routers/webstar-project/json/users-data/success.json @@ -0,0 +1,50 @@ +{ + "data": [ + { + "name": "Yuji Itadori", + "email": "user1@gmail.com", + "password": "12345", + "cardId": "1", + "favourites": [ + "1", + "2", + "6" + ] + }, + { + "name": "Admin", + "email": "admin@admin.ru", + "password": "*****", + "cardId": "2", + "favourites": [] + }, + { + "name": "Secret Guest", + "email": "user2@gmail.com", + "password": "qwer", + "cardId": "3", + "favourites": [] + }, + { + "name": "Hatsune Miku", + "email": "super@star.ru", + "password": "12345", + "cardId": "4", + "favourites": [] + }, + { + "name": "СберТян", + "email": "sb@sb.ru", + "password": "12345", + "cardId": "5", + "favourites": [] + }, + { + "name": "Помощник Олег", + "email": "tin@tin.ru", + "password": "12345", + "cardId": "6", + "favourites": [] + } + ] +} \ No newline at end of file diff --git a/server/routers/webstar-project/middleware/auth-middleware.js b/server/routers/webstar-project/middleware/auth-middleware.js new file mode 100644 index 0000000..71ff52d --- /dev/null +++ b/server/routers/webstar-project/middleware/auth-middleware.js @@ -0,0 +1,14 @@ +const authMiddleware = (req, res, next) => { + const email = (req.headers.authorization || "").replace("Bearer ", ""); + + if (email) { + req.userEmail = email; + next(); + } else { + return res.status(403).json({ + message: "Access Denied", + }); + } +}; + +module.exports = authMiddleware