decks/modify route

This commit is contained in:
Jörn-Michael Miehe 2019-05-20 17:02:39 +02:00
parent 417c8cdd75
commit 1354b59cb0
4 changed files with 63 additions and 2 deletions

View file

@ -187,13 +187,15 @@ class FFTCGDB
modDeck: (userID, deckID, deckCards) ->
new Promise (resolve, reject) =>
stmt = @db.prepare 'UPDATE decks SET json = ? WHERE deck = ? AND user = ?'
stmt.run [JSON.stringify deckCards, deckID, userID], (err) ->
stmt.run [(JSON.stringify deckCards), deckID, userID], (err) ->
stmt.finalize()
if err
logger.warn "modDeck: FAIL db '#{err.code}' for '#{deckID}'"
reject messages.db
else if @changes == 0
logger.warn "no changes for input (#{userID}, #{deckID}, #{JSON.stringify deckCards})!"
reject messages.db
else
logger.debug "modDeck: OK '#{deckID}'"
resolve deckID
getDecks: (userID) ->

View file

@ -0,0 +1,34 @@
logger = (require 'logging').default '/decks/modify'
# session storage (volatile data)
session = (require '../../session')
# fftcg.db (persistent data)
fftcgdb = (require '../../db')
module.exports =
url: '/decks/modify'
method: 'POST'
# schema: (require './modify.schema')
handler: (request, reply) ->
session.check request.body.session ? ""
.then (userid) ->
# active session found, get associated user
fftcgdb.modDeck (userid), (request.body.deckID), (request.body.deckCards)
.then (deckID) ->
logger.info "OK user '#{userid}' modified 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

View file

@ -22,6 +22,8 @@ fastify.route (require "./routes/#{route}") for route in [
'user/register'
# list decks
'decks/list'
# modify deck
'decks/modify'
]
# request logging

View file

@ -42,6 +42,9 @@
</template>
<script>
import * as Cookies from 'js-cookie'
import axios from '@/plugins/axios'
export default {
name: 'DeckEditor',
@ -57,6 +60,10 @@ export default {
checked: false
}),
computed: {
session: () => Cookies.get('session')
},
methods: {
parse_deck(deck_string) {
let retval = {
@ -164,6 +171,22 @@ export default {
// deck has now been checked
this.checked = true
},
save() {
let new_deck = this.parse_deck(this.$refs.textarea.lazyValue)
axios
.post('/decks/modify', {
session: this.session,
deckID: this.id,
deckCards: new_deck
})
.then(response => {
if (response.data.success) {
this.close()
}
})
}
},