forked from qor5/admin
/
helper.go
146 lines (125 loc) · 3.34 KB
/
helper.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
package pagebuilder
import (
"path"
"regexp"
"github.com/qor5/web"
"gorm.io/gorm"
)
var (
pathRe = regexp.MustCompile(`^/[0-9a-zA-Z-_().\/]*$`)
slugWithCategoryRe = regexp.MustCompile(`^/[0-9a-zA-Z-_().]*$`)
)
const (
queryPathWithSlugSQL = `
SELECT pages.id, pages.version, categories.path || pages.slug AS path_with_slug
FROM page_builder_pages pages
LEFT JOIN page_builder_categories categories ON category_id = categories.id
WHERE pages.deleted_at IS NULL AND categories.deleted_at IS NULL
`
missingCategoryOrSlugMsg = "Category or Slug is required"
invalidSlugMsg = "The slug must start with a '/' followed by one or more characters"
invalidSlugWithCategoryMsg = "The slug must start with a '/' followed by one or more characters excluding '/'"
invalidPathMsg = "The path must start with a '/' followed by one or more characters"
conflictSlugMsg = "Conflicting Slug"
conflictPathMsg = "Conflicting Path"
existingPathMsg = "Existing Path"
unableDeleteCategoryMsg = "this category cannot be deleted because it has used with pages"
)
func pageValidator(p *Page, db *gorm.DB) (err web.ValidationErrors) {
if p.CategoryID == 0 && p.Slug == "" {
err.FieldError("Page.Category", missingCategoryOrSlugMsg)
err.FieldError("Page.Slug", missingCategoryOrSlugMsg)
return
}
if p.CategoryID == 0 {
s := path.Clean(p.Slug)
if s != "" && !pathRe.MatchString(s) {
err.FieldError("Page.Slug", invalidSlugMsg)
return
}
} else {
if p.Slug != "" {
s := path.Clean(p.Slug)
if !slugWithCategoryRe.MatchString(s) {
err.FieldError("Page.Slug", invalidSlugWithCategoryMsg)
return
}
}
}
categories := []*Category{}
if err := db.Model(&Category{}).Find(&categories).Error; err != nil {
panic(err)
}
var c Category
for _, e := range categories {
if e.ID == p.CategoryID {
c = *e
break
}
}
urlPath := c.Path + p.Slug
type result struct {
ID uint
Version string
PathWithSlug string
}
var results []result
if err := db.Raw(queryPathWithSlugSQL).Scan(&results).Error; err != nil {
panic(err)
}
for _, r := range results {
if r.ID == p.ID {
continue
}
if r.PathWithSlug == urlPath {
err.FieldError("Page.Slug", conflictSlugMsg)
return
}
}
if p.Slug != "" {
for _, e := range categories {
if e.Path == urlPath {
err.FieldError("Page.Slug", conflictSlugMsg)
return
}
}
}
return
}
func categoryValidator(category *Category, db *gorm.DB) (err web.ValidationErrors) {
p := path.Clean(category.Path)
if p != "" && !pathRe.MatchString(p) {
err.FieldError("Category.Category", invalidPathMsg)
return
}
// Verify category does not conflict the category with slug.
type result struct {
ID uint
PathWithSlug string
}
var results []result
if err := db.Raw(queryPathWithSlugSQL).Scan(&results).Error; err != nil {
panic(err)
}
for _, r := range results {
if r.PathWithSlug == p {
err.FieldError("Category.Category", conflictPathMsg)
return
}
}
// Verify category not duplicate.
categories := []*Category{}
if err := db.Model(&Category{}).Find(&categories).Error; err != nil {
panic(err)
}
for _, c := range categories {
if c.ID == category.ID {
continue
}
if c.Path == p {
err.FieldError("Category.Category", existingPathMsg)
return
}
}
return
}