-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtag.go
117 lines (88 loc) · 1.99 KB
/
newtag.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
package models
import (
"errors"
"github.com/microcosm-cc/bluemonday"
"html"
"github.com/eirka/eirka-libs/config"
"github.com/eirka/eirka-libs/db"
e "github.com/eirka/eirka-libs/errors"
"github.com/eirka/eirka-libs/validate"
)
type NewTagModel struct {
Ib uint
Tag string
TagType uint
}
// check struct validity
func (n *NewTagModel) IsValid() bool {
if n.Ib == 0 {
return false
}
if n.Tag == "" {
return false
}
if n.TagType == 0 {
return false
}
return true
}
// ValidateInput will make sure all the parameters are valid
func (i *NewTagModel) ValidateInput() (err error) {
if i.Ib == 0 {
return e.ErrInvalidParam
}
if i.TagType == 0 {
return e.ErrInvalidParam
}
// Initialize bluemonday
p := bluemonday.StrictPolicy()
// sanitize for html and xss
i.Tag = html.UnescapeString(p.Sanitize(i.Tag))
// Validate name input
tag := validate.Validate{Input: i.Tag, Max: config.Settings.Limits.TagMaxLength, Min: config.Settings.Limits.TagMinLength}
if tag.IsEmpty() {
return e.ErrNoTagName
} else if tag.MinPartsLength() {
return e.ErrTagShort
} else if tag.MaxLength() {
return e.ErrTagLong
}
return
}
// Status will return info about the thread
func (i *NewTagModel) Status() (err error) {
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
var check bool
// Check if tag is already there
err = dbase.QueryRow("select count(1) from tags where ib_id = ? AND tag_name = ?", i.Ib, i.Tag).Scan(&check)
if err != nil {
return
}
// return if it does
if check {
return e.ErrDuplicateTag
}
return
}
// Post will add the reply to the database with a transaction
func (i *NewTagModel) Post() (err error) {
// check model validity
if !i.IsValid() {
return errors.New("NewTagModel is not valid")
}
// Get Database handle
dbase, err := db.GetDb()
if err != nil {
return
}
_, err = dbase.Exec("INSERT into tags (tag_name,ib_id,tagtype_id) VALUES (?,?,?)",
i.Tag, i.Ib, i.TagType)
if err != nil {
return
}
return
}