feat: save creation date along with quote and sort them from oldest to latest

This commit is contained in:
Settel 2022-08-28 17:24:45 +02:00
parent 269effd8de
commit ba5f72117e
4 changed files with 29 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package game package game
import ( import (
"sort"
"sirlab.de/go/knowyt/user" "sirlab.de/go/knowyt/user"
) )
@ -9,6 +11,12 @@ func (gm *Game) GetQuotesInfoByUser(usr *user.User) *QuotesInfo {
return &QuotesInfo{Quotes: gm.getQuotesInfoByUserId(usrId)} return &QuotesInfo{Quotes: gm.getQuotesInfoByUserId(usrId)}
} }
type ByCreated []Quote
func (quotes ByCreated) Len() int { return len(quotes) }
func (quotes ByCreated) Less(a, b int) bool { return quotes[a].Created < quotes[b].Created }
func (quotes ByCreated) Swap(a, b int) { quotes[a], quotes[b] = quotes[b], quotes[a] }
func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote { func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote {
gm.mu.Lock() gm.mu.Lock()
defer gm.mu.Unlock() defer gm.mu.Unlock()
@ -19,9 +27,11 @@ func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote {
quotes = append(quotes, Quote{ quotes = append(quotes, Quote{
Id: quote.GetId(), Id: quote.GetId(),
Quote: quote.GetQuote(), Quote: quote.GetQuote(),
Created: quote.GetCreated(),
}) })
} }
} }
sort.Sort(ByCreated(quotes))
return quotes return quotes
} }

View File

@ -1,9 +1,10 @@
package game package game
import ( import (
"sync"
"sirlab.de/go/knowyt/engine" "sirlab.de/go/knowyt/engine"
"sirlab.de/go/knowyt/quote" "sirlab.de/go/knowyt/quote"
"sync"
) )
const ( const (
@ -79,6 +80,7 @@ type GameJson struct {
type Quote struct { type Quote struct {
Id string `json:"id"` Id string `json:"id"`
Quote string `json:"quote"` Quote string `json:"quote"`
Created int64 `json:"created"`
} }
type QuotesInfo struct { type QuotesInfo struct {

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"time"
) )
func NewQuoteFromFile(fileName string) (*Quote, error) { func NewQuoteFromFile(fileName string) (*Quote, error) {
@ -16,7 +17,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
var quJson QuoteJson var quJson QuoteJson
if err := json.Unmarshal(jsonBytes, &quJson); err != nil { if err := json.Unmarshal(jsonBytes, &quJson); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err) return nil, fmt.Errorf("%s: %v", fileName, err)
} else { } else {
_, fileNameShort := path.Split(fileName) _, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json") id := strings.TrimSuffix(fileNameShort, ".json")
@ -24,6 +25,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
id: id, id: id,
sourceId: quJson.SourceId, sourceId: quJson.SourceId,
quote: quJson.Quote, quote: quJson.Quote,
created: quJson.Created,
isPlayed: false, isPlayed: false,
} }
@ -36,6 +38,7 @@ func NewQuote(id, sourceId, quoteText string) *Quote {
id: id, id: id,
sourceId: sourceId, sourceId: sourceId,
quote: quoteText, quote: quoteText,
created: time.Now().Unix(),
isPlayed: false, isPlayed: false,
} }
} }
@ -44,6 +47,7 @@ func (qu *Quote) Save(fileName string) error {
quJson := QuoteJson{ quJson := QuoteJson{
Quote: qu.quote, Quote: qu.quote,
SourceId: qu.sourceId, SourceId: qu.sourceId,
Created: qu.created,
} }
quoteJson, err := json.MarshalIndent(quJson, "", "\t") quoteJson, err := json.MarshalIndent(quJson, "", "\t")
@ -51,7 +55,7 @@ func (qu *Quote) Save(fileName string) error {
return err return err
} }
return os.WriteFile(fileName, quoteJson, 0777) return os.WriteFile(fileName, quoteJson, 0666)
} }
func (qu *Quote) GetId() string { func (qu *Quote) GetId() string {
@ -77,3 +81,7 @@ func (qu *Quote) IsPlayed() bool {
func (qu *Quote) SetIsPlayed() { func (qu *Quote) SetIsPlayed() {
qu.isPlayed = true qu.isPlayed = true
} }
func (qu *Quote) GetCreated() int64 {
return qu.created
}

View File

@ -3,6 +3,7 @@ package quote
type Quote struct { type Quote struct {
id string id string
sourceId string sourceId string
created int64
quote string quote string
isPlayed bool isPlayed bool
} }
@ -10,4 +11,5 @@ type Quote struct {
type QuoteJson struct { type QuoteJson struct {
Quote string `json:"quote"` Quote string `json:"quote"`
SourceId string `json:"source"` SourceId string `json:"source"`
Created int64 `json:"created"`
} }