250 lines
5.9 KiB
Go
250 lines
5.9 KiB
Go
package news
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"dk/internal/database"
|
|
|
|
"zombiezen.com/go/sqlite"
|
|
)
|
|
|
|
// News represents a news post in the database
|
|
type News struct {
|
|
ID int `json:"id"`
|
|
Author int `json:"author"`
|
|
Posted int64 `json:"posted"`
|
|
Content string `json:"content"`
|
|
|
|
db *database.DB
|
|
}
|
|
|
|
// Find retrieves a news post by ID
|
|
func Find(db *database.DB, id int) (*News, error) {
|
|
news := &News{db: db}
|
|
|
|
query := "SELECT id, author, posted, content FROM news WHERE id = ?"
|
|
err := db.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news.ID = stmt.ColumnInt(0)
|
|
news.Author = stmt.ColumnInt(1)
|
|
news.Posted = stmt.ColumnInt64(2)
|
|
news.Content = stmt.ColumnText(3)
|
|
return nil
|
|
}, id)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find news: %w", err)
|
|
}
|
|
|
|
if news.ID == 0 {
|
|
return nil, fmt.Errorf("news with ID %d not found", id)
|
|
}
|
|
|
|
return news, nil
|
|
}
|
|
|
|
// All retrieves all news posts ordered by posted date (newest first)
|
|
func All(db *database.DB) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := "SELECT id, author, posted, content FROM news ORDER BY posted DESC, id DESC"
|
|
err := db.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := &News{
|
|
ID: stmt.ColumnInt(0),
|
|
Author: stmt.ColumnInt(1),
|
|
Posted: stmt.ColumnInt64(2),
|
|
Content: stmt.ColumnText(3),
|
|
db: db,
|
|
}
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve all news: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// ByAuthor retrieves news posts by a specific author
|
|
func ByAuthor(db *database.DB, authorID int) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := "SELECT id, author, posted, content FROM news WHERE author = ? ORDER BY posted DESC, id DESC"
|
|
err := db.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := &News{
|
|
ID: stmt.ColumnInt(0),
|
|
Author: stmt.ColumnInt(1),
|
|
Posted: stmt.ColumnInt64(2),
|
|
Content: stmt.ColumnText(3),
|
|
db: db,
|
|
}
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, authorID)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve news by author: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Recent retrieves the most recent news posts (limited by count)
|
|
func Recent(db *database.DB, limit int) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := "SELECT id, author, posted, content FROM news ORDER BY posted DESC, id DESC LIMIT ?"
|
|
err := db.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := &News{
|
|
ID: stmt.ColumnInt(0),
|
|
Author: stmt.ColumnInt(1),
|
|
Posted: stmt.ColumnInt64(2),
|
|
Content: stmt.ColumnText(3),
|
|
db: db,
|
|
}
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, limit)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve recent news: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Since retrieves news posts since a specific timestamp
|
|
func Since(db *database.DB, since int64) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := "SELECT id, author, posted, content FROM news WHERE posted >= ? ORDER BY posted DESC, id DESC"
|
|
err := db.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := &News{
|
|
ID: stmt.ColumnInt(0),
|
|
Author: stmt.ColumnInt(1),
|
|
Posted: stmt.ColumnInt64(2),
|
|
Content: stmt.ColumnText(3),
|
|
db: db,
|
|
}
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, since)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve news since timestamp: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Between retrieves news posts between two timestamps (inclusive)
|
|
func Between(db *database.DB, start, end int64) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := "SELECT id, author, posted, content FROM news WHERE posted >= ? AND posted <= ? ORDER BY posted DESC, id DESC"
|
|
err := db.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := &News{
|
|
ID: stmt.ColumnInt(0),
|
|
Author: stmt.ColumnInt(1),
|
|
Posted: stmt.ColumnInt64(2),
|
|
Content: stmt.ColumnText(3),
|
|
db: db,
|
|
}
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, start, end)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to retrieve news between timestamps: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// Save updates an existing news post in the database
|
|
func (n *News) Save() error {
|
|
if n.ID == 0 {
|
|
return fmt.Errorf("cannot save news without ID")
|
|
}
|
|
|
|
query := `UPDATE news SET author = ?, posted = ?, content = ? WHERE id = ?`
|
|
return n.db.Exec(query, n.Author, n.Posted, n.Content, n.ID)
|
|
}
|
|
|
|
// Delete removes the news post from the database
|
|
func (n *News) Delete() error {
|
|
if n.ID == 0 {
|
|
return fmt.Errorf("cannot delete news without ID")
|
|
}
|
|
|
|
query := "DELETE FROM news WHERE id = ?"
|
|
return n.db.Exec(query, n.ID)
|
|
}
|
|
|
|
// PostedTime returns the posted timestamp as a time.Time
|
|
func (n *News) PostedTime() time.Time {
|
|
return time.Unix(n.Posted, 0)
|
|
}
|
|
|
|
// SetPostedTime sets the posted timestamp from a time.Time
|
|
func (n *News) SetPostedTime(t time.Time) {
|
|
n.Posted = t.Unix()
|
|
}
|
|
|
|
// IsRecent returns true if the news post was made within the last 24 hours
|
|
func (n *News) IsRecent() bool {
|
|
return time.Since(n.PostedTime()) < 24*time.Hour
|
|
}
|
|
|
|
// Age returns how long ago the news post was made
|
|
func (n *News) Age() time.Duration {
|
|
return time.Since(n.PostedTime())
|
|
}
|
|
|
|
// IsAuthor returns true if the given user ID is the author of this news post
|
|
func (n *News) IsAuthor(userID int) bool {
|
|
return n.Author == userID
|
|
}
|
|
|
|
// Preview returns a truncated version of the content for previews
|
|
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] + "..."
|
|
}
|
|
|
|
// WordCount returns the number of words in the content
|
|
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
|
|
} |