multy-stub/server/routers/epja-2024-1/cats/admin/index.js

148 lines
4.5 KiB
JavaScript
Raw Normal View History

2024-10-16 11:06:23 +03:00
const adminRouter = require('express').Router()
const fs = require('fs')
const path = require('path')
const { TOKEN } = require('../const')
require('dotenv').config()
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
const dataFilePath = path.join(__dirname, '../data.json')
let data = require('../data.json')
2024-10-12 13:04:51 +03:00
const verifyToken = (req, res, next) => {
2024-10-16 11:06:23 +03:00
const token = req.headers['authorization']
2024-10-12 13:23:16 +03:00
if (token === TOKEN) {
2024-10-16 11:06:23 +03:00
next()
2024-10-12 13:04:51 +03:00
} else {
2024-10-16 11:06:23 +03:00
res.status(403).send({ 'status': 'Failed', 'data': 'Invalid token' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
}
2024-10-12 13:04:51 +03:00
const saveData = (data) => {
2024-10-16 11:06:23 +03:00
fs.writeFileSync(dataFilePath, JSON.stringify(data, null, 2), 'utf-8')
}
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/nickname', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { name, colored } = req.body
2024-10-12 13:04:51 +03:00
if (!name || !colored) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Nickname is required' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.nickname = { name, colored }
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'Nickname updated successfully' })
})
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/tech-stack', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { techStack } = req.body
2024-10-12 13:04:51 +03:00
if (!techStack || !Array.isArray(techStack)) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Valid tech stack is required' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.techStack = techStack
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'Tech stack updated successfully' })
})
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/city', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { city } = req.body
2024-10-12 13:04:51 +03:00
if (!city) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'City is required' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
const isValid = typeof city === 'object' && 'name' in city && 'href' in city
2024-10-12 13:04:51 +03:00
if (!isValid) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'City must contain href and name' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.city = city
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'City updated successfully' })
})
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/github-repo', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { github } = req.body
2024-10-12 13:04:51 +03:00
if (!github) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Github is required' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
const isValid = typeof github === 'object' && 'author' in github && 'repo' in github
2024-10-12 13:04:51 +03:00
if (!isValid) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Github must contain author and repo' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.githubRepo = github
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'Github updated successfully' })
})
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/nav-links', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { navLinks } = req.body
2024-10-12 13:04:51 +03:00
if (!navLinks || !Array.isArray(navLinks)) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Valid navLinks are required' })
2024-10-12 13:04:51 +03:00
}
const isValid = navLinks.every(link =>
link && typeof link === 'object' && 'href' in link && 'title' in link
2024-10-16 11:06:23 +03:00
)
2024-10-12 13:04:51 +03:00
if (!isValid) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Each navLink must contain href and title' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.navLinks = navLinks
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'Navigation links updated successfully' })
})
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/links', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { links } = req.body
2024-10-12 13:04:51 +03:00
if (!links || !Array.isArray(links)) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Valid links are required' })
2024-10-12 13:04:51 +03:00
}
const isValid = links.every(link =>
link && typeof link === 'object' && 'href' in link && 'title' in link
2024-10-16 11:06:23 +03:00
)
2024-10-12 13:04:51 +03:00
if (!isValid) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Each link must contain href and title' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.links = links
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'Links updated successfully' })
})
2024-10-12 13:04:51 +03:00
adminRouter.post('/edit/projects', verifyToken, (req, res) => {
2024-10-16 11:06:23 +03:00
const { projects } = req.body
2024-10-12 13:04:51 +03:00
if (!projects) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'Projects are required' })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
const projectFields = ['id', 'title', 'description', 'link', 'techStack', 'image']
2024-10-12 13:04:51 +03:00
const isValidProject = (project) => {
2024-10-16 11:06:23 +03:00
return projectFields.every(field => field && field in project)
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
const allProjectsValid = projects.every(project => isValidProject(project))
2024-10-12 13:04:51 +03:00
if (!allProjectsValid) {
2024-10-16 11:06:23 +03:00
return res.status(400).send({ 'status': 'Failed', 'data': 'All projects must contain ' + projectFields.join(", ") })
2024-10-12 13:04:51 +03:00
}
2024-10-16 11:06:23 +03:00
data.projects = projects
saveData(data)
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
res.status(200).send({ 'status': 'OK', 'data': 'Projects updated successfully' })
})
2024-10-12 13:04:51 +03:00
2024-10-16 11:06:23 +03:00
module.exports = adminRouter