-
Notifications
You must be signed in to change notification settings - Fork 0
/
newtag.go
119 lines (89 loc) · 2.04 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
118
119
package models
import (
"errors"
"html"
"github.com/microcosm-cc/bluemonday"
"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"
)
// NewTagModel holds the request input
type NewTagModel struct {
Ib uint
Tag string
TagType uint
}
// IsValid will check struct validity
func (m *NewTagModel) IsValid() bool {
if m.Ib == 0 {
return false
}
if m.Tag == "" {
return false
}
if m.TagType == 0 {
return false
}
return true
}
// ValidateInput will make sure all the parameters are valid
func (m *NewTagModel) ValidateInput() (err error) {
if m.Ib == 0 {
return e.ErrInvalidParam
}
if m.TagType == 0 {
return e.ErrInvalidParam
}
// Initialize bluemonday
p := bluemonday.StrictPolicy()
// sanitize for html and xss
m.Tag = html.UnescapeString(p.Sanitize(m.Tag))
// Validate name input
tag := validate.Validate{Input: m.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 (m *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 = ?", m.Ib, m.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 (m *NewTagModel) Post() (err error) {
// check model validity
if !m.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 (?,?,?)",
m.Tag, m.Ib, m.TagType)
if err != nil {
return
}
return
}