1
0

Changed Post struct. Added NewPost() func and PostID() method.

This commit is contained in:
Alexander Andreev 2023-05-10 03:18:39 +04:00
parent ee486aefdf
commit 3f4c2cc2f6
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -1,12 +1,31 @@
package mindflow
import (
"errors"
"fmt"
"strings"
"time"
)
type Post struct {
Category string
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 {
return fmt.Sprint(strings.ToLower(p.Category.Name), "-", p.Date.Format("20060102-1504"))
}