2023-05-10 03:19:02 +04:00
|
|
|
package mindflow
|
|
|
|
|
2023-05-22 22:35:47 +04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-09-25 00:23:06 +04:00
|
|
|
|
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/util"
|
2023-05-22 22:35:47 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
type Category struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2023-05-23 02:44:54 +04:00
|
|
|
func NewCategory(name string) (*Category, error) {
|
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("category's name should not be empty")
|
|
|
|
}
|
|
|
|
return &Category{Name: name}, nil
|
|
|
|
}
|
|
|
|
|
2023-05-22 22:35:47 +04:00
|
|
|
type Post struct {
|
|
|
|
ID int64
|
|
|
|
Category Category
|
|
|
|
Date time.Time
|
|
|
|
Title string
|
2023-09-25 00:23:06 +04:00
|
|
|
URL string
|
2023-05-22 22:35:47 +04:00
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
2023-09-25 00:23:06 +04:00
|
|
|
func NewPost(category Category, title, url, body string) (*Post, error) {
|
2023-05-22 22:35:47 +04:00
|
|
|
if title == "" || body == "" {
|
|
|
|
return nil, errors.New("empty title or body is not allowed")
|
|
|
|
}
|
|
|
|
return &Post{
|
|
|
|
Category: category,
|
|
|
|
Date: time.Now().UTC(),
|
|
|
|
Title: title,
|
2023-09-25 00:23:06 +04:00
|
|
|
URL: url,
|
2023-05-22 22:35:47 +04:00
|
|
|
Body: body}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Post) PostID() string {
|
2023-05-23 00:29:16 +04:00
|
|
|
name := strings.ToLower(p.Category.Name)
|
|
|
|
name = strings.ReplaceAll(name, " ", ".")
|
2023-05-23 04:18:07 +04:00
|
|
|
return fmt.Sprint(name, "-", p.Date.UTC().Format("20060102-150405"))
|
2023-05-22 22:35:47 +04:00
|
|
|
}
|
|
|
|
|
2023-09-25 00:23:06 +04:00
|
|
|
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, "") + p.URL
|
|
|
|
}
|
|
|
|
} else if p.URL == "" && rss {
|
|
|
|
return util.GetServiceByHost(host, "") + "/mindflow#" + p.PostID()
|
|
|
|
}
|
|
|
|
return p.URL
|
|
|
|
}
|
|
|
|
|
2023-05-10 03:19:02 +04:00
|
|
|
type Mindflow interface {
|
2023-05-23 00:47:10 +04:00
|
|
|
NewPost(post *Post) error
|
|
|
|
EditPost(post *Post) error
|
|
|
|
DeletePost(id int64) error
|
2023-05-22 05:21:00 +04:00
|
|
|
Posts() ([]Post, error)
|
2023-05-23 02:44:54 +04:00
|
|
|
NewCategory(category *Category) (int64, error)
|
2023-05-23 00:22:12 +04:00
|
|
|
EditCategory(category *Category) error
|
2023-05-22 23:06:14 +04:00
|
|
|
DeleteCategory(id int64) error
|
2023-05-23 00:47:10 +04:00
|
|
|
Categories() ([]Category, error)
|
2023-05-14 20:17:30 +04:00
|
|
|
Close() error
|
2023-05-10 03:19:02 +04:00
|
|
|
}
|