Repository navigation
Expand file tree
/
Copy pathrecycle_bin.go
More file actions
169 lines (135 loc) · 4.45 KB
/
Copy pathrecycle_bin.go
File metadata and controls
169 lines (135 loc) · 4.45 KB
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
package bookstack
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
type RecycleBinItem struct {
ID int `json:"id,omitempty"`
DeletedBy int `json:"deleted_by,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
DeletableType ContentType `json:"deletable_type,omitempty"`
DeletableID int `json:"deletable_id,omitempty"`
Deletable json.RawMessage `json:"deletable,omitempty"`
}
type RecycledBook struct {
Book
PagesCount int `json:"pages_count,omitempty"`
ChaptersCount int `json:"chapters_count,omitempty"`
}
type RecycledPage 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"`
OwnedBy int `json:"owned_by,omitempty"`
Editor string `json:"editor,omitempty"`
BookSlug string `json:"book_slug,omitempty"`
Parent Parent `json:"parent,omitempty"`
}
type RecycledChapter struct {
Chapter
BookSlug string `json:"book_slug,omitempty"`
PagesCount int `json:"pages_count,omitempty"`
Parent Parent `json:"parent,omitempty"`
}
type Parent struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Description string `json:"description,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"`
OwnedBy int `json:"owned_by,omitempty"`
Type string `json:"type,omitempty"`
}
func (i RecycleBinItem) Book() (*RecycledBook, bool) {
result, err := ParseSingle[RecycledBook]([]byte(i.Deletable))
if err != nil {
return nil, false
}
return &result, true
}
func (i RecycleBinItem) Chapter() (*RecycledChapter, bool) {
result, err := ParseSingle[RecycledChapter]([]byte(i.Deletable))
if err != nil {
return nil, false
}
return &result, true
}
func (i RecycleBinItem) Shelf() (*Shelf, bool) {
result, err := ParseSingle[Shelf]([]byte(i.Deletable))
if err != nil {
return nil, false
}
return &result, true
}
func (i RecycleBinItem) Page() (*RecycledPage, bool) {
result, err := ParseSingle[RecycledPage]([]byte(i.Deletable))
if err != nil {
return nil, false
}
return &result, true
}
type ContentType string
const (
ContentBook ContentType = "book"
ContentChapter ContentType = "chapter"
ContentShelf ContentType = "bookshelf"
ContentAttachment ContentType = "attachment"
ContentPage ContentType = "page"
)
// ListRecycleBinItems will list the items in the recycle bin.
func (b *Bookstack) ListRecycleBinItems(ctx context.Context) ([]RecycleBinItem, error) {
raw, err := b.request(ctx, http.MethodGet, "/recycle-bin", blank{})
if err != nil {
return nil, err
}
list, err := ParseMultiple[[]RecycleBinItem](raw)
if err != nil {
return nil, err
}
return list, nil
}
// RestoreRecycleBinItem will restore an item from the recycle bin.
func (b *Bookstack) RestoreRecyleBinItem(ctx context.Context, id int) (int, error) {
raw, err := b.request(ctx, http.MethodPut, fmt.Sprintf("/recycle-bin/%d", id), blank{})
if err != nil {
return 0, err
}
resp := struct {
Count int `json:"restore_count"`
}{}
if err := json.Unmarshal(raw, &resp); err != nil {
return 0, err
}
return resp.Count, nil
}
// DeleteRecycleBinItem will delete an item from the recycle bin.
func (b *Bookstack) DeleteRecycleBinItem(ctx context.Context, id int) (int, error) {
raw, err := b.request(ctx, http.MethodDelete, fmt.Sprintf("/recycle-bin/%d", id), blank{})
if err != nil {
return 0, err
}
resp := struct {
Count int `json:"delete_count"`
}{}
if err := json.Unmarshal(raw, &resp); err != nil {
return 0, err
}
return resp.Count, nil
}