create new quote
This commit is contained in:
parent
1df31a6e35
commit
fd8aaeef78
@ -20,7 +20,7 @@ export default {
|
||||
},
|
||||
save() {
|
||||
console.log('save')
|
||||
this.$engine.saveQuote(this.quote.id, this.quote.quote)
|
||||
this.$engine.saveQuote(":new:", this.quote.quote)
|
||||
},
|
||||
remove() {
|
||||
console.log('remove')
|
||||
|
@ -2,8 +2,10 @@ package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"net/http"
|
||||
"path"
|
||||
"sirlab.de/go/knyt/game"
|
||||
"sirlab.de/go/knyt/user"
|
||||
)
|
||||
|
||||
@ -22,20 +24,27 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
|
||||
return
|
||||
}
|
||||
|
||||
quoteId := r.URL.Query().Get("id")
|
||||
quoteText := r.URL.Query().Get("quote")
|
||||
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
|
||||
quoteFileNameShort := quoteId + ".json"
|
||||
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort)
|
||||
|
||||
err2 := gm.SaveQuote(
|
||||
quoteFileName,
|
||||
quoteId := r.URL.Query().Get("id")
|
||||
if quoteId == ":new:" {
|
||||
quoteNewUuid := uuid.NewString()
|
||||
err = gm.CreateQuote(
|
||||
app.getQuoteFileNameFromId(gm, quoteNewUuid),
|
||||
usr.GetId(),
|
||||
quoteNewUuid,
|
||||
quoteText,
|
||||
)
|
||||
} else {
|
||||
err = gm.SaveQuote(
|
||||
app.getQuoteFileNameFromId(gm, quoteId),
|
||||
usr.GetId(),
|
||||
quoteId,
|
||||
quoteText,
|
||||
)
|
||||
if err2 != nil {
|
||||
fmt.Printf("%s\n", err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("%s", err)
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
fmt.Fprintf(w, "forbidden")
|
||||
return
|
||||
@ -43,3 +52,9 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
|
||||
|
||||
fmt.Fprintf(w, "ok")
|
||||
}
|
||||
|
||||
func (app *Application) getQuoteFileNameFromId(gm *game.Game, id string) string {
|
||||
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId())
|
||||
quoteFileNameShort := id + ".json"
|
||||
return path.Join(gameDirName, "quotes", quoteFileNameShort)
|
||||
}
|
||||
|
@ -2,19 +2,25 @@ package game
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sirlab.de/go/knyt/quote"
|
||||
)
|
||||
|
||||
func (gm *Game) CreateQuote(fileName, usrId, quoteId, quoteText string) error {
|
||||
qu := quote.NewQuote(quoteId, usrId, quoteText)
|
||||
return qu.Save(fileName)
|
||||
}
|
||||
|
||||
func (gm *Game) SaveQuote(fileName, usrId, quoteId, quoteText string) error {
|
||||
quote, err := gm.getQuoteById(quoteId)
|
||||
qu, err := gm.getQuoteById(quoteId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if quote.GetSourceId() != usrId {
|
||||
if qu.GetSourceId() != usrId {
|
||||
return fmt.Errorf("usrId does not match quote source id")
|
||||
}
|
||||
|
||||
quote.SetQuote(quoteText)
|
||||
qu.SetQuote(quoteText)
|
||||
|
||||
return quote.Save(fileName)
|
||||
return qu.Save(fileName)
|
||||
}
|
||||
|
@ -2,4 +2,7 @@ module sirlab.de/go/knyt
|
||||
|
||||
go 1.16
|
||||
|
||||
require github.com/imkira/go-observer v1.0.3
|
||||
require (
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/imkira/go-observer v1.0.3
|
||||
)
|
||||
|
@ -1,2 +1,4 @@
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/imkira/go-observer v1.0.3 h1:l45TYAEeAB4L2xF6PR2gRLn2NE5tYhudh33MLmC7B80=
|
||||
github.com/imkira/go-observer v1.0.3/go.mod h1:zLzElv2cGTHufQG17IEILJMPDg32TD85fFgKyFv00wU=
|
||||
|
@ -31,6 +31,15 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func NewQuote(id, sourceId, quoteText string) *Quote {
|
||||
return &Quote{
|
||||
id: id,
|
||||
sourceId: sourceId,
|
||||
quote: quoteText,
|
||||
isPlayed: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (qu *Quote) Save(fileName string) error {
|
||||
quJson := QuoteJson{
|
||||
Quote: qu.quote,
|
||||
|
Loading…
Reference in New Issue
Block a user