Add category pkg

This supports categories in Write.as / WriteFreely.

Ref T809
This commit is contained in:
Matt Baer
2021-09-29 17:01:17 -04:00
parent ee6bf8e8b7
commit a8daef8401
5 changed files with 93 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
// 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,
}
}