This repository has been archived by the owner on Jul 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
create.go
179 lines (152 loc) · 4.63 KB
/
create.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
177
178
179
package rating
import (
"context"
"encoding/json"
"errors"
"net/http"
"reflect"
"github.com/factly/dega-server/config"
"github.com/factly/dega-server/service/fact-check/model"
"github.com/factly/dega-server/test"
"github.com/factly/dega-server/util"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/meilisearchx"
"github.com/factly/x/middlewarex"
"github.com/factly/x/renderx"
"github.com/factly/x/slugx"
"github.com/factly/x/validationx"
"gorm.io/gorm"
)
// create - Create rating
// @Summary Create rating
// @Description Create rating
// @Tags Rating
// @ID add-rating
// @Consume json
// @Produce json
// @Param X-User header string true "User ID"
// @Param X-Space header string true "Space ID"
// @Param Rating body rating true "Rating Object"
// @Success 201 {object} model.Rating
// @Failure 400 {array} string
// @Router /fact-check/ratings [post]
func create(w http.ResponseWriter, r *http.Request) {
sID, err := middlewarex.GetSpace(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
rating := &rating{}
err = json.NewDecoder(r.Body).Decode(&rating)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
validationError := validationx.Check(rating)
if validationError != nil {
loggerx.Error(errors.New("validation error"))
errorx.Render(w, validationError)
return
}
// Get table name
stmt := &gorm.Statement{DB: config.DB}
_ = stmt.Parse(&model.Rating{})
tableName := stmt.Schema.Table
var ratingSlug string
if rating.Slug != "" && slugx.Check(rating.Slug) {
ratingSlug = rating.Slug
} else {
ratingSlug = slugx.Make(rating.Name)
}
// Check if rating with same name exist
if util.CheckName(uint(sID), rating.Name, tableName) {
loggerx.Error(errors.New(`rating with same name exist`))
errorx.Render(w, errorx.Parser(errorx.SameNameExist()))
return
}
// Check if rating with same numeric value exist
var sameValueRatings int64
config.DB.Model(&model.Rating{}).Where(&model.Rating{
SpaceID: uint(sID),
NumericValue: rating.NumericValue,
}).Count(&sameValueRatings)
if sameValueRatings > 0 {
loggerx.Error(errors.New(`rating with same numeric value exist`))
errorx.Render(w, errorx.Parser(errorx.GetMessage(`rating with same numeric value exist`, http.StatusUnprocessableEntity)))
return
}
mediumID := &rating.MediumID
if rating.MediumID == 0 {
mediumID = nil
}
// Store HTML description
var description string
if len(rating.Description.RawMessage) > 0 && !reflect.DeepEqual(rating.Description, test.NilJsonb()) {
description, err = util.HTMLDescription(rating.Description)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.GetMessage("cannot parse rating description", http.StatusUnprocessableEntity)))
return
}
}
result := &model.Rating{
Name: rating.Name,
Slug: slugx.Approve(&config.DB, ratingSlug, sID, tableName),
BackgroundColour: rating.BackgroundColour,
TextColour: rating.TextColour,
Description: rating.Description,
HTMLDescription: description,
MediumID: mediumID,
SpaceID: uint(sID),
NumericValue: rating.NumericValue,
MetaFields: rating.MetaFields,
}
tx := config.DB.WithContext(context.WithValue(r.Context(), userContext, uID)).Begin()
err = tx.Model(&model.Rating{}).Create(&result).Error
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
tx.Model(&model.Rating{}).Preload("Medium").First(&result)
err = insertIntoMeili(*result)
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
tx.Commit()
if util.CheckNats() {
if err = util.NC.Publish("rating.created", result); err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InternalServerError()))
return
}
}
renderx.JSON(w, http.StatusCreated, result)
}
func insertIntoMeili(rating model.Rating) error {
meiliObj := map[string]interface{}{
"id": rating.ID,
"kind": "rating",
"name": rating.Name,
"background_colour": rating.BackgroundColour,
"text_colour": rating.TextColour,
"slug": rating.Slug,
"description": rating.Description,
"numeric_value": rating.NumericValue,
"space_id": rating.SpaceID,
}
return meilisearchx.AddDocument("dega", meiliObj)
}