330 lines
7.8 KiB
Go
330 lines
7.8 KiB
Go
package news
|
|
|
|
import (
|
|
"dk/internal/database"
|
|
"dk/internal/helpers/scanner"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"zombiezen.com/go/sqlite"
|
|
)
|
|
|
|
// News represents a news post in the database
|
|
type News struct {
|
|
ID int `db:"id" json:"id"`
|
|
Author int `db:"author" json:"author"`
|
|
Posted int64 `db:"posted" json:"posted"`
|
|
Content string `db:"content" json:"content"`
|
|
}
|
|
|
|
// New 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
|
|
}
|
|
}
|
|
|
|
var newsScanner = scanner.New[News]()
|
|
|
|
// newsColumns returns the column list for news queries
|
|
func newsColumns() string {
|
|
return newsScanner.Columns()
|
|
}
|
|
|
|
// scanNews populates a News struct using the fast scanner
|
|
func scanNews(stmt *sqlite.Stmt) *News {
|
|
news := &News{}
|
|
newsScanner.Scan(stmt, news)
|
|
return news
|
|
}
|
|
|
|
// Find retrieves a news post by ID
|
|
func Find(id int) (*News, error) {
|
|
var news *News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE id = ?`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news = scanNews(stmt)
|
|
return nil
|
|
}, id)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find news: %w", err)
|
|
}
|
|
|
|
if news == nil {
|
|
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() ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
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(authorID int) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE author = ? ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
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(limit int) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news ORDER BY posted DESC, id DESC LIMIT ?`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
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(since int64) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE posted >= ? ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
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(start, end int64) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE posted >= ? AND posted <= ? ORDER BY posted DESC, id DESC`
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
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 database.Exec(query, n.Author, n.Posted, n.Content, n.ID)
|
|
}
|
|
|
|
// Insert saves a new news post to the database and sets the ID
|
|
func (n *News) Insert() error {
|
|
if n.ID != 0 {
|
|
return fmt.Errorf("news already has ID %d, use Save() to update", n.ID)
|
|
}
|
|
|
|
// Use a transaction to ensure we can get the ID
|
|
err := database.Transaction(func(tx *database.Tx) error {
|
|
query := `INSERT INTO news (author, posted, content) VALUES (?, ?, ?)`
|
|
|
|
if err := tx.Exec(query, n.Author, n.Posted, n.Content); err != nil {
|
|
return fmt.Errorf("failed to insert news: %w", err)
|
|
}
|
|
|
|
// Get the last insert ID
|
|
var id int
|
|
err := tx.Query("SELECT last_insert_rowid()", func(stmt *sqlite.Stmt) error {
|
|
id = stmt.ColumnInt(0)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get insert ID: %w", err)
|
|
}
|
|
|
|
n.ID = id
|
|
return nil
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
// 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 database.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
|
|
}
|
|
|
|
// Length returns the character length of the content
|
|
func (n *News) Length() int {
|
|
return len(n.Content)
|
|
}
|
|
|
|
// Contains returns true if the content contains the given term (case-insensitive)
|
|
func (n *News) Contains(term string) bool {
|
|
return strings.Contains(strings.ToLower(n.Content), strings.ToLower(term))
|
|
}
|
|
|
|
// IsEmpty returns true if the content is empty or whitespace-only
|
|
func (n *News) IsEmpty() bool {
|
|
return strings.TrimSpace(n.Content) == ""
|
|
}
|
|
|
|
// Search retrieves news posts containing the search term in content
|
|
func Search(term string) ([]*News, error) {
|
|
var newsPosts []*News
|
|
|
|
query := `SELECT ` + newsColumns() + ` FROM news WHERE LOWER(content) LIKE LOWER(?) ORDER BY posted DESC, id DESC`
|
|
searchTerm := "%" + term + "%"
|
|
|
|
err := database.Query(query, func(stmt *sqlite.Stmt) error {
|
|
news := scanNews(stmt)
|
|
newsPosts = append(newsPosts, news)
|
|
return nil
|
|
}, searchTerm)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to search news: %w", err)
|
|
}
|
|
|
|
return newsPosts, nil
|
|
}
|
|
|
|
// ToMap converts the news to a map for efficient template rendering
|
|
func (n *News) ToMap() map[string]any {
|
|
return map[string]any{
|
|
"ID": n.ID,
|
|
"Author": n.Author,
|
|
"Posted": n.Posted,
|
|
"Content": n.Content,
|
|
|
|
// Computed values
|
|
"PostedTime": n.PostedTime(),
|
|
"IsRecent": n.IsRecent(),
|
|
"Age": n.Age(),
|
|
"WordCount": n.WordCount(),
|
|
"Length": n.Length(),
|
|
"IsEmpty": n.IsEmpty(),
|
|
}
|
|
}
|