2021-08-13 19:37:32 +00:00
|
|
|
package quote
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-08-28 15:24:45 +00:00
|
|
|
"time"
|
2021-08-13 19:37:32 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-08-28 15:24:45 +00:00
|
|
|
return nil, fmt.Errorf("%s: %v", fileName, err)
|
2021-08-13 19:37:32 +00:00
|
|
|
} 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,
|
2022-08-28 15:24:45 +00:00
|
|
|
created: quJson.Created,
|
2021-09-17 21:40:15 +00:00
|
|
|
isPlayed: false,
|
2021-08-13 19:37:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return qu, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 18:14:58 +00:00
|
|
|
func NewQuote(id, sourceId, quoteText string) *Quote {
|
|
|
|
return &Quote{
|
|
|
|
id: id,
|
|
|
|
sourceId: sourceId,
|
|
|
|
quote: quoteText,
|
2022-08-28 15:24:45 +00:00
|
|
|
created: time.Now().Unix(),
|
2021-10-16 18:14:58 +00:00
|
|
|
isPlayed: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:57:28 +00:00
|
|
|
func (qu *Quote) Save(fileName string) error {
|
|
|
|
quJson := QuoteJson{
|
|
|
|
Quote: qu.quote,
|
|
|
|
SourceId: qu.sourceId,
|
2022-08-28 15:24:45 +00:00
|
|
|
Created: qu.created,
|
2021-10-16 14:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
quoteJson, err := json.MarshalIndent(quJson, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-28 15:24:45 +00:00
|
|
|
return os.WriteFile(fileName, quoteJson, 0666)
|
2021-10-16 14:57:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 19:37:32 +00:00
|
|
|
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
|
|
|
|
2021-10-16 14:57:28 +00:00
|
|
|
func (qu *Quote) SetQuote(quote string) {
|
|
|
|
qu.quote = quote
|
|
|
|
}
|
|
|
|
|
2021-09-17 21:40:15 +00:00
|
|
|
func (qu *Quote) IsPlayed() bool {
|
|
|
|
return qu.isPlayed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qu *Quote) SetIsPlayed() {
|
|
|
|
qu.isPlayed = true
|
|
|
|
}
|
2022-08-28 15:24:45 +00:00
|
|
|
|
|
|
|
func (qu *Quote) GetCreated() int64 {
|
|
|
|
return qu.created
|
|
|
|
}
|