package quote import ( "encoding/json" "fmt" "os" "path" "strings" "time" ) 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", fileName, err) } else { _, fileNameShort := path.Split(fileName) id := strings.TrimSuffix(fileNameShort, ".json") qu := &Quote{ id: id, sourceId: quJson.SourceId, quote: quJson.Quote, created: quJson.Created, isPlayed: false, } return qu, nil } } func NewQuote(id, sourceId, quoteText string) *Quote { return &Quote{ id: id, sourceId: sourceId, quote: quoteText, created: time.Now().Unix(), isPlayed: false, } } func (qu *Quote) Save(fileName string) error { quJson := QuoteJson{ Quote: qu.quote, SourceId: qu.sourceId, Created: qu.created, } quoteJson, err := json.MarshalIndent(quJson, "", "\t") if err != nil { return err } return os.WriteFile(fileName, quoteJson, 0666) } func (qu *Quote) GetId() string { return qu.id } func (qu *Quote) GetSourceId() string { return qu.sourceId } func (qu *Quote) GetQuote() string { return qu.quote } func (qu *Quote) SetQuote(quote string) { qu.quote = quote } func (qu *Quote) IsPlayed() bool { return qu.isPlayed } func (qu *Quote) SetIsPlayed() { qu.isPlayed = true } func (qu *Quote) GetCreated() int64 { return qu.created }