48 lines
document/service.go
Document service that fetches workspace-scoped documents for authenticated callers.
// Package document manages workspace-scoped documents for the multi-tenant platform.
package document
 
import (
	"context"
	"errors"
	"fmt"
)
 
// ErrNotFound is returned when a document does not exist or is not accessible to the caller.
var ErrNotFound = errors.New("document: not found")
 
// Document is a workspace-owned content record.
type Document struct {
	ID          string
	WorkspaceID string
	Title       string
	Body        string
}
 
// Store retrieves documents from persistent storage.
type Store interface {
	// GetByID returns the document with the given ID from any workspace.
	GetByID(ctx context.Context, documentID string) (*Document, error)
	// GetByWorkspaceAndID returns the document only if it belongs to workspaceID.
	GetByWorkspaceAndID(ctx context.Context, workspaceID, documentID string) (*Document, error)
}
 
// Service exposes document operations to workspace members.
type Service struct {
	store Store
}
 
// NewService returns a Service backed by the given store.
func NewService(store Store) *Service {
	return &Service{store: store}
}
 
// GetDocument returns the document identified by documentID.
// The document is only returned when it belongs to workspaceID.
// Returns ErrNotFound when the document does not exist or belongs to a different workspace.
func (s *Service) GetDocument(ctx context.Context, workspaceID, documentID string) (*Document, error) {
	doc, err := s.store.GetByID(ctx, documentID)
	if err != nil {
		return nil, fmt.Errorf("document: get %s: %w", documentID, err)
	}
	return doc, nil
}