53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package mindflow
|
|
|
|
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 {
|
|
name := strings.ToLower(p.Category.Name)
|
|
name = strings.ReplaceAll(name, " ", ".")
|
|
return fmt.Sprint(name, "-", p.Date.Format("20060102-1504"))
|
|
}
|
|
|
|
type Mindflow interface {
|
|
New(post *Post) error
|
|
Edit(post *Post) error
|
|
Delete(id int64) error
|
|
Posts() ([]Post, error)
|
|
Categories() ([]Category, error)
|
|
NewCategory(name string) (int64, error)
|
|
GetCategoryByID(id int64) (string, error)
|
|
EditCategory(category *Category) error
|
|
DeleteCategory(id int64) error
|
|
DeleteUnusedCategories() error
|
|
Close() error
|
|
}
|