Changed Post struct. Added NewPost() func and PostID() method.
This commit is contained in:
parent
ee486aefdf
commit
3f4c2cc2f6
@ -1,12 +1,31 @@
|
|||||||
package mindflow
|
package mindflow
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
Category string
|
ID int64
|
||||||
|
Category Category
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Title string
|
Title string
|
||||||
Body 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"))
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user