Documentation
¶
Index ¶
- Variables
- type AliasDeleter
- type HookExecutor
- type Option
- type PostType
- type PostTypeAdapter
- func (a *PostTypeAdapter) GetBySlug(slug string) (PostType, error)
- func (a *PostTypeAdapter) GetFieldsByPostType(slug string) ([]customfield.FieldSchema, error)
- func (a *PostTypeAdapter) GetSystemFieldsByPostType(slug string) ([]customfield.FieldSchema, error)
- func (a *PostTypeAdapter) SearchableSlugs() []string
- type PostTypeServiceInterface
- type Repository
- type RoleChecker
- type Service
- func (s *Service) AuthorExists(ctx context.Context, username string) (bool, error)
- func (s *Service) Count(ctx context.Context, userID int, filters contentdomain.ContentFilters) (int, error)
- func (s *Service) Create(ctx context.Context, principal kernel.Principal, ...) (*contentdomain.Content, error)
- func (s *Service) DeleteContent(ctx context.Context, id int, principal kernel.Principal) error
- func (s *Service) GenerateSlug(title string) string
- func (s *Service) GenerateSlugFromTitle(ctx context.Context, title string) (string, error)
- func (s *Service) GetAll(ctx context.Context, limit int, offset int) ([]*contentdomain.Content, error)
- func (s *Service) GetByID(ctx context.Context, id int) (*contentdomain.Content, error)
- func (s *Service) GetBySlug(ctx context.Context, slug string) (*contentdomain.Content, error)
- func (s *Service) GetBySlugAndLanguage(ctx context.Context, slug string, language string) (*contentdomain.Content, error)
- func (s *Service) GetByUser(ctx context.Context, userID int, limit int, offset int) ([]*contentdomain.Content, error)
- func (s *Service) GetPublished(ctx context.Context, limit int, offset int) ([]*contentdomain.Content, error)
- func (s *Service) GetPublishedArchive(ctx context.Context, postType string, languages []string) ([]*contentdomain.ArchiveMonth, error)
- func (s *Service) GetPublishedAuthor(ctx context.Context, username string) (*contentdomain.PublishedAuthor, error)
- func (s *Service) GetPublishedAuthors(ctx context.Context, filters contentdomain.PublishedAuthorFilters) ([]*contentdomain.PublishedAuthor, error)
- func (s *Service) GetPublishedByAuthorUsername(ctx context.Context, username string, languages []string, limit int, ...) ([]*contentdomain.Content, error)
- func (s *Service) GetPublishedByID(ctx context.Context, id int) (*contentdomain.Content, error)
- func (s *Service) GetPublishedByPostType(ctx context.Context, postType string, languages []string, year int, month int, ...) ([]*contentdomain.Content, error)
- func (s *Service) GetPublishedBySlug(ctx context.Context, slug string, language string) (*contentdomain.Content, error)
- func (s *Service) GetPublishedBySlugAny(ctx context.Context, slug string) (*contentdomain.Content, error)
- func (s *Service) GetPublishedByTag(ctx context.Context, tag string, languages []string, year int, month int, ...) ([]*contentdomain.Content, error)
- func (s *Service) GetPublishedCustomPostTypes(ctx context.Context) ([]string, error)
- func (s *Service) GetPublishedPages(ctx context.Context) ([]*contentdomain.Content, error)
- func (s *Service) GetPublishedTags(ctx context.Context) ([]string, error)
- func (s *Service) GetRelated(ctx context.Context, id int, limit int) ([]*contentdomain.Content, error)
- func (s *Service) GetTranslations(ctx context.Context, translationGroupID int, excludeID int) ([]*contentdomain.Content, error)
- func (s *Service) ListByCursor(ctx context.Context, userID int, limit int, beforeID int, ...) ([]*contentdomain.Content, error)
- func (s *Service) ListByFilters(ctx context.Context, userID int, filters contentdomain.ContentFilters) ([]*contentdomain.Content, error)
- func (s *Service) Publish(ctx context.Context, id int, principal kernel.Principal) (*contentdomain.Content, error)
- func (s *Service) SearchPublished(ctx context.Context, query string, limit int) ([]*contentdomain.Content, error)
- func (s *Service) SetSystemFields(ctx context.Context, contentID int, principal kernel.Principal, ...) (*contentdomain.Content, error)
- func (s *Service) SlugExists(ctx context.Context, slug string, language string) (bool, error)
- func (s *Service) Unpublish(ctx context.Context, id int, principal kernel.Principal) (*contentdomain.Content, error)
- func (s *Service) Update(ctx context.Context, id int, principal kernel.Principal, ...) (*contentdomain.Content, error)
- type UpdateContentRequest
Constants ¶
This section is empty.
Variables ¶
var ErrPostTypeNotFound = errors.New("post type not found")
ErrPostTypeNotFound is returned when a post type is not found
Functions ¶
This section is empty.
Types ¶
type AliasDeleter ¶ added in v0.10.0
AliasDeleter removes the alias rows that point at a content item. It is wired to the alias domain service so deleting content never leaves dangling aliases behind (the SQLite schema's ON DELETE CASCADE does not fire because foreign keys are not enforced there).
type HookExecutor ¶
type HookExecutor interface {
Execute(ctx context.Context, hookName plugin.HookName, data []byte) ([]byte, error)
}
HookExecutor executes plugin hooks during content lifecycle operations.
type Option ¶ added in v0.6.0
type Option func(*Service)
Option configures a content Service at construction time.
func WithAliasDeleter ¶ added in v0.10.0
func WithAliasDeleter(ad AliasDeleter) Option
WithAliasDeleter attaches the alias cleanup so DeleteContent removes the item's alias rows. Without it deletion leaves aliases dangling (the schema's CASCADE is not enforced on SQLite).
func WithCommentsEnabled ¶ added in v0.9.0
WithCommentsEnabled sets whether the comment system is active. When false, new content always defaults to allowComments=false regardless of the request (the comment routes are unmounted at the HTTP layer; this covers the data layer so no item accepts comments).
func WithDefaultLanguage ¶ added in v0.6.0
WithDefaultLanguage sets the language used when a create or read request omits an explicit language. Defaults to "en".
func WithLanguages ¶ added in v1.1.0
WithLanguages sets the configured language list a create request is validated against: an explicit language outside the list fails with ErrInvalidLanguage. An empty list disables the check (legacy services constructed without configuration accept any language).
func WithRoleChecker ¶ added in v0.10.0
func WithRoleChecker(rc RoleChecker) Option
WithRoleChecker attaches the config-driven role registry so content operations enforce per-post-type and publish capabilities. Without it the Service behaves as before (owner-or-Admin ownership checks only).
type PostType ¶
type PostType struct {
Slug string
}
PostType represents a minimal post type for validation purposes
type PostTypeAdapter ¶
type PostTypeAdapter struct {
// contains filtered or unexported fields
}
PostTypeAdapter wraps posttype.Service to implement content.PostTypeServiceInterface
func NewPostTypeAdapter ¶
func NewPostTypeAdapter(service *posttype.Service) *PostTypeAdapter
NewPostTypeAdapter creates a new adapter for post type service
func (*PostTypeAdapter) GetBySlug ¶
func (a *PostTypeAdapter) GetBySlug(slug string) (PostType, error)
GetBySlug retrieves a post type by slug and converts it to content.PostType
func (*PostTypeAdapter) GetFieldsByPostType ¶
func (a *PostTypeAdapter) GetFieldsByPostType(slug string) ([]customfield.FieldSchema, error)
func (*PostTypeAdapter) GetSystemFieldsByPostType ¶
func (a *PostTypeAdapter) GetSystemFieldsByPostType(slug string) ([]customfield.FieldSchema, error)
func (*PostTypeAdapter) SearchableSlugs ¶ added in v1.1.0
func (a *PostTypeAdapter) SearchableSlugs() []string
SearchableSlugs projects the registered post types into the public-search scope: every non-hidden slug, sorted so queries and tests are deterministic.
type PostTypeServiceInterface ¶
type PostTypeServiceInterface interface {
GetBySlug(slug string) (PostType, error)
GetFieldsByPostType(slug string) ([]customfield.FieldSchema, error)
GetSystemFieldsByPostType(slug string) ([]customfield.FieldSchema, error)
// SearchableSlugs returns the slugs of all registered post types that are
// not hidden — the scope public search runs over. Hidden types stay out of
// every presentation surface, search included.
SearchableSlugs() []string
}
PostTypeServiceInterface defines the interface for post type service This allows the content service to validate post types without tight coupling
type Repository ¶
type Repository interface {
Create(ctx context.Context, content *contentdomain.Content) error
GetBySlug(ctx context.Context, slug string, language string) (*contentdomain.Content, error)
GetByUser(ctx context.Context, userID int, limit int, offset int) ([]*contentdomain.Content, error)
GetAll(ctx context.Context, limit int, offset int) ([]*contentdomain.Content, error)
// ListByCursor returns the caller's content in newest-first (id DESC) order using
// keyset pagination. beforeID <= 0 means "first page"; otherwise only rows with a
// strictly smaller id are returned. The optional filters restrict which rows
// qualify: empty string / nil fields are no-ops, so a zero ContentFilters value
// behaves exactly as the old unfiltered call. Tags is AND-of-tags. Author matches
// the joined users.name (with users.username as a fallback), case-insensitive.
// It is additive — the offset-based GetAll / GetByUser / ListByFilters methods are
// untouched (the agent v1 list contract is cursor-only; offset is unstable under
// concurrent inserts/deletes).
ListByCursor(ctx context.Context, userID int, limit int, beforeID int, filters contentdomain.ContentFilters) ([]*contentdomain.Content, error)
CheckSlugUnique(ctx context.Context, slug string, language string) (bool, error)
GetByID(ctx context.Context, id int) (*contentdomain.Content, error)
Update(ctx context.Context, content *contentdomain.Content) error
GetPublished(ctx context.Context, limit int, offset int) ([]*contentdomain.Content, error)
GetPublishedBySlug(ctx context.Context, slug string, language string) (*contentdomain.Content, error)
// GetPublishedByAuthorUsername returns published content by the given author
// username, filtered by languages as described in GetPublishedByPostType
// (used by the paginated public author page so the fetch-limit+1 HasMore
// probe stays accurate).
GetPublishedByAuthorUsername(ctx context.Context, username string, languages []string, limit int, offset int) ([]*contentdomain.Content, error)
AuthorExists(ctx context.Context, username string) (bool, error)
Delete(ctx context.Context, id int, userID int) error
DeleteByID(ctx context.Context, id int) error
ListByFilters(ctx context.Context, userID int, filters contentdomain.ContentFilters) ([]*contentdomain.Content, error)
// Count returns the number of content items matching the given filters, ignoring
// Limit/Offset. userID <= 0 counts across all users (admin scope); otherwise the
// count is restricted to that user's content. It shares the exact WHERE clause
// with ListByFilters so a list response's total always matches what the list
// returned.
Count(ctx context.Context, userID int, filters contentdomain.ContentFilters) (int, error)
GetPublishedPages(ctx context.Context) ([]*contentdomain.Content, error)
GetPublishedCustomPostTypes(ctx context.Context) ([]string, error)
// GetPublishedByPostType returns published content of the given post type,
// filtered by languages. The languages slice is priority-ordered:
//
// - empty: no language restriction, every language is returned.
// - single element: restricted to exactly that language.
// - multiple elements: each translation group is represented once by the
// best-ranked available language — a post missing from the primary
// language falls back to the next configured one (Hugo-style fallback).
// Rows in languages outside the slice are never returned.
//
// The filter is applied before LIMIT/OFFSET so pagination and counts stay
// exact under deduplication. When year and month are both non-zero, results
// are restricted to that calendar month.
GetPublishedByPostType(ctx context.Context, postType string, languages []string, year int, month int, limit int, offset int) ([]*contentdomain.Content, error)
// GetPublishedByTag returns published content carrying the given tag,
// filtered by languages as described in GetPublishedByPostType. When year
// and month are both non-zero, results are restricted to that calendar
// month.
GetPublishedByTag(ctx context.Context, tag string, languages []string, year int, month int, limit int, offset int) ([]*contentdomain.Content, error)
// GetPublishedTags returns the distinct set of tags used by any published
// content item, ordered for stable display. An empty (non-nil) slice is
// returned when no published content carries tags.
GetPublishedTags(ctx context.Context) ([]string, error)
// GetPublishedAuthors returns the users who have at least one published
// content item, aggregated with their published-content count and the
// distinct post types they publish under.
//
// With a zero-value PublishedAuthorFilters, results are ordered by content
// count (desc) then username (asc) — the legacy default ranking. When
// filters.SortBy is a non-empty custom-field slug, results are instead
// ordered by that field's numeric value (the wrapping CASE in each driver
// treats non-numeric values as 0). CustomFieldFilters add additional
// numeric/equality predicates on user custom fields.
//
// Each PublishedAuthor carries the raw user custom_fields map. The handler
// projects only [[public_field]] allowlisted slugs into the public DTO.
// Email, role, and status are never populated — those stay behind
// authentication.
GetPublishedAuthors(ctx context.Context, filters contentdomain.PublishedAuthorFilters) ([]*contentdomain.PublishedAuthor, error)
// GetPublishedAuthor returns a single published author by username, or nil
// if no published content exists for that user. The returned PublishedAuthor
// follows the same shape as GetPublishedAuthors and carries the raw
// custom_fields map.
GetPublishedAuthor(ctx context.Context, username string) (*contentdomain.PublishedAuthor, error)
// GetPublishedArchive returns published-content counts grouped by year and
// month, ordered newest-first, filtered by languages as described in
// GetPublishedByPostType. When postType is non-empty, results are
// restricted to that post type. An empty postType aggregates across all
// types.
GetPublishedArchive(ctx context.Context, postType string, languages []string) ([]*contentdomain.ArchiveMonth, error)
// SearchPublished returns published content whose title or meta
// description matches the query, scoped to postTypes. A nil or empty
// postTypes list searches every type; the service passes an explicit list
// of registered, non-hidden types when the post-type registry is
// available. Results are ordered newest-first.
SearchPublished(ctx context.Context, query string, postTypes []string, limit int) ([]*contentdomain.Content, error)
// GetTranslations returns all content items in the same translation group, excluding the given content ID.
GetTranslations(ctx context.Context, translationGroupID int, excludeID int) ([]*contentdomain.Content, error)
// TranslationGroupExists checks whether a content item with the given ID exists.
TranslationGroupExists(ctx context.Context, id int) (bool, error)
// GetPublishedBySlugAny finds published content by slug regardless of language.
GetPublishedBySlugAny(ctx context.Context, slug string) (*contentdomain.Content, error)
// GetRelatedByTags returns published content of the same post type and language
// that shares at least one tag with the given tags, excluding excludeID. Results
// are ranked by the number of shared tags (descending) then by created_at
// (descending). An empty tags slice yields no rows.
GetRelatedByTags(ctx context.Context, excludeID int, tags []string, postType string, language string, limit int) ([]*contentdomain.Content, error)
// GetLatestByPostType returns the most recently created published content of the
// given post type and language, excluding excludeID, ordered by created_at desc.
GetLatestByPostType(ctx context.Context, excludeID int, postType string, language string, limit int) ([]*contentdomain.Content, error)
}
Repository defines the interface for content repository operations
type RoleChecker ¶ added in v0.10.0
type RoleChecker interface {
IsAdmin(role string) bool
CanManageType(role string, postType string) bool
CanPublish(role string) bool
}
RoleChecker reports role capabilities for content authorization. It is optional: a Service built without one keeps the legacy behavior (owners may manage their own content, Admin bypasses ownership, and publish is unrestricted for owners/admins). *role.Service implements this interface.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service handles content business logic
func NewService ¶
func NewService( repo Repository, seoService *seo.Service, postTypeService PostTypeServiceInterface, opts ...Option, ) *Service
func NewServiceWithHooks ¶
func NewServiceWithHooks( repo Repository, seoService *seo.Service, postTypeService PostTypeServiceInterface, hookExecutor HookExecutor, opts ...Option, ) *Service
func (*Service) AuthorExists ¶
func (*Service) Count ¶ added in v0.8.0
func (s *Service) Count(ctx context.Context, userID int, filters contentdomain.ContentFilters) (int, error)
Count returns the number of content items matching the given filters, sharing the same validation (sort order, custom field filters) as ListByFilters. userID <= 0 counts across all users (admin scope).
func (*Service) Create ¶
func (s *Service) Create(ctx context.Context, principal kernel.Principal, req contentdomain.CreateContentRequest) (*contentdomain.Content, error)
func (*Service) DeleteContent ¶
func (*Service) GenerateSlug ¶
func (*Service) GenerateSlugFromTitle ¶
func (*Service) GetBySlugAndLanguage ¶ added in v0.8.0
func (s *Service) GetBySlugAndLanguage(ctx context.Context, slug string, language string) (*contentdomain.Content, error)
GetBySlugAndLanguage retrieves a content item by slug in the given language. It returns contentdomain.ErrContentNotFound when the slug does not exist in that language. Importers use it to resolve the ID of an already-imported item so translated variants can still link to it on idempotent re-runs.
func (*Service) GetPublished ¶
func (*Service) GetPublishedArchive ¶ added in v0.6.0
func (s *Service) GetPublishedArchive(ctx context.Context, postType string, languages []string) ([]*contentdomain.ArchiveMonth, error)
func (*Service) GetPublishedAuthor ¶ added in v0.7.0
func (s *Service) GetPublishedAuthor(ctx context.Context, username string) (*contentdomain.PublishedAuthor, error)
func (*Service) GetPublishedAuthors ¶ added in v0.5.0
func (s *Service) GetPublishedAuthors(ctx context.Context, filters contentdomain.PublishedAuthorFilters) ([]*contentdomain.PublishedAuthor, error)
func (*Service) GetPublishedByAuthorUsername ¶
func (*Service) GetPublishedByID ¶
func (*Service) GetPublishedByPostType ¶
func (*Service) GetPublishedBySlug ¶
func (*Service) GetPublishedBySlugAny ¶
func (*Service) GetPublishedByTag ¶
func (*Service) GetPublishedCustomPostTypes ¶
func (*Service) GetPublishedPages ¶
func (*Service) GetPublishedTags ¶ added in v0.5.0
func (*Service) GetRelated ¶ added in v0.4.0
func (s *Service) GetRelated(ctx context.Context, id int, limit int) ([]*contentdomain.Content, error)
GetRelated returns up to limit related posts for the content identified by id. Related posts share at least one tag, the same post type, and the same language as the source, ranked by the number of shared tags then by recency. When there are not enough tag-overlap matches (or the source has no tags), the result is backfilled with the latest published posts of the same post type and language. The source post is always excluded.
func (*Service) GetTranslations ¶
func (*Service) ListByCursor ¶
func (s *Service) ListByCursor(ctx context.Context, userID int, limit int, beforeID int, filters contentdomain.ContentFilters) ([]*contentdomain.Content, error)
func (*Service) ListByFilters ¶
func (s *Service) ListByFilters(ctx context.Context, userID int, filters contentdomain.ContentFilters) ([]*contentdomain.Content, error)
func (*Service) Publish ¶
func (s *Service) Publish(ctx context.Context, id int, principal kernel.Principal) (*contentdomain.Content, error)
Publish flips a content item's status to contentdomain.StatusPublished via transitionStatus. Ownership/role checks mirror Update: a non-admin caller must be the owner or the service returns contentdomain.ErrUnauthorized. An already-published post is a no-op (200 idempotent: the row is persisted unchanged, no hook fires, no SEO regen runs) — only the actual draft→published edge triggers SEO + hook work.
Errors:
- contentdomain.ErrContentNotFound when the id does not exist
- contentdomain.ErrUnauthorized when the caller is neither owner nor admin
- wrapped repo / SEO errors on persistence / generation failure
func (*Service) SearchPublished ¶
func (*Service) SetSystemFields ¶
func (*Service) SlugExists ¶ added in v0.8.0
SlugExists reports whether a content item with the given slug already exists in the given language. Importers use it for idempotent re-runs — items whose slug already exists are skipped instead of failing or duplicating.
func (*Service) Unpublish ¶
func (s *Service) Unpublish(ctx context.Context, id int, principal kernel.Principal) (*contentdomain.Content, error)
Unpublish flips a content item's status to contentdomain.StatusDraft via transitionStatus. Same ownership/role contract as Publish. An already-draft post is a no-op (idempotent: row is persisted unchanged, no hook fires, no SEO regen runs).
Errors:
- contentdomain.ErrContentNotFound when the id does not exist
- contentdomain.ErrUnauthorized when the caller is neither owner nor admin
- wrapped repo errors on persistence failure
type UpdateContentRequest ¶
type UpdateContentRequest struct {
Title string `json:"title"`
Slug string `json:"slug,omitempty"`
Content string `json:"content"`
Tags []string `json:"tags"`
Status contentdomain.Status `json:"status"`
Format contentdomain.Format `json:"format,omitempty"`
PostType string `json:"postType"`
MetaDescription string `json:"metaDescription,omitempty"`
OGTitle string `json:"ogTitle,omitempty"`
OGDescription string `json:"ogDescription,omitempty"`
FeaturedImage *string `json:"featuredImage,omitempty"`
AllowComments *bool `json:"allowComments,omitempty"`
CustomFields map[string]any `json:"customFields,omitempty"`
Language string `json:"language,omitempty"`
TranslationGroupID *int `json:"translationGroupId,omitempty"`
}
UpdateContentRequest represents a request to update content