topic

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package topic manages notification topics owned by users.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTopicNotFound     = errors.New("topic not found")
	ErrTopicProtected    = errors.New("cannot delete the protected 'general' topic")
	ErrTopicNameReserved = errors.New("topic name 'general' is reserved")
	ErrTopicNameConflict = errors.New("a topic with this name already exists")
	ErrDuplicateTopicIDs = errors.New("duplicate topic IDs are not allowed")
)

Sentinel errors — used by service, matched by handler via errors.Is.

Functions

This section is empty.

Types

type CreateTopicRequest

type CreateTopicRequest struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

CreateTopicRequest is the request body for POST /topics.

func (*CreateTopicRequest) Validate

func (r *CreateTopicRequest) Validate() []error

Validate validates the create topic request fields.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler handles topic HTTP requests.

func NewHandler

func NewHandler(topicSvc *Service, logger *slog.Logger) *Handler

NewHandler creates a new topic handler.

func (*Handler) CreateTopic

func (h *Handler) CreateTopic(w http.ResponseWriter, r *http.Request)

CreateTopic handles POST requests to create a topic.

func (*Handler) DeleteTopic

func (h *Handler) DeleteTopic(w http.ResponseWriter, r *http.Request)

DeleteTopic handles DELETE requests to delete a topic.

func (*Handler) GetTopics

func (h *Handler) GetTopics(w http.ResponseWriter, r *http.Request)

GetTopics handles GET requests for topics.

func (*Handler) UpdateTopic

func (h *Handler) UpdateTopic(w http.ResponseWriter, r *http.Request)

UpdateTopic handles PATCH requests to update a topic's description.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository provides data access for the topic domain.

func NewRepository

func NewRepository(db *sqlx.DB) *Repository

NewRepository creates a new topic repository.

func (*Repository) CountOwnedByUser

func (r *Repository) CountOwnedByUser(ctx context.Context, userID string, topicIDs []string) (int, error)

CountOwnedByUser returns how many of the provided topic IDs belong to the user.

func (*Repository) Create

func (r *Repository) Create(ctx context.Context, userID, name, description string) (*Topic, error)

Create creates a new topic.

func (*Repository) Delete

func (r *Repository) Delete(ctx context.Context, userID, topicID string) error

Delete deletes a topic by ID and user.

func (*Repository) GetByID

func (r *Repository) GetByID(ctx context.Context, userID, topicID string) (*Topic, error)

GetByID retrieves a topic by ID and user.

func (*Repository) GetByName

func (r *Repository) GetByName(ctx context.Context, userID, name string) (*Topic, error)

GetByName retrieves a topic by user and name.

func (*Repository) GetByUser

func (r *Repository) GetByUser(ctx context.Context, userID string) ([]Topic, error)

GetByUser retrieves all topics for a user.

func (*Repository) Update

func (r *Repository) Update(ctx context.Context, userID, topicID, description string) error

Update updates a topic's description.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service provides topic business logic.

func NewService

func NewService(repo *Repository, logger *slog.Logger) *Service

NewService creates a new topic service.

func (*Service) CreateDefaultTopic

func (s *Service) CreateDefaultTopic(ctx context.Context, userID string) error

CreateDefaultTopic creates the default "general" topic for a new user.

func (*Service) CreateTopic

func (s *Service) CreateTopic(ctx context.Context, userID, name, description string) (*Topic, error)

CreateTopic creates a new topic after validating the name.

func (*Service) DeleteTopic

func (s *Service) DeleteTopic(ctx context.Context, userID, topicID string) error

DeleteTopic deletes a topic, enforcing protection of the "general" topic.

func (*Service) GetByName

func (s *Service) GetByName(ctx context.Context, userID, name string) (*Topic, error)

GetByName retrieves a topic by name.

func (*Service) GetTopics

func (s *Service) GetTopics(ctx context.Context, userID string) ([]Topic, error)

GetTopics retrieves all topics for a user.

func (*Service) UpdateTopic

func (s *Service) UpdateTopic(ctx context.Context, userID, topicID, description string) error

UpdateTopic updates a topic's description.

func (*Service) ValidateTopicIDs

func (s *Service) ValidateTopicIDs(ctx context.Context, userID string, topicIDs []string) error

ValidateTopicIDs verifies that all provided topic IDs belong to the given user.

type Topic

type Topic struct {
	ID          string  `db:"id"`
	UserID      string  `db:"user_id"`
	Name        string  `db:"name"`
	Description *string `db:"description"`
	CreatedAt   int64   `db:"created_at"`
	UpdatedAt   int64   `db:"updated_at"`
}

Topic is the DB struct — db tags only, no json tags.

type TopicResponse

type TopicResponse struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Description *string   `json:"description,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

TopicResponse is the HTTP response struct — json tags only.

func ToTopicResponse

func ToTopicResponse(t *Topic) TopicResponse

ToTopicResponse converts a Topic DB struct to a TopicResponse.

type TopicsListResponse

type TopicsListResponse struct {
	Data []TopicResponse `json:"data"`
}

TopicsListResponse wraps a collection for future pagination.

func ToTopicsListResponse

func ToTopicsListResponse(topics []Topic) TopicsListResponse

ToTopicsListResponse converts a slice of Topics to TopicsListResponse.

type UpdateTopicRequest

type UpdateTopicRequest struct {
	Description string `json:"description"`
}

UpdateTopicRequest updates a topic's description.

func (*UpdateTopicRequest) Validate

func (r *UpdateTopicRequest) Validate() []error

Validate validates the update topic request fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL