Skip to content

Commit

Permalink
added soft delete to book
Browse files Browse the repository at this point in the history
  • Loading branch information
Blueblazer172 committed Jun 13, 2021
1 parent 4700ca2 commit 7c9e4ba
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
28 changes: 28 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,34 @@ app.delete('/api/book/return/:bookId', (req, res, next) => {
}
});

app.post('/api/book/delete/:bookId', (req, res, next) => {
if (req.body.userId) {
Book.destroy({where: {id: req.params.bookId}}).then(() => {
res.json({
'message': 'success',
'data': {
userId: req.body.userId
}
});
}).catch((error) => {
console.log(error)
res.json({
'message': 'failure',
'data': {
message: error.message
}
});
});
} else {
res.json({
'message': 'failure',
'data': {
message: 'You are not admin!'
}
});
}
});

app.listen(port, () => {
console.log(`Api listening at http://localhost:${port}`)
});
Binary file modified db.sqlite
Binary file not shown.
2 changes: 2 additions & 0 deletions models/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ let Book = sequelize.define('books', {
type: Sequelize.STRING,
allowNull: false
}
},{
paranoid: true
});

// create the defined table in the specified database.
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
36 changes: 36 additions & 0 deletions views/components/book/edit.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="icon" type="image/png" href="/public/favicon.png"/>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert.min.css">
<link rel="stylesheet" type="text/css" href="/public/style.css"/>
</head>
<body>
Expand Down Expand Up @@ -83,6 +84,9 @@
</div>
</div>
<div class="row mt-3 justify-content-end">
<div class="col-auto mt-4">
<button class="btn btn-danger" data-book-id="<%= book.id %>" onclick="deleteBook(event, this)">Löschen</button>
</div>
<div class="col-auto mt-4">
<button class="btn btn-primary" type="submit">Aktualisieren</button>
</div>
Expand All @@ -91,6 +95,9 @@
</div>
</main>
<%- include('../../components/footer', {page: 'admin'}) %>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sweetalert.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script>
window.onload = () => {
Expand All @@ -111,6 +118,35 @@
});
}
function deleteBook(e, element) {
e.preventDefault();
let bookId = element.getAttribute('data-book-id');
swal({
title: "Möchtest du das Buch löschen ?",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-warning",
confirmButtonText: "Ja, Buch löschen!",
cancelButtonText: "Abbrechen",
closeOnConfirm: true,
closeOnCancel: true
},
function (isConfirm) {
if (isConfirm) {
axios.post(`http://localhost:4000/api/book/delete/${bookId}/`, {
userId: '<%= (typeof user === 'undefined' || user === null) ? null : user.id %>'
}).then((res) => {
if (res.data.message === 'failure') {
window.location.href = '/login';
} else {
window.location.href = `/profile/${res.data.data.userId}`;
}
});
}
});
}
function categories(el) {
document.getElementById('newCategory').value = el.innerText.toUpperCase()
}
Expand Down

0 comments on commit 7c9e4ba

Please sign in to comment.