-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
116 lines (99 loc) · 2.91 KB
/
update.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
package webhook
import (
"context"
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/factly/hukz/config"
"github.com/factly/hukz/model"
"github.com/factly/x/errorx"
"github.com/factly/x/loggerx"
"github.com/factly/x/middlewarex"
"github.com/factly/x/renderx"
"github.com/factly/x/validationx"
"github.com/go-chi/chi"
)
// update - Update webhook by id
// @Summary Update a webhook by id
// @Description Update webhook by ID
// @Tags Webhooks
// @ID update-webhook-by-id
// @Produce json
// @Consume json
// @Param X-User header string true "User ID"
// @Param webhook_id path string true "Webhook ID"
// @Param Webhook body webhook false "Webhook Object"
// @Success 200 {object} model.Webhook
// @Router /webhooks/{webhook_id} [put]
func update(w http.ResponseWriter, r *http.Request) {
webhookID := chi.URLParam(r, "webhook_id")
id, err := strconv.Atoi(webhookID)
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.InvalidID()))
return
}
uID, err := middlewarex.GetUser(r.Context())
if err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.Unauthorized()))
return
}
webhook := &webhook{}
if err = json.NewDecoder(r.Body).Decode(&webhook); err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
if validationError := validationx.Check(webhook); validationError != nil {
loggerx.Error(errors.New("validation error"))
errorx.Render(w, validationError)
return
}
result := &model.Webhook{}
result.ID = uint(id)
// check record exists or not
if err = config.DB.First(&result).Error; err != nil {
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.RecordNotFound()))
return
}
tx := config.DB.WithContext(context.WithValue(r.Context(), userContext, uID)).Begin()
// validate tags
var tags map[string]string
err = json.Unmarshal(webhook.Tags.RawMessage, &tags)
if err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DecodeError()))
return
}
newEvents := make([]model.Event, 0)
if len(webhook.EventIDs) > 0 {
config.DB.Model(&model.Event{}).Where(webhook.EventIDs).Find(&newEvents)
if err = tx.Model(&result).Association("Events").Replace(&newEvents); err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
} else {
_ = config.DB.Model(&result).Association("Events").Clear()
}
tx.Model(&result).Select("Enabled").Omit("Events").Updates(model.Webhook{Enabled: webhook.Enabled})
updatedWebhook := model.Webhook{
Name: webhook.Name,
Base: model.Base{UpdatedByID: uint(uID)},
URL: webhook.URL,
Tags: webhook.Tags,
}
if err = tx.Model(&result).Omit("Events").Updates(updatedWebhook).Preload("Events").First(&result).Error; err != nil {
tx.Rollback()
loggerx.Error(err)
errorx.Render(w, errorx.Parser(errorx.DBError()))
return
}
tx.Commit()
renderx.JSON(w, http.StatusOK, result)
}