-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelstermsModel.go
176 lines (142 loc) · 4.45 KB
/
ModelstermsModel.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package tags
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/go-bolo/bolo"
"github.com/go-bolo/bolo/helpers"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// ModelstermsModel - Stores terms associations with other models
type ModelstermsModel struct {
ID uint64 `gorm:"primaryKey;column:id" json:"id"`
ModelName string `gorm:"index:modelsterms_modelName_IDX;index:modelName_modelId;column:modelName;type:varchar(255);not null" json:"modelName"`
ModelID uint64 `gorm:"index:modelName_modelId;column:modelId;type:int(11);not null" json:"modelId"`
Field string `gorm:"index:modelsterms_modelName_IDX;column:field;type:varchar(255);not null" json:"field"`
IsTag string `gorm:"column:isTag;type:varchar(255)" json:"isTag"`
Order int `gorm:"column:order;type:tinyint(1);default:0" json:"order"`
VocabularyName string `gorm:"column:vocabularyName;type:varchar(255);not null;default:Tags" json:"vocabularyName"`
CreatedAt time.Time `gorm:"column:createdAt" json:"createdAt"`
UpdatedAt time.Time `gorm:"column:updatedAt" json:"updatedAt"`
TermID *uint64 `gorm:"column:termId;type:int(11)" json:"termId"`
}
func NewModelsterms(vocabularyName, modelName, field string, modelId, termId uint64) (ModelstermsModel, error) {
r := ModelstermsModel{
VocabularyName: vocabularyName,
ModelName: modelName,
ModelID: modelId,
Field: field,
TermID: &termId,
}
return r, nil
}
// TableName get sql table name
func (m *ModelstermsModel) TableName() string {
return "modelsterms"
}
func (r *ModelstermsModel) GetIDString() string {
return strconv.FormatUint(r.ID, 10)
}
func (r *ModelstermsModel) GetModelIDString() string {
return strconv.FormatUint(r.ModelID, 10)
}
func (r *ModelstermsModel) ToJSON() string {
jsonString, _ := json.MarshalIndent(r, "", " ")
return string(jsonString)
}
// Save - Create if is new or update
func (m *ModelstermsModel) Save() error {
var err error
db := bolo.GetDefaultDatabaseConnection()
if m.ID == 0 {
// create ....
err = db.Create(&m).Error
if err != nil {
return err
}
} else {
// update ...
err = db.Save(&m).Error
if err != nil {
return err
}
}
return nil
}
func (r *ModelstermsModel) RenderRelatedRecord(ctx *bolo.RequestContext, app bolo.App) (bytes.Buffer, error) {
tPlugin := app.GetPlugin("taxonomy").(*Plugin)
return tPlugin.RenderRelatedRecord(r, ctx)
}
func (r *ModelstermsModel) Delete() error {
db := bolo.GetDefaultDatabaseConnection()
return db.Unscoped().Delete(&r).Error
}
type ModelstermQueryOpts struct {
Records *[]ModelstermsModel
Count *int64
Limit int
Offset int
C echo.Context
IsHTML bool
}
func ModelstermQueryAndCountReq(opts *ModelstermQueryOpts) error {
db := bolo.GetDefaultDatabaseConnection()
c := opts.C
vocabularyName := c.Param("vocabulary")
termId := c.Param("id")
query := db
ctx := c.(*bolo.RequestContext)
queryI, err := ctx.Query.SetDatabaseQueryForModel(query, &VocabularyModel{})
if err != nil {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
}).Error("ModelstermQueryAndCountReq error")
}
query = queryI.(*gorm.DB)
if termId != "" {
query = query.Where("termId = ?", termId)
}
if vocabularyName != "" {
query = query.Where("vocabularyName = ?", vocabularyName)
}
orderColumn, orderIsDesc, orderValid := helpers.ParseUrlQueryOrder(c.QueryParam("order"), c.QueryParam("sort"), c.QueryParam("sortDirection"))
if orderValid {
query = query.Order(clause.OrderByColumn{
Column: clause.Column{Table: clause.CurrentTable, Name: orderColumn},
Desc: orderIsDesc,
})
} else {
query = query.
Order("createdAt DESC").
Order("id DESC")
}
query = query.Limit(opts.Limit).
Offset(opts.Offset)
r := query.Find(opts.Records)
if r.Error != nil {
return r.Error
}
return ModelstermsCountReq(opts)
}
func ModelstermsCountReq(opts *ModelstermQueryOpts) error {
db := bolo.GetDefaultDatabaseConnection()
c := opts.C
ctx := c.(*bolo.RequestContext)
// Count ...
queryCount := db
queryICount, err := ctx.Query.SetDatabaseQueryForModel(queryCount, &VocabularyModel{})
if err != nil {
logrus.WithFields(logrus.Fields{
"error": fmt.Sprintf("%+v\n", err),
}).Error("ModelstermsCountReq count error")
}
queryCount = queryICount.(*gorm.DB)
return queryCount.
Table("modelsterms").
Count(opts.Count).Error
}