package news import ( "dk/internal/store" "fmt" "strings" "time" ) // News represents a news post in the game type News struct { ID int `json:"id"` Author int `json:"author"` Posted int64 `json:"posted"` Content string `json:"content"` } func (n *News) Save() error { return GetStore().UpdateWithRebuild(n.ID, n) } func (n *News) Delete() error { GetStore().RemoveWithRebuild(n.ID) return nil } // Creates a new News with sensible defaults func New() *News { return &News{ Author: 0, // No author by default Posted: time.Now().Unix(), // Current time Content: "", // Empty content } } // Validate checks if news has valid values func (n *News) Validate() error { if n.Posted < 0 { return fmt.Errorf("news Posted timestamp cannot be negative") } if strings.TrimSpace(n.Content) == "" { return fmt.Errorf("news Content cannot be empty") } return nil } // NewsStore with enhanced BaseStore type NewsStore struct { *store.BaseStore[News] } // Global store with singleton pattern var GetStore = store.NewSingleton(func() *NewsStore { ns := &NewsStore{BaseStore: store.NewBaseStore[News]()} // Register indices ns.RegisterIndex("byAuthor", store.BuildIntGroupIndex(func(n *News) int { return n.Author })) ns.RegisterIndex("allByPosted", store.BuildSortedListIndex(func(a, b *News) bool { if a.Posted != b.Posted { return a.Posted > b.Posted // DESC } return a.ID > b.ID // DESC })) return ns }) // Enhanced CRUD operations func (ns *NewsStore) AddNews(news *News) error { return ns.AddWithRebuild(news.ID, news) } func (ns *NewsStore) RemoveNews(id int) { ns.RemoveWithRebuild(id) } func (ns *NewsStore) UpdateNews(news *News) error { return ns.UpdateWithRebuild(news.ID, news) } // Data persistence func LoadData(dataPath string) error { ns := GetStore() return ns.BaseStore.LoadData(dataPath) } func SaveData(dataPath string) error { ns := GetStore() return ns.BaseStore.SaveData(dataPath) } // Query functions using enhanced store func Find(id int) (*News, error) { ns := GetStore() news, exists := ns.Find(id) if !exists { return nil, fmt.Errorf("news with ID %d not found", id) } return news, nil } func All() ([]*News, error) { ns := GetStore() return ns.AllSorted("allByPosted"), nil } func ByAuthor(authorID int) ([]*News, error) { ns := GetStore() return ns.GroupByIndex("byAuthor", authorID), nil } func Recent(limit int) ([]*News, error) { ns := GetStore() all := ns.AllSorted("allByPosted") if limit > len(all) { limit = len(all) } return all[:limit], nil } func Since(since int64) ([]*News, error) { ns := GetStore() return ns.FilterByIndex("allByPosted", func(n *News) bool { return n.Posted >= since }), nil } func Between(start, end int64) ([]*News, error) { ns := GetStore() return ns.FilterByIndex("allByPosted", func(n *News) bool { return n.Posted >= start && n.Posted <= end }), nil } func Search(term string) ([]*News, error) { ns := GetStore() lowerTerm := strings.ToLower(term) return ns.FilterByIndex("allByPosted", func(n *News) bool { return strings.Contains(strings.ToLower(n.Content), lowerTerm) }), nil } // Insert with ID assignment func (n *News) Insert() error { ns := GetStore() if n.ID == 0 { n.ID = ns.GetNextID() } return ns.AddNews(n) } // Helper methods func (n *News) PostedTime() time.Time { return time.Unix(n.Posted, 0) } func (n *News) SetPostedTime(t time.Time) { n.Posted = t.Unix() } func (n *News) IsRecent() bool { return time.Since(n.PostedTime()) < 24*time.Hour } func (n *News) Age() time.Duration { return time.Since(n.PostedTime()) } func (n *News) ReadableTime() string { return n.PostedTime().Format("Jan 2, 2006 3:04 PM") } func (n *News) IsAuthor(userID int) bool { return n.Author == userID } func (n *News) Preview(maxLength int) string { if len(n.Content) <= maxLength { return n.Content } if maxLength < 3 { return n.Content[:maxLength] } return n.Content[:maxLength-3] + "..." } func (n *News) WordCount() int { if n.Content == "" { return 0 } // Simple word count by splitting on spaces words := 0 inWord := false for _, char := range n.Content { if char == ' ' || char == '\t' || char == '\n' || char == '\r' { if inWord { words++ inWord = false } } else { inWord = true } } if inWord { words++ } return words } func (n *News) Length() int { return len(n.Content) } func (n *News) Contains(term string) bool { return strings.Contains(strings.ToLower(n.Content), strings.ToLower(term)) } func (n *News) IsEmpty() bool { return strings.TrimSpace(n.Content) == "" }