saveQuote, removeQuote (WIP)

This commit is contained in:
Settel 2021-10-15 23:07:41 +02:00
parent 8815cddebf
commit 680ec9c6ce
10 changed files with 124 additions and 2 deletions

View File

@ -16,9 +16,15 @@ export default {
methods: {
edit() {
console.log('edit')
this.save()
},
save() {
console.log('save')
this.$engine.saveQuote(this.quote.id, this.quote.quote)
},
remove() {
console.log('remove')
this.$engine.removeQuote(this.quote.id)
},
},
}
@ -46,6 +52,7 @@ export default {
line-height: 48px;
font-size: 32px;
color: #ffffff;
cursor: pointer;
&:hover {
background-color: #6040c0;

View File

@ -1,6 +1,6 @@
<template>
<div>
<CollectQuotesExplain />
<!-- <CollectQuotesExplain /> -->
<CollectQuote v-for="quote in quotes" :key="quote.id" :quote="quote" />
</div>
</template>

View File

@ -12,6 +12,7 @@ import parseSyncData from './parseSyncData'
import saveSelection from './saveSelection'
import getMyQuotes from './getMyQuotes'
import saveQuote from './saveQuote'
import removeQuote from './removeQuote'
export default (context, inject) => {
const engine = {
@ -28,6 +29,7 @@ export default (context, inject) => {
collectQuotes,
getMyQuotes,
saveQuote,
removeQuote,
startGame,
resetGame,
continueGame,

View File

@ -0,0 +1,8 @@
export default async function(id, quote) {
const { store } = this.context
await this.callApi('/api/removeQuote', {
g: store.state.engine.user?.game,
id,
})
}

View File

@ -0,0 +1,31 @@
package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) RemoveQuote(usr *user.User, w http.ResponseWriter, r *http.Request) {
gameRef := r.URL.Query().Get("g")
gm, err := app.GetGameById(gameRef)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "game not found")
return
}
if usr.GetGameId() != gameRef || !usr.IsGamemaster() {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
quoteId := r.URL.Query().Get("id")
if err := gm.RemoveQuote(usr.GetId(), quoteId); err != nil {
fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
}

View File

@ -0,0 +1,32 @@
package application
import (
"fmt"
"net/http"
"sirlab.de/go/knyt/user"
)
func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http.Request) {
gameRef := r.URL.Query().Get("g")
gm, err := app.GetGameById(gameRef)
if err != nil {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "game not found")
return
}
if usr.GetGameId() != gameRef || !usr.IsGamemaster() {
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
quoteId := r.URL.Query().Get("id")
quoteText := r.URL.Query().Get("quote")
if err := gm.SaveQuote(usr.GetId(), quoteId, quoteText); err != nil {
fmt.Printf("%s\n", err)
w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden")
return
}
}

View File

@ -11,7 +11,7 @@ func (gm *Game) getQuoteById(id string) (*quote.Quote, error) {
quote := gm.quotes[id]
if quote == nil {
return nil, fmt.Errorf("player id \"%s\" not found in game\n", id)
return nil, fmt.Errorf("quote id \"%s\" not found in game\n", id)
}
return quote, nil

View File

@ -0,0 +1,20 @@
package game
import (
"fmt"
)
func (gm *Game) RemoveQuote(usrId string, quoteId string) error {
quote, err := gm.getQuoteById(quoteId)
if err != nil {
return err
}
if quote.GetSourceId() != usrId {
return fmt.Errorf("usrId does not match quote source id")
}
fmt.Printf("%v\n", quote)
return fmt.Errorf("Game::RemoveQuote() not yet implemented")
}

View File

@ -0,0 +1,20 @@
package game
import (
"fmt"
)
func (gm *Game) SaveQuote(usrId string, quoteId string, quoteText string) error {
quote, err := gm.getQuoteById(quoteId)
if err != nil {
return err
}
if quote.GetSourceId() != usrId {
return fmt.Errorf("usrId does not match quote source id")
}
fmt.Printf("%v\n", quote)
return fmt.Errorf("Game::SaveQuote() not yet implemented")
}

View File

@ -30,6 +30,8 @@ func main() {
mux.PrivateHandleFunc("/api/finishGame", app.FinishGame)
mux.PrivateHandleFunc("/api/saveSelection", app.SaveSelection)
mux.PrivateHandleFunc("/api/getQuotes", app.GetQuotes)
mux.PrivateHandleFunc("/api/saveQuote", app.SaveQuote)
mux.PrivateHandleFunc("/api/removeQuote", app.RemoveQuote)
// default handler
fsHandler := http.FileServer(http.Dir("../../client/dist/"))