Add NewCategoryFromPartial func

This commit is contained in:
Matt Baer
2021-10-16 15:27:37 -04:00
parent 375d22d230
commit f276f4d64a
+21
View File
@@ -28,3 +28,24 @@ func NewCategory(hashtag string) *Category {
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
}