diff --git a/backend/db.coffee b/backend/db.coffee index 734770c..2e0d075 100644 --- a/backend/db.coffee +++ b/backend/db.coffee @@ -210,10 +210,10 @@ class FFTCGDB logger.debug "getDecks: OK '#{userID}'" resolve (id: row.deck, content: JSON.parse row.json for row, i in rows) - delDeck: (deckID) -> + delDeck: (userID, deckID) -> new Promise (resolve, reject) => - stmt = @db.prepare 'DELETE FROM decks WHERE deck = ?' - stmt.run [deckID], (err) -> + stmt = @db.prepare 'DELETE FROM decks WHERE deck = ? AND user = ?' + stmt.run [deckID, userID], (err) -> stmt.finalize() if err logger.warn "delDeck: FAIL db '#{err.code}' for '#{deckID}'" diff --git a/backend/routes/decks/delete.coffee b/backend/routes/decks/delete.coffee new file mode 100644 index 0000000..f3166b2 --- /dev/null +++ b/backend/routes/decks/delete.coffee @@ -0,0 +1,34 @@ +logger = (require 'logging').default '/decks/delete' + +# session storage (volatile data) +session = (require '../../session') +# fftcg.db (persistent data) +fftcgdb = (require '../../db') + +module.exports = + url: '/decks/delete' + method: 'POST' + # schema: (require './modify.schema') + + handler: (request, reply) -> + session.check request.body.session ? "" + .then (userid) -> + # active session found, get associated user + fftcgdb.delDeck (userid), (request.body.deckID) + .then (deckID) -> + logger.info "OK user '#{userid}' deleted deck '#{deckID}'" + reply.send + success: true + deck: deckID + + .catch (err) -> + # couldnt get user details + logger.warn "FAIL '#{err}' for user id '#{userid}'" + reply.send + success: false + + .catch -> + # no session found + logger.info "FAIL '#{request.body.session}' session not found" + reply.send + success: false diff --git a/backend/server.coffee b/backend/server.coffee index 07e8367..78265a0 100644 --- a/backend/server.coffee +++ b/backend/server.coffee @@ -26,6 +26,8 @@ fastify.route (require "./routes/#{route}") for route in [ 'decks/add' # modify deck 'decks/modify' + # delete deck + 'decks/delete' ] # request logging diff --git a/frontend/src/components/Deck.vue b/frontend/src/components/Deck.vue index 1208c14..7b6516b 100644 --- a/frontend/src/components/Deck.vue +++ b/frontend/src/components/Deck.vue @@ -28,6 +28,40 @@ edit + + + + delete + + + + + Really delete this deck? + + + + Are you sure you want to delete this deck? This cannot be undone. + + + + + + + Cancel + + + + Confirm + + + + @@ -60,7 +94,8 @@ export default { }, data: () => ({ - editing: false + editing: false, + deleting: false }), computed: { @@ -81,6 +116,19 @@ export default { this.$emit('change') } }) + }, + + delete_deck() { + axios + .post('/decks/delete', { + session: this.session, + deckID: this.deck.id + }) + .then(response => { + if (response.data.success) { + this.$emit('change') + } + }) } } }