create new quote

This commit is contained in:
Settel 2021-10-16 20:14:58 +02:00
parent 1df31a6e35
commit fd8aaeef78
6 changed files with 53 additions and 18 deletions

View File

@ -20,7 +20,7 @@ export default {
}, },
save() { save() {
console.log('save') console.log('save')
this.$engine.saveQuote(this.quote.id, this.quote.quote) this.$engine.saveQuote(":new:", this.quote.quote)
}, },
remove() { remove() {
console.log('remove') console.log('remove')

View File

@ -2,8 +2,10 @@ package application
import ( import (
"fmt" "fmt"
"github.com/google/uuid"
"net/http" "net/http"
"path" "path"
"sirlab.de/go/knyt/game"
"sirlab.de/go/knyt/user" "sirlab.de/go/knyt/user"
) )
@ -22,20 +24,27 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
return return
} }
quoteId := r.URL.Query().Get("id")
quoteText := r.URL.Query().Get("quote") quoteText := r.URL.Query().Get("quote")
gameDirName := path.Join(app.config.DataDir, "games", gm.GetId()) quoteId := r.URL.Query().Get("id")
quoteFileNameShort := quoteId + ".json" if quoteId == ":new:" {
quoteFileName := path.Join(gameDirName, "quotes", quoteFileNameShort) quoteNewUuid := uuid.NewString()
err = gm.CreateQuote(
err2 := gm.SaveQuote( app.getQuoteFileNameFromId(gm, quoteNewUuid),
quoteFileName, usr.GetId(),
quoteNewUuid,
quoteText,
)
} else {
err = gm.SaveQuote(
app.getQuoteFileNameFromId(gm, quoteId),
usr.GetId(), usr.GetId(),
quoteId, quoteId,
quoteText, quoteText,
) )
if err2 != nil { }
fmt.Printf("%s\n", err)
if err != nil {
fmt.Printf("%s", err)
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
fmt.Fprintf(w, "forbidden") fmt.Fprintf(w, "forbidden")
return return
@ -43,3 +52,9 @@ func (app *Application) SaveQuote(usr *user.User, w http.ResponseWriter, r *http
fmt.Fprintf(w, "ok") 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)
}

View File

@ -2,19 +2,25 @@ package game
import ( import (
"fmt" "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 { func (gm *Game) SaveQuote(fileName, usrId, quoteId, quoteText string) error {
quote, err := gm.getQuoteById(quoteId) qu, err := gm.getQuoteById(quoteId)
if err != nil { if err != nil {
return err return err
} }
if quote.GetSourceId() != usrId { if qu.GetSourceId() != usrId {
return fmt.Errorf("usrId does not match quote source id") return fmt.Errorf("usrId does not match quote source id")
} }
quote.SetQuote(quoteText) qu.SetQuote(quoteText)
return quote.Save(fileName) return qu.Save(fileName)
} }

View File

@ -2,4 +2,7 @@ module sirlab.de/go/knyt
go 1.16 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
)

View File

@ -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 h1:l45TYAEeAB4L2xF6PR2gRLn2NE5tYhudh33MLmC7B80=
github.com/imkira/go-observer v1.0.3/go.mod h1:zLzElv2cGTHufQG17IEILJMPDg32TD85fFgKyFv00wU= github.com/imkira/go-observer v1.0.3/go.mod h1:zLzElv2cGTHufQG17IEILJMPDg32TD85fFgKyFv00wU=

View File

@ -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 { func (qu *Quote) Save(fileName string) error {
quJson := QuoteJson{ quJson := QuoteJson{
Quote: qu.quote, Quote: qu.quote,