From 2c7977cf634b90cec907ee667e1141463dcab75b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Wed, 13 Oct 2021 17:12:55 +0200 Subject: [PATCH] chore: invert visualization of project labels (resolve #263) --- routes/settings.go | 28 ++++++++++++---------------- services/project_label.go | 26 +++++++++++++++++++++----- services/services.go | 1 + views/settings.tpl.html | 4 ++-- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/routes/settings.go b/routes/settings.go index ec96c10..3ac9ce2 100644 --- a/routes/settings.go +++ b/routes/settings.go @@ -354,27 +354,23 @@ func (h *SettingsHandler) actionDeleteLabel(w http.ResponseWriter, r *http.Reque } user := middlewares.GetPrincipal(r) - labelKey := r.PostFormValue("key") - labelValue := r.PostFormValue("value") + labelKey := r.PostFormValue("key") // label key + labelValue := r.PostFormValue("value") // project key - labelMap, err := h.projectLabelSrvc.GetByUserGrouped(user.ID) + labels, err := h.projectLabelSrvc.GetByUser(user.ID) if err != nil { return http.StatusInternalServerError, "", "could not delete label" } - if projectLabels, ok := labelMap[labelKey]; ok { - for _, l := range projectLabels { - if l.Label == labelValue { - if err := h.projectLabelSrvc.Delete(l); err != nil { - return http.StatusInternalServerError, "", "could not delete label" - } - return http.StatusOK, "label deleted successfully", "" + for _, l := range labels { + if l.Label == labelKey && l.ProjectKey == labelValue { + if err := h.projectLabelSrvc.Delete(l); err != nil { + return http.StatusInternalServerError, "", "could not delete label" } + return http.StatusOK, "label deleted successfully", "" } - return http.StatusNotFound, "", "label not found" - } else { - return http.StatusNotFound, "", "project not found" } + return http.StatusNotFound, "", "label not found" } func (h *SettingsHandler) actionDeleteLanguageMapping(w http.ResponseWriter, r *http.Request) (int, string, string) { @@ -651,7 +647,7 @@ func (h *SettingsHandler) buildViewModel(r *http.Request) *view.SettingsViewMode } // labels - labelMap, err := h.projectLabelSrvc.GetByUserGrouped(user.ID) + labelMap, err := h.projectLabelSrvc.GetByUserGroupedInverted(user.ID) if err != nil { conf.Log().Request(r).Error("error while building settings project label map - %v", err) return &view.SettingsViewModel{Error: criticalError} @@ -660,11 +656,11 @@ func (h *SettingsHandler) buildViewModel(r *http.Request) *view.SettingsViewMode combinedLabels := make([]*view.SettingsVMCombinedLabel, 0) for _, l := range labelMap { cl := &view.SettingsVMCombinedLabel{ - Key: l[0].ProjectKey, + Key: l[0].Label, Values: make([]string, len(l)), } for i, l1 := range l { - cl.Values[i] = l1.Label + cl.Values[i] = l1.ProjectKey } combinedLabels = append(combinedLabels, cl) } diff --git a/services/project_label.go b/services/project_label.go index 853eaf1..b2ac19b 100644 --- a/services/project_label.go +++ b/services/project_label.go @@ -44,21 +44,37 @@ func (srv *ProjectLabelService) GetByUser(userId string) ([]*models.ProjectLabel } func (srv *ProjectLabelService) GetByUserGrouped(userId string) (map[string][]*models.ProjectLabel, error) { - labels := make(map[string][]*models.ProjectLabel) + labelsByProject := make(map[string][]*models.ProjectLabel) userLabels, err := srv.GetByUser(userId) if err != nil { return nil, err } for _, l := range userLabels { - if _, ok := labels[l.ProjectKey]; !ok { - labels[l.ProjectKey] = []*models.ProjectLabel{l} + if _, ok := labelsByProject[l.ProjectKey]; !ok { + labelsByProject[l.ProjectKey] = []*models.ProjectLabel{l} } else { - labels[l.ProjectKey] = append(labels[l.ProjectKey], l) + labelsByProject[l.ProjectKey] = append(labelsByProject[l.ProjectKey], l) } } + return labelsByProject, nil +} - return labels, nil +func (srv *ProjectLabelService) GetByUserGroupedInverted(userId string) (map[string][]*models.ProjectLabel, error) { + projectsByLabel := make(map[string][]*models.ProjectLabel) + userLabels, err := srv.GetByUser(userId) + if err != nil { + return nil, err + } + + for _, l := range userLabels { + if _, ok := projectsByLabel[l.Label]; !ok { + projectsByLabel[l.Label] = []*models.ProjectLabel{l} + } else { + projectsByLabel[l.Label] = append(projectsByLabel[l.Label], l) + } + } + return projectsByLabel, nil } func (srv *ProjectLabelService) Create(label *models.ProjectLabel) (*models.ProjectLabel, error) { diff --git a/services/services.go b/services/services.go index ab25952..0dd954f 100644 --- a/services/services.go +++ b/services/services.go @@ -62,6 +62,7 @@ type IProjectLabelService interface { GetById(uint) (*models.ProjectLabel, error) GetByUser(string) ([]*models.ProjectLabel, error) GetByUserGrouped(string) (map[string][]*models.ProjectLabel, error) + GetByUserGroupedInverted(string) (map[string][]*models.ProjectLabel, error) Create(*models.ProjectLabel) (*models.ProjectLabel, error) Delete(*models.ProjectLabel) error } diff --git a/views/settings.tpl.html b/views/settings.tpl.html index 3be74b0..5c56a69 100644 --- a/views/settings.tpl.html +++ b/views/settings.tpl.html @@ -221,9 +221,9 @@
- ▸ {{ $label.Key }}: + ▸ {{ $label.Key }}: {{ range $j, $value := $label.Values }} -
+