1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

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

View File

@ -365,21 +365,36 @@ func (h *SettingsHandler) actionAddLabel(w http.ResponseWriter, r *http.Request)
} }
user := middlewares.GetPrincipal(r) user := middlewares.GetPrincipal(r)
label := &models.ProjectLabel{ var labels []*models.ProjectLabel
UserID: user.ID,
ProjectKey: r.PostFormValue("key"), if r.PostFormValue("num_projects") == "multiple" {
Label: r.PostFormValue("value"), 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() { for _, label := range labels {
return http.StatusBadRequest, "", "invalid input" 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", "" return http.StatusOK, "label added to project successfully", ""
} }