final project

This commit is contained in:
Sergey Yarkov 2019-10-08 20:07:12 +03:00
parent 88ac2ac614
commit 8fda412f73
6 changed files with 60 additions and 7 deletions

View File

@ -62,7 +62,7 @@ app.get('/delete/:id', (req, res) => {
'DELETE FROM posts WHERE id='+id, 'DELETE FROM posts WHERE id='+id,
function(err, result) { function(err, result) {
if (!err && result.affectedRows != 0) { if (!err && result.affectedRows != 0) {
res.send(`Пост под id ${id} был удален.`); res.redirect('/');
} else { } else {
res.status(404).send('<h1>404 Not found</h1>'); res.status(404).send('<h1>404 Not found</h1>');
console.log(err); console.log(err);
@ -71,6 +71,25 @@ app.get('/delete/:id', (req, res) => {
); );
}); });
// запрос на добавление
app.post('/add', (req, res) => {
const data = {
title: req.body.title,
date: req.body.date,
description: req.body.description
};
conn.query(
`INSERT INTO posts (title, date, description) VALUES ('${data.title}', '${data.date}', '${data.description}')`,
function(err, result) {
if (!err && result.affectedRows != 0) {
res.redirect('/');
} else {
res.status(404).send('<h1>404 Not found</h1>');
}
}
);
});
// запрос на редактирование // запрос на редактирование
app.post('/update', (req, res) => { app.post('/update', (req, res) => {
const data = { const data = {
@ -82,8 +101,8 @@ app.post('/update', (req, res) => {
conn.query( conn.query(
`UPDATE posts SET title='${data.title}', date='${data.date}', description='${data.description}' WHERE id=${data.id}`, `UPDATE posts SET title='${data.title}', date='${data.date}', description='${data.description}' WHERE id=${data.id}`,
function(err, result) { function(err, result) {
if (!err) { if (!err && result.affectedRows != 0) {
res.send('Пост был успешно отредактирован'); res.redirect('/');
} else { } else {
res.status(404).send('<h1>404 Not found</h1>'); res.status(404).send('<h1>404 Not found</h1>');
} }

View File

@ -1,7 +1,7 @@
function updatePost() { function updatePost() {
const modal = document.querySelector('.modal-mask'), const modal = document.querySelector('.modal-update'),
btnShowModal = document.querySelectorAll('.update-post'), btnShowModal = document.querySelectorAll('.update-post'),
btnClose = document.querySelector('.btn-close'), btnClose = modal.querySelector('.btn-close'),
form = modal.querySelector('form'); form = modal.querySelector('form');
btnShowModal.forEach(btn => { btnShowModal.forEach(btn => {
@ -27,6 +27,7 @@ function updatePost() {
}); });
}); });
// закрыть модалку
btnClose.addEventListener('click', (e) => { btnClose.addEventListener('click', (e) => {
e.preventDefault(); e.preventDefault();
for (let i = 1; i < 4; i++) { for (let i = 1; i < 4; i++) {
@ -35,4 +36,21 @@ function updatePost() {
modal.style.display = 'none'; modal.style.display = 'none';
}); });
} }
function addPost() {
const modal = document.querySelector('.modal-add'),
btnShowModal = document.querySelector('.post-block_add'),
btnClose = modal.querySelector('.btn-close');
btnShowModal.addEventListener('click', () => {
modal.style.display = 'flex'; // показать модальное окно
// закрыть модалку
btnClose.addEventListener('click', (e) => {
e.preventDefault();
modal.style.display = 'none';
});
});
}
addPost();
updatePost(); updatePost();

15
views/assets/add-form.pug Normal file
View File

@ -0,0 +1,15 @@
div(class="modal-mask modal-add")
div(class="modal")
div(class="modal-head")
p(class="modal-title") Добавить запись
div(class="modal-body")
form(action="/add" method="POST")
label(for="title") Название
input(id="title" type="text" name="title" placeholder="Запись" required)
label(for="date") Дата
input(id="date" type="date" name="date" required)
label(for="description") Описание
textarea(id="description" type="text" name="description" placeholder="Описание" required)
div(class="modal-footer")
button(class="button-primary btn-send" type="submit") Принять
button(class="button-danger btn-close") Закрыть окно

View File

@ -1,4 +1,4 @@
div(class="modal-mask") div(class="modal-mask modal-update")
div(class="modal") div(class="modal")
div(class="modal-head") div(class="modal-head")
p(class="modal-title") Изменить запись p(class="modal-title") Изменить запись

View File

@ -3,6 +3,7 @@ div(class="container")
div(class="post-block__wrap") div(class="post-block__wrap")
h1 Все записи: h1 Все записи:
include assets/update-form.pug include assets/update-form.pug
include assets/add-form.pug
include assets/post-card.pug include assets/post-card.pug
div(class="post-block_add") div(class="post-block_add")
h3 Добавить запись h3 Добавить запись

View File

@ -1,7 +1,7 @@
include assets/header.pug include assets/header.pug
div(class="container") div(class="container")
div(class="post-block__wrap") div(class="post-block__wrap")
h1 Это одиночный пост: h1 Одностраничный пост:
include assets/update-form.pug include assets/update-form.pug
include assets/post-card.pug include assets/post-card.pug
include assets/scripts.pug include assets/scripts.pug