2023-05-10 03:19:02 +04:00
|
|
|
package mindflow
|
|
|
|
|
2023-05-22 22:35:47 +04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Category struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Post struct {
|
|
|
|
ID int64
|
|
|
|
Category Category
|
|
|
|
Date time.Time
|
|
|
|
Title string
|
|
|
|
Body string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPost(category Category, title, 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,
|
|
|
|
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, " ", ".")
|
|
|
|
return fmt.Sprint(name, "-", p.Date.Format("20060102-1504"))
|
2023-05-22 22:35:47 +04:00
|
|
|
}
|
|
|
|
|
2023-05-10 03:19:02 +04:00
|
|
|
type Mindflow interface {
|
2023-05-14 03:36:13 +04:00
|
|
|
New(post *Post) error
|
|
|
|
Edit(post *Post) error
|
2023-05-10 03:19:02 +04:00
|
|
|
Delete(id int64) error
|
2023-05-22 05:21:00 +04:00
|
|
|
Posts() ([]Post, error)
|
2023-05-22 05:21:53 +04:00
|
|
|
Categories() ([]Category, error)
|
2023-05-14 03:52:28 +04:00
|
|
|
NewCategory(name string) (int64, error)
|
|
|
|
GetCategoryByID(id int64) (string, 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-22 23:05:52 +04:00
|
|
|
DeleteUnusedCategories() error
|
2023-05-14 20:17:30 +04:00
|
|
|
Close() error
|
2023-05-10 03:19:02 +04:00
|
|
|
}
|