Реализовал добавление новых стихов

This commit is contained in:
Денис Буторин 2025-06-30 03:05:22 +03:00
parent 55cd21180a
commit 39877bed54
Signed by: dekabu
GPG key ID: 867F0C8041D69B4C
3 changed files with 33 additions and 6 deletions

View file

@ -21,12 +21,20 @@ function getVerseCount() {
return Object.keys(Verse).length
}
function peopleByLogin(login) {
function peopleNumberByLogin(login) {
for (let p in People)
if (People[p].LOGIN == login)
return People[p]
return p
return false
}
function peopleByLogin(login) {
const number = peopleNumberByLogin(login)
return number ? People[number] : false
}
function checkPassword(login, password) {
return peopleByLogin(login).HASH == createHash('sha256').update(password).digest('hex')
}
function Init() {
const people = fs.readdirSync(DB + '/people')
@ -60,13 +68,13 @@ function addPeople(name, subname, login, password) {
})
}
function addVerse(title, author, genre, text) {
function addVerse(title, login, genre, text) {
const v = {
TITLE: title,
AUTHOR: author,
AUTHOR: peopleNumberByLogin(login),
GENRE: genre
}
const index = Number(Object.keys(Verse)[getVerseCount() - 1]) + 1
const index = getVerseCount() == 0 ? 1 : Number(Object.keys(Verse)[getVerseCount() - 1]) + 1
Verse[index] = v
@ -84,7 +92,9 @@ export default {
getVerse,
getPeopleCount,
getVerseCount,
peopleNumberByLogin,
peopleByLogin,
checkPassword,
Init,
addPeople,
addVerse

View file

@ -99,6 +99,17 @@ const server = createServer(async (req, res) => {
res.writeHead(301, {location: '/login'})
res.end()
break
case 'addverse':
if (db.checkPassword(body.login, body.password)) {
db.addVerse(body.title, body.login, body.genre, body.text)
res.writeHead(301, {location: '/'})
res.end()
}
else {
res.statusCode = 403
res.sendText('Нет прав! Перезайдите')
}
break
}
})
}
@ -115,6 +126,12 @@ const server = createServer(async (req, res) => {
else
notFound()
break
case 'add':
if (tokens.length == 1)
sendFile(path.join(Config.WWW, 'add.html'))
else
notFound()
break
default:
if (db.peopleByLogin(command)) {
sendText('Есть такой пользователь!')

2
www

@ -1 +1 @@
Subproject commit c8910f218e876847badc832783e3af78f68aa234
Subproject commit 12275291636c4659b3a0f3108024f4523931635f