feat(settings): handle bulk associations in db

This commit is contained in:
Diptesh Choudhuri 2023-01-24 16:31:46 +00:00
parent fb02916b1e
commit 69f59a9a16
1 changed files with 27 additions and 12 deletions

View File

@ -365,21 +365,36 @@ func (h *SettingsHandler) actionAddLabel(w http.ResponseWriter, r *http.Request)
}
user := middlewares.GetPrincipal(r)
label := &models.ProjectLabel{
UserID: user.ID,
ProjectKey: r.PostFormValue("key"),
Label: r.PostFormValue("value"),
var labels []*models.ProjectLabel
if r.PostFormValue("num_projects") == "multiple" {
for _, key := range r.Form["keys"] {
label := &models.ProjectLabel{
UserID: user.ID,
ProjectKey: key,
Label: r.PostFormValue("value"),
}
labels = append(labels, label)
}
} else {
label := &models.ProjectLabel{
UserID: user.ID,
ProjectKey: r.PostFormValue("key"),
Label: r.PostFormValue("value"),
}
labels = append(labels, label)
}
if !label.IsValid() {
return http.StatusBadRequest, "", "invalid input"
for _, label := range labels {
msg := "invalid input for project: " + label.ProjectKey
if !label.IsValid() {
return http.StatusBadRequest, "", msg
}
if _, err := h.projectLabelSrvc.Create(label); err != nil {
// TODO: distinguish between bad request, conflict and server error
return http.StatusBadRequest, "", msg
}
}
if _, err := h.projectLabelSrvc.Create(label); err != nil {
// TODO: distinguish between bad request, conflict and server error
return http.StatusBadRequest, "", "invalid input"
}
return http.StatusOK, "label added to project successfully", ""
}