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
import (
"sort"
"sirlab.de/go/knowyt/user"
)
@ -9,6 +11,12 @@ func (gm *Game) GetQuotesInfoByUser(usr *user.User) *QuotesInfo {
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 {
gm.mu.Lock()
defer gm.mu.Unlock()
@ -17,11 +25,13 @@ func (gm *Game) getQuotesInfoByUserId(usrId string) []Quote {
for _, quote := range gm.quotes {
if quote.GetSourceId() == usrId {
quotes = append(quotes, Quote{
Id: quote.GetId(),
Quote: quote.GetQuote(),
Id: quote.GetId(),
Quote: quote.GetQuote(),
Created: quote.GetCreated(),
})
}
}
sort.Sort(ByCreated(quotes))
return quotes
}

View File

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

View File

@ -6,6 +6,7 @@ import (
"os"
"path"
"strings"
"time"
)
func NewQuoteFromFile(fileName string) (*Quote, error) {
@ -16,7 +17,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
var quJson QuoteJson
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 {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
@ -24,6 +25,7 @@ func NewQuoteFromFile(fileName string) (*Quote, error) {
id: id,
sourceId: quJson.SourceId,
quote: quJson.Quote,
created: quJson.Created,
isPlayed: false,
}
@ -36,6 +38,7 @@ func NewQuote(id, sourceId, quoteText string) *Quote {
id: id,
sourceId: sourceId,
quote: quoteText,
created: time.Now().Unix(),
isPlayed: false,
}
}
@ -44,6 +47,7 @@ func (qu *Quote) Save(fileName string) error {
quJson := QuoteJson{
Quote: qu.quote,
SourceId: qu.sourceId,
Created: qu.created,
}
quoteJson, err := json.MarshalIndent(quJson, "", "\t")
@ -51,7 +55,7 @@ func (qu *Quote) Save(fileName string) error {
return err
}
return os.WriteFile(fileName, quoteJson, 0777)
return os.WriteFile(fileName, quoteJson, 0666)
}
func (qu *Quote) GetId() string {
@ -77,3 +81,7 @@ func (qu *Quote) IsPlayed() bool {
func (qu *Quote) SetIsPlayed() {
qu.isPlayed = true
}
func (qu *Quote) GetCreated() int64 {
return qu.created
}

View File

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