Page Menu
Home
Musing Studio
Search
Configure Global Search
Log In
Files
F10669890
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
1 KB
Subscribers
None
View Options
diff --git a/category/category.go b/category/category.go
index f60dc4b..bc427b1 100644
--- a/category/category.go
+++ b/category/category.go
@@ -1,30 +1,51 @@
// Package category supports post categories
package category
import (
"errors"
"github.com/writeas/slug"
)
var (
ErrNotFound = errors.New("category doesn't exist")
)
// Category represents a post tag with additional metadata, like a title and slug.
type Category struct {
ID int64 `json:"-"`
Hashtag string `json:"hashtag"`
Slug string `json:"slug"`
Title string `json:"title"`
}
// NewCategory creates a Category you can insert into the database, based on a hashtag. It automatically breaks up the
// hashtag by words, based on capitalization, for both the title and a URL-friendly slug.
func NewCategory(hashtag string) *Category {
title := titleFromHashtag(hashtag)
return &Category{
Hashtag: hashtag,
Slug: slug.Make(title),
Title: title,
}
}
+
+// NewCategoryFromPartial creates a Category from a partially-populated Category, such as when a user initially creates
+// one.
+func NewCategoryFromPartial(cat *Category) *Category {
+ newCat := &Category{
+ Hashtag: cat.Hashtag,
+ }
+ // Create title from hashtag, if none supplied
+ if cat.Title == "" {
+ newCat.Title = titleFromHashtag(cat.Hashtag)
+ } else {
+ newCat.Title = cat.Title
+ }
+ // Create slug from title, if none supplied; otherwise ensure slug is valid
+ if cat.Slug == "" {
+ newCat.Slug = slug.Make(newCat.Title)
+ } else {
+ newCat.Slug = slug.Make(cat.Slug)
+ }
+ return newCat
+}
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Fri, May 16, 6:15 PM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3240324
Attached To
rWC Write.as Web Core
Event Timeline
Log In to Comment