-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
116 lines (95 loc) · 2.35 KB
/
page.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
package model
import (
"html/template"
"cloud.google.com/go/firestore"
"github.com/quandaodev/cherry/utils"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
)
// PageDB is a database struct for Page
type PageDB struct {
ID string `firestore:"id,omitempty"`
Content string `firestore:"content,omitempty"`
HTML string `firestore:"html,omitempty"`
}
// Page is a display struct for Page
type Page struct {
ID string
Content string
HTML template.HTML
}
func convertPageDBToPage(pdb PageDB) (p Page) {
p.ID = pdb.ID
p.HTML = template.HTML(pdb.HTML)
p.Content = pdb.Content
return
}
// ListPages list all pages in the database
func ListPages() (pages []Page, err error) {
utils.LogInfo("ListPages() called")
ctx := context.Background()
client := getDBClient()
iter := client.Collection("pages").Documents(ctx)
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
utils.LogError("Failed to iterate: ", err)
}
var pdb PageDB
doc.DataTo(&pdb)
pages = append(pages, convertPageDBToPage(pdb))
}
return
}
// GetPageByID return an article matching the id
func GetPageByID(id string) (p Page, err error) {
utils.LogInfo("GetPageById() called")
ctx := context.Background()
client := getDBClient()
dsnap, _ := client.Collection("pages").Doc(id).Get(ctx)
var pdb PageDB
dsnap.DataTo(&pdb)
p = convertPageDBToPage(pdb)
return
}
// CreatePage inserts a new page to the database
func CreatePage(p PageDB) (err error) {
utils.LogInfo("CreatePage() called")
ctx := context.Background()
client := getDBClient()
_, _, err = client.Collection("pages").Add(ctx, p)
if err != nil {
utils.LogError("Failed to create page: %v", err)
}
return
}
// UpdatePage update a page exists in the database
func UpdatePage(p PageDB) (err error) {
utils.LogInfo("UpdatePage() called")
ctx := context.Background()
client := getDBClient()
pts := client.Collection("pages")
q := pts.Where("id", "==", p.ID)
iter := q.Documents(ctx)
defer iter.Stop()
for {
doc, err := iter.Next()
if err == iterator.Done {
break
}
if err != nil {
utils.LogError("Failed to iterate: ", err)
}
_, err = doc.Ref.Set(ctx, map[string]interface{}{
"content": p.Content,
"html": p.HTML}, firestore.MergeAll)
}
if err != nil {
utils.LogError("Failed to update page: ", err)
}
return
}