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:
parent
fb02916b1e
commit
69f59a9a16
@ -365,21 +365,36 @@ func (h *SettingsHandler) actionAddLabel(w http.ResponseWriter, r *http.Request)
|
||||
}
|
||||
user := middlewares.GetPrincipal(r)
|
||||
|
||||
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"),
|
||||
}
|
||||
|
||||
if !label.IsValid() {
|
||||
return http.StatusBadRequest, "", "invalid input"
|
||||
labels = append(labels, label)
|
||||
}
|
||||
|
||||
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, "", "invalid input"
|
||||
return http.StatusBadRequest, "", msg
|
||||
}
|
||||
}
|
||||
|
||||
return http.StatusOK, "label added to project successfully", ""
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user