Refactor item formatting into core/item.go

This commit is contained in:
Tim Van Baak 2025-01-17 07:30:01 -08:00
parent c040f97680
commit 4b93a258a6
3 changed files with 46 additions and 56 deletions

View File

@ -2,7 +2,6 @@ package cmd
import ( import (
"database/sql" "database/sql"
"encoding/json"
"fmt" "fmt"
"log" "log"
@ -40,14 +39,14 @@ func init() {
} }
func feed() { func feed() {
var formatter func(core.Item) var formatter func(core.Item) string
switch feedFormat { switch feedFormat {
case "headlines": case "headlines":
formatter = formatHeadline formatter = core.FormatAsHeadline
case "json": case "json":
formatter = formatJson formatter = core.FormatAsJson
case "short": case "short":
formatter = formatShort formatter = core.FormatAsShort
default: default:
log.Fatalf("error: invalid format %s", feedFormat) log.Fatalf("error: invalid format %s", feedFormat)
} }
@ -76,26 +75,6 @@ func feed() {
} }
for _, item := range items { for _, item := range items {
formatter(item) fmt.Println(formatter(item))
} }
} }
func formatHeadline(item core.Item) {
title := item.Title
if title == "" {
title = item.Id
}
fmt.Println(title)
}
func formatJson(item core.Item) {
data, err := json.Marshal(item)
if err != nil {
log.Fatalf("error: failed to serialize %s/%s: %v", item.Source, item.Id, err)
}
fmt.Println(string(data))
}
func formatShort(item core.Item) {
fmt.Printf("%s/%s\n", item.Source, item.Id)
}

View File

@ -10,18 +10,6 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
type Item struct {
Source string `json:"source"`
Id string `json:"id"`
Created int `json:"created"`
Active bool `json:"active"`
Title string `json:"title"`
Author string `json:"author"`
Body string `json:"body"`
Link string `json:"link"`
Time int `json:"time"`
}
//go:embed sql/*.sql //go:embed sql/*.sql
var migrations embed.FS var migrations embed.FS
@ -172,15 +160,7 @@ func DeactivateItem(db *sql.DB, source string, id string) (bool, error) {
func GetAllActiveItems(db *sql.DB) ([]Item, error) { func GetAllActiveItems(db *sql.DB) ([]Item, error) {
rows, err := db.Query(` rows, err := db.Query(`
select select
source, source, id, created, active, title, author, body, link, time
id,
created,
active,
title,
author,
body,
link,
time
from items from items
where active <> 0 where active <> 0
`) `)
@ -199,15 +179,7 @@ func GetAllActiveItems(db *sql.DB) ([]Item, error) {
func GetActiveItemsForSource(db *sql.DB, source string) ([]Item, error) { func GetActiveItemsForSource(db *sql.DB, source string) ([]Item, error) {
rows, err := db.Query(` rows, err := db.Query(`
select select
source, source, id, created, active, title, author, body, link, time
id,
created,
active,
title,
author,
body,
link,
time
from items from items
where where
source = ? source = ?

39
core/item.go Normal file
View File

@ -0,0 +1,39 @@
package core
import (
"encoding/json"
"fmt"
"log"
)
type Item struct {
Source string `json:"source"`
Id string `json:"id"`
Created int `json:"created"`
Active bool `json:"active"`
Title string `json:"title"`
Author string `json:"author"`
Body string `json:"body"`
Link string `json:"link"`
Time int `json:"time"`
}
func FormatAsHeadline(item Item) string {
title := item.Title
if title == "" {
title = item.Id
}
return title
}
func FormatAsJson(item Item) string {
data, err := json.Marshal(item)
if err != nil {
log.Fatalf("error: failed to serialize %s/%s: %v", item.Source, item.Id, err)
}
return string(data)
}
func FormatAsShort(item Item) string {
return fmt.Sprintf("%s/%s", item.Source, item.Id)
}