This repository has been archived by the owner on Sep 1, 2023. It is now read-only.
/
chapters.go
177 lines (137 loc) · 5.36 KB
/
chapters.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package bookstack
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
type Chapter struct {
ID int `json:"id,omitempty"`
BookID int `json:"book_id,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Description string `json:"description,omitempty"`
Priority int `json:"priority,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
CreatedBy int `json:"created_by,omitempty"`
UpdatedBy int `json:"updated_by,omitempty"`
OwnedBy int `json:"owned_by,omitempty"`
}
type ChapterDetailed struct {
ID int `json:"id,omitempty"`
BookID int `json:"book_id,omitempty"`
Slug string `json:"slug,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Priority int `json:"priority,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
CreatedBy CreatedBy `json:"created_by,omitempty"`
UpdatedBy UpdatedBy `json:"updated_by,omitempty"`
OwnedBy OwnedBy `json:"owned_by,omitempty"`
Tags []Tag `json:"tags,omitempty"`
Pages []ChapterPage `json:"pages,omitempty"`
}
type ChapterPage struct {
ID int `json:"id,omitempty"`
BookID int `json:"book_id,omitempty"`
ChapterID int `json:"chapter_id,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Priority int `json:"priority,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
CreatedBy int `json:"created_by,omitempty"`
UpdatedBy int `json:"updated_by,omitempty"`
Draft bool `json:"draft,omitempty"`
RevisionCount int `json:"revision_count,omitempty"`
Template bool `json:"template,omitempty"`
}
type ChapterParams struct {
BookID int `json:"book_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Tags []TagParams `json:"tags,omitempty"`
}
func (bp ChapterParams) Form() (string, io.Reader, error) {
r, err := json.Marshal(bp)
if err != nil {
return "", nil, err
}
return appJSON, bytes.NewReader(r), nil
}
// ListChapters will return the chapters that match the given params.
func (b *Bookstack) ListChapters(ctx context.Context, params *QueryParams) ([]Chapter, error) {
resp, err := b.request(ctx, http.MethodGet, params.String("/chapters"), blank{})
if err != nil {
return nil, err
}
return ParseMultiple[[]Chapter](resp)
}
// GetChapter will return a single chapter that matches id.
func (b *Bookstack) GetChapter(ctx context.Context, id int) (ChapterDetailed, error) {
resp, err := b.request(ctx, http.MethodGet, fmt.Sprintf("/chapters/%d", id), blank{})
if err != nil {
return ChapterDetailed{}, err
}
return ParseSingle[ChapterDetailed](resp)
}
// CreateChapter will create a chapter according to the given params.
func (b *Bookstack) CreateChapter(ctx context.Context, params ChapterParams) (Chapter, error) {
resp, err := b.request(ctx, http.MethodPost, "/chapters", params)
if err != nil {
return Chapter{}, err
}
return ParseSingle[Chapter](resp)
}
// UpdateChapter will update a chapter with the given params.
func (b *Bookstack) UpdateChapter(ctx context.Context, id int, params ChapterParams) (Chapter, error) {
resp, err := b.request(ctx, http.MethodPut, fmt.Sprintf("/chapters/%d", id), params)
if err != nil {
return Chapter{}, err
}
return ParseSingle[Chapter](resp)
}
// DeleteChapter will delete a chapter with the given id.
func (b *Bookstack) DeleteChapter(ctx context.Context, id int) (bool, error) {
if _, err := b.request(ctx, http.MethodDelete, fmt.Sprintf("/chapters/%d", id), blank{}); err != nil {
return false, err
}
return true, nil
}
// ExportChapterHTML will return a chapter in HTML format.
func (b *Bookstack) ExportChapterHTML(ctx context.Context, id int) (io.Reader, error) {
data, err := b.request(ctx, http.MethodGet, fmt.Sprintf("/chapters/%d/export/html", id), blank{})
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
// ExportChapterPDF will return a chapter in PDF format.
func (b *Bookstack) ExportChapterPDF(ctx context.Context, id int) (io.Reader, error) {
data, err := b.request(ctx, http.MethodGet, fmt.Sprintf("/chapters/%d/export/pdf", id), blank{})
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
// ExportChapterMarkdown will return a chapter in Markdown format.
func (b *Bookstack) ExportChapterMarkdown(ctx context.Context, id int) (io.Reader, error) {
data, err := b.request(ctx, http.MethodGet, fmt.Sprintf("/chapters/%d/export/markdown", id), blank{})
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}
// ExportChapterPlaintext will return a chapter in Plaintext format.
func (b *Bookstack) ExportChapterPlaintext(ctx context.Context, id int) (io.Reader, error) {
data, err := b.request(ctx, http.MethodGet, fmt.Sprintf("/chapters/%d/export/plaintext", id), blank{})
if err != nil {
return nil, err
}
return bytes.NewReader(data), nil
}