2021-08-13 19:37:32 +00:00
|
|
|
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,
|
2021-08-13 19:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2021-08-13 19:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|