1
0
dwelling-home/pkg/mindflow/mindflow.go

78 lines
1.7 KiB
Go
Raw Permalink Normal View History

2023-05-10 03:19:02 +04:00
package mindflow
import (
"errors"
"fmt"
"strings"
"time"
"git.arav.su/Arav/dwelling-home/pkg/util"
)
type Category struct {
ID int64
Name string
}
func NewCategory(name string) (*Category, error) {
if name == "" {
return nil, errors.New("category's name should not be empty")
}
return &Category{Name: name}, nil
}
type Post struct {
ID int64
Category Category
Date time.Time
Title string
URL string
Body string
}
func NewPost(category Category, title, url, body string) (*Post, error) {
if title == "" || body == "" {
return nil, errors.New("empty title or body is not allowed")
}
return &Post{
Category: category,
Date: time.Now().UTC(),
Title: title,
URL: url,
Body: body}, nil
}
func (p *Post) PostID() string {
name := strings.ToLower(p.Category.Name)
name = strings.ReplaceAll(name, " ", ".")
return fmt.Sprint(name, "-", p.Date.UTC().Format("20060102-150405"))
}
func (p *Post) PostURL(host string, rss bool) string {
if p.URL != "" {
if p.URL[0] == ':' {
lastColon := strings.IndexByte(p.URL[1:], ':')
service := p.URL[1 : lastColon+1]
return strings.Replace(p.URL, p.URL[:lastColon+2],
util.GetServiceByHost(host, service), 1)
} else if rss && p.URL[0] == '/' {
return util.GetServiceByHost(host, util.ServiceHome) + p.URL
}
} else if p.URL == "" && rss {
return util.GetServiceByHost(host, util.ServiceHome) + "/mindflow#" + p.PostID()
}
return p.URL
}
2023-05-10 03:19:02 +04:00
type Mindflow interface {
NewPost(post *Post) error
EditPost(post *Post) error
DeletePost(id int64) error
Posts() ([]Post, error)
NewCategory(category *Category) (int64, error)
2023-05-23 00:22:12 +04:00
EditCategory(category *Category) error
DeleteCategory(id int64) error
Categories() ([]Category, error)
Close() error
2023-05-10 03:19:02 +04:00
}