arimelody-web/model/blog.go

52 lines
1.4 KiB
Go
Raw Normal View History

2025-04-02 23:04:09 +01:00
package model
import (
"database/sql"
2025-04-02 23:04:09 +01:00
"fmt"
"html/template"
"regexp"
"strings"
"time"
)
type (
BlogPost struct {
2025-11-07 19:31:34 +00:00
ID string `json:"id" db:"id"`
Title string `json:"title" db:"title"`
Description string `json:"description" db:"description"`
Visible bool `json:"visible" db:"visible"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
ModifiedAt sql.NullTime `json:"modified_at" db:"modified_at"`
AuthorID string `json:"author" db:"author"`
Markdown string `json:"markdown" db:"markdown"`
HTML template.HTML `json:"html" db:"html"`
BlueskyActorID *string `json:"bluesky_actor" db:"bluesky_actor"`
BlueskyPostID *string `json:"bluesky_post" db:"bluesky_post"`
2025-04-02 23:04:09 +01:00
}
)
func (b *BlogPost) TitleNormalised() string {
2025-04-02 23:04:09 +01:00
rgx := regexp.MustCompile(`[^a-z0-9\-]`)
return rgx.ReplaceAllString(
strings.ReplaceAll(
strings.ToLower(b.Title), " ", "-",
),
"",
)
}
func (b *BlogPost) GetMonth() string {
return fmt.Sprintf("%02d", int(b.CreatedAt.Month()))
2025-04-02 23:04:09 +01:00
}
func (b *BlogPost) PrintDate() string {
2025-11-07 19:31:34 +00:00
return b.CreatedAt.Format("2 January 2006, 15:04")
}
func (b *BlogPost) PrintModifiedDate() string {
if !b.ModifiedAt.Valid {
return ""
}
2025-11-07 19:31:34 +00:00
return b.ModifiedAt.Time.Format("2 January 2006, 15:04")
2025-04-02 23:04:09 +01:00
}