Documentation
¶
Overview ¶
Package topic manages notification topics owned by users.
Index ¶
- Variables
- type CreateTopicRequest
- type Handler
- type Repository
- func (r *Repository) CountOwnedByUser(ctx context.Context, userID string, topicIDs []string) (int, error)
- func (r *Repository) Create(ctx context.Context, userID, name, description string) (*Topic, error)
- func (r *Repository) Delete(ctx context.Context, userID, topicID string) error
- func (r *Repository) GetByID(ctx context.Context, userID, topicID string) (*Topic, error)
- func (r *Repository) GetByName(ctx context.Context, userID, name string) (*Topic, error)
- func (r *Repository) GetByUser(ctx context.Context, userID string) ([]Topic, error)
- func (r *Repository) Update(ctx context.Context, userID, topicID, description string) error
- type Service
- func (s *Service) CreateDefaultTopic(ctx context.Context, userID string) error
- func (s *Service) CreateTopic(ctx context.Context, userID, name, description string) (*Topic, error)
- func (s *Service) DeleteTopic(ctx context.Context, userID, topicID string) error
- func (s *Service) GetByName(ctx context.Context, userID, name string) (*Topic, error)
- func (s *Service) GetTopics(ctx context.Context, userID string) ([]Topic, error)
- func (s *Service) UpdateTopic(ctx context.Context, userID, topicID, description string) error
- func (s *Service) ValidateTopicIDs(ctx context.Context, userID string, topicIDs []string) error
- type Topic
- type TopicResponse
- type TopicsListResponse
- type UpdateTopicRequest
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) Delete ¶
func (r *Repository) Delete(ctx context.Context, userID, topicID string) error
Delete deletes a topic by ID and user.
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 ¶
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 ¶
DeleteTopic deletes a topic, enforcing protection of the "general" topic.
func (*Service) UpdateTopic ¶
UpdateTopic updates a topic's description.
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.