knowyt/server/src/quote/quote.go

53 lines
873 B
Go
Raw Normal View History

package quote
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
)
func NewQuoteFromFile(fileName string) (*Quote, error) {
jsonBytes, err := os.ReadFile(fileName)
if err != nil {
return nil, err
}
var quJson QuoteJson
if err := json.Unmarshal(jsonBytes, &quJson); err != nil {
return nil, fmt.Errorf("%s: %v\n", fileName, err)
} else {
_, fileNameShort := path.Split(fileName)
id := strings.TrimSuffix(fileNameShort, ".json")
qu := &Quote{
2021-09-06 21:18:34 +00:00
id: id,
sourceId: quJson.SourceId,
quote: quJson.Quote,
2021-09-17 21:40:15 +00:00
isPlayed: false,
}
return qu, nil
}
}
func (qu *Quote) GetId() string {
return qu.id
}
2021-09-06 21:18:34 +00:00
func (qu *Quote) GetSourceId() string {
return qu.sourceId
}
func (qu *Quote) GetQuote() string {
return qu.quote
}
2021-09-17 21:40:15 +00:00
func (qu *Quote) IsPlayed() bool {
return qu.isPlayed
}
func (qu *Quote) SetIsPlayed() {
qu.isPlayed = true
}