From f276f4d64a2320f0e2024c1a840a62fbf4613811 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 16 Oct 2021 15:27:37 -0400 Subject: [PATCH] Add NewCategoryFromPartial func --- category/category.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/category/category.go b/category/category.go index f60dc4b..bc427b1 100644 --- a/category/category.go +++ b/category/category.go @@ -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 +}