diff --git a/routes/settings.go b/routes/settings.go index 3ef7cd8..6ed8ea4 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -3,6 +3,12 @@ package routes import ( "encoding/base64" "fmt" + "net/http" + "sort" + "strconv" + "strings" + "time" + datastructure "github.com/duke-git/lancet/v2/datastructure/set" "github.com/emvi/logbuch" "github.com/gorilla/mux" @@ -15,11 +21,6 @@ import ( "github.com/muety/wakapi/services" "github.com/muety/wakapi/services/imports" "github.com/muety/wakapi/utils" - "net/http" - "sort" - "strconv" - "strings" - "time" ) const criticalError = "a critical error has occurred, sorry" @@ -139,6 +140,8 @@ func (h *SettingsHandler) dispatchAction(action string) action { return h.actionDeleteAlias case "add_alias": return h.actionAddAlias + case "add_project_to_label": + return h.addProjectToLabel case "add_label": return h.actionAddLabel case "delete_label": @@ -382,6 +385,29 @@ func (h *SettingsHandler) actionAddLabel(w http.ResponseWriter, r *http.Request) return http.StatusOK, "label added successfully", "" } +func (h *SettingsHandler) addProjectToLabel(w http.ResponseWriter, r *http.Request) (int, string, string) { + if h.config.IsDev() { + loadTemplates() + } + user := middlewares.GetPrincipal(r) + label := &models.ProjectLabel{ + UserID: user.ID, + ProjectKey: r.PostFormValue("project"), + Label: r.PostFormValue("label"), + } + + if !label.IsValid() { + return http.StatusBadRequest, "", "invalid input" + } + + 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, "added project to label successfully", "" +} + func (h *SettingsHandler) actionDeleteLabel(w http.ResponseWriter, r *http.Request) (int, string, string) { if h.config.IsDev() { loadTemplates()