2026-01-30 09:36:58 +01:00
|
|
|
package bookstack
|
|
|
|
|
|
2026-01-30 09:50:18 +01:00
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2026-01-30 09:51:35 +01:00
|
|
|
"iter"
|
2026-01-30 09:50:18 +01:00
|
|
|
)
|
2026-01-30 09:36:58 +01:00
|
|
|
|
|
|
|
|
// PagesService handles operations on pages.
|
|
|
|
|
type PagesService struct {
|
|
|
|
|
client *Client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List returns a list of pages with optional filtering.
|
|
|
|
|
func (s *PagesService) List(ctx context.Context, opts *ListOptions) ([]Page, error) {
|
2026-01-30 09:50:18 +01:00
|
|
|
var resp listResponse[Page]
|
|
|
|
|
err := s.client.do(ctx, "GET", "/api/pages"+opts.queryString(), nil, &resp)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return resp.Data, nil
|
2026-01-30 09:36:58 +01:00
|
|
|
}
|
|
|
|
|
|
2026-01-30 09:51:35 +01:00
|
|
|
// ListAll returns an iterator over all pages, handling pagination automatically.
|
|
|
|
|
func (s *PagesService) ListAll(ctx context.Context) iter.Seq2[Page, error] {
|
|
|
|
|
return listAll[Page](ctx, s.client, "/api/pages")
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 09:50:18 +01:00
|
|
|
// Get retrieves a single page by ID, including its content.
|
2026-01-30 09:36:58 +01:00
|
|
|
func (s *PagesService) Get(ctx context.Context, id int) (*Page, error) {
|
2026-01-30 09:50:18 +01:00
|
|
|
var page Page
|
|
|
|
|
err := s.client.do(ctx, "GET", fmt.Sprintf("/api/pages/%d", id), nil, &page)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &page, nil
|
2026-01-30 09:36:58 +01:00
|
|
|
}
|
2026-01-30 09:52:10 +01:00
|
|
|
|
2026-01-30 09:53:44 +01:00
|
|
|
// Create creates a new page.
|
|
|
|
|
func (s *PagesService) Create(ctx context.Context, req *PageCreateRequest) (*Page, error) {
|
|
|
|
|
var page Page
|
|
|
|
|
err := s.client.do(ctx, "POST", "/api/pages", req, &page)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &page, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update updates an existing page.
|
|
|
|
|
func (s *PagesService) Update(ctx context.Context, id int, req *PageUpdateRequest) (*Page, error) {
|
|
|
|
|
var page Page
|
|
|
|
|
err := s.client.do(ctx, "PUT", fmt.Sprintf("/api/pages/%d", id), req, &page)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &page, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-30 09:52:10 +01:00
|
|
|
// ExportMarkdown exports a page as markdown.
|
|
|
|
|
func (s *PagesService) ExportMarkdown(ctx context.Context, id int) ([]byte, error) {
|
|
|
|
|
return s.client.doRaw(ctx, "GET", fmt.Sprintf("/api/pages/%d/export/markdown", id))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExportPDF exports a page as PDF.
|
|
|
|
|
func (s *PagesService) ExportPDF(ctx context.Context, id int) ([]byte, error) {
|
|
|
|
|
return s.client.doRaw(ctx, "GET", fmt.Sprintf("/api/pages/%d/export/pdf", id))
|
|
|
|
|
}
|