diff --git a/index.js b/index.js
index 177c393..be05e56 100644
--- a/index.js
+++ b/index.js
@@ -62,7 +62,7 @@ app.get('/delete/:id', (req, res) => {
'DELETE FROM posts WHERE id='+id,
function(err, result) {
if (!err && result.affectedRows != 0) {
- res.send(`Пост под id ${id} был удален.`);
+ res.redirect('/');
} else {
res.status(404).send('
404 Not found
');
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('404 Not found
');
+ }
+ }
+ );
+});
+
// запрос на редактирование
app.post('/update', (req, res) => {
const data = {
@@ -82,8 +101,8 @@ app.post('/update', (req, res) => {
conn.query(
`UPDATE posts SET title='${data.title}', date='${data.date}', description='${data.description}' WHERE id=${data.id}`,
function(err, result) {
- if (!err) {
- res.send('Пост был успешно отредактирован');
+ if (!err && result.affectedRows != 0) {
+ res.redirect('/');
} else {
res.status(404).send('404 Not found
');
}
diff --git a/public/js/script.js b/public/js/script.js
index d57d33d..6a676c0 100644
--- a/public/js/script.js
+++ b/public/js/script.js
@@ -1,7 +1,7 @@
function updatePost() {
- const modal = document.querySelector('.modal-mask'),
+ const modal = document.querySelector('.modal-update'),
btnShowModal = document.querySelectorAll('.update-post'),
- btnClose = document.querySelector('.btn-close'),
+ btnClose = modal.querySelector('.btn-close'),
form = modal.querySelector('form');
btnShowModal.forEach(btn => {
@@ -27,6 +27,7 @@ function updatePost() {
});
});
+ // закрыть модалку
btnClose.addEventListener('click', (e) => {
e.preventDefault();
for (let i = 1; i < 4; i++) {
@@ -35,4 +36,21 @@ function updatePost() {
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();
\ No newline at end of file
diff --git a/views/assets/add-form.pug b/views/assets/add-form.pug
new file mode 100644
index 0000000..f3763de
--- /dev/null
+++ b/views/assets/add-form.pug
@@ -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") Закрыть окно
\ No newline at end of file
diff --git a/views/assets/update-form.pug b/views/assets/update-form.pug
index 4f49644..8b5ded8 100644
--- a/views/assets/update-form.pug
+++ b/views/assets/update-form.pug
@@ -1,4 +1,4 @@
-div(class="modal-mask")
+div(class="modal-mask modal-update")
div(class="modal")
div(class="modal-head")
p(class="modal-title") Изменить запись
diff --git a/views/main.pug b/views/main.pug
index 3f092e7..a02c2d4 100644
--- a/views/main.pug
+++ b/views/main.pug
@@ -3,6 +3,7 @@ div(class="container")
div(class="post-block__wrap")
h1 Все записи:
include assets/update-form.pug
+ include assets/add-form.pug
include assets/post-card.pug
div(class="post-block_add")
h3 Добавить запись
diff --git a/views/post.pug b/views/post.pug
index 2745380..f2a2381 100644
--- a/views/post.pug
+++ b/views/post.pug
@@ -1,7 +1,7 @@
include assets/header.pug
div(class="container")
div(class="post-block__wrap")
- h1 Это одиночный пост:
+ h1 Одностраничный пост:
include assets/update-form.pug
include assets/post-card.pug
include assets/scripts.pug
\ No newline at end of file