From f94f7a2e7d54576c92f0c97469e28fd7464889af Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 1 Feb 2022 23:55:16 -0800 Subject: [PATCH] removed direct label assignment --- net/server/internal/api_addArticle.go | 8 ++++---- net/server/internal/api_addGroup.go | 16 ---------------- net/server/internal/api_getArticles.go | 11 +++++++++-- net/server/internal/api_removeGroup.go | 9 +-------- net/server/internal/modelUtil.go | 15 +++++++-------- net/server/internal/store/schema.go | 3 +-- 6 files changed, 22 insertions(+), 40 deletions(-) diff --git a/net/server/internal/api_addArticle.go b/net/server/internal/api_addArticle.go index a7d67513..82e26b4d 100644 --- a/net/server/internal/api_addArticle.go +++ b/net/server/internal/api_addArticle.go @@ -23,8 +23,8 @@ func AddArticle(w http.ResponseWriter, r *http.Request) { return } - var groups []store.Label - if err := store.DB.Raw("select labels.* from labels inner join label_groups on labels.id = label_groups.label_id inner join groups on label_groups.group_id = groups.id where groups.group_id in ?", articleAccess.Groups).Scan(&groups).Error; err != nil { + var groups []store.Group + if err := store.DB.Where("group_id IN ?", articleAccess.Groups).Find(&groups).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return } @@ -35,7 +35,6 @@ func AddArticle(w http.ResponseWriter, r *http.Request) { return } - // save data and apply transaction var article *store.Article err = store.DB.Transaction(func(tx *gorm.DB) error { @@ -46,7 +45,8 @@ func AddArticle(w http.ResponseWriter, r *http.Request) { TagUpdated: time.Now().Unix(), TagRevision: 1, TagCount: 0, - Labels: append(groups, labels...), + Groups: groups, + Labels: labels, }; if res := store.DB.Save(articleData).Error; res != nil { return res; diff --git a/net/server/internal/api_addGroup.go b/net/server/internal/api_addGroup.go index 3749f25a..2f401bf3 100644 --- a/net/server/internal/api_addGroup.go +++ b/net/server/internal/api_addGroup.go @@ -24,20 +24,9 @@ func AddGroup(w http.ResponseWriter, r *http.Request) { var group *store.Group err = store.DB.Transaction(func(tx *gorm.DB) error { - label := &store.Label{ - LabelId: uuid.New().String(), - AccountID: account.ID, - Revision: 1, - Direct: true, - } - if res := tx.Save(label).Error; res != nil { - return res - } - group = &store.Group{ GroupId: uuid.New().String(), AccountID: account.ID, - LabelID: label.ID, Revision: 1, DataType: subject.DataType, Data: subject.Data, @@ -46,11 +35,6 @@ func AddGroup(w http.ResponseWriter, r *http.Request) { return res } - label.Groups = []store.Group{*group} - if res := tx.Save(label).Error; res != nil { - return res - } - if res := tx.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_getArticles.go b/net/server/internal/api_getArticles.go index 91d53ee0..bb55ed9f 100644 --- a/net/server/internal/api_getArticles.go +++ b/net/server/internal/api_getArticles.go @@ -79,6 +79,13 @@ func isShared(article *store.Article, guid string) bool { if article.ArticleData == nil { return false } + for _, group := range article.ArticleData.Groups { + for _, card := range group.Cards { + if card.Guid == guid { + return true + } + } + } for _, label := range article.ArticleData.Labels { for _, group := range label.Groups { for _, card := range group.Cards { @@ -92,11 +99,11 @@ func isShared(article *store.Article, guid string) bool { } func getAccountArticles(account *store.Account, revision int64, articles *[]store.Article) error { - return store.DB.Preload("ArticleData.Labels.Groups").Where("account_id = ? AND revision > ?", account.ID, revision).Find(articles).Error + return store.DB.Preload("ArticleData.Groups").Preload("ArticleData.Labels.Groups").Where("account_id = ? AND revision > ?", account.ID, revision).Find(articles).Error } func getContactArticles(card *store.Card, revision int64, articles *[]store.Article) error { - return store.DB.Preload("ArticleData.Labels.Groups.Cards").Where("account_id = ? AND revision > ?", card.Account.ID, revision).Find(articles).Error + return store.DB.Preload("ArticleData.Groups.Cards").Preload("ArticleData.Labels.Groups.Cards").Where("account_id = ? AND revision > ?", card.Account.ID, revision).Find(articles).Error } diff --git a/net/server/internal/api_removeGroup.go b/net/server/internal/api_removeGroup.go index 25f804ef..d00c5114 100644 --- a/net/server/internal/api_removeGroup.go +++ b/net/server/internal/api_removeGroup.go @@ -19,7 +19,7 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) { groupId := params["groupId"] var group store.Group - if err := store.DB.Preload("Label").Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil { + if err := store.DB.Where("account_id = ? AND group_id = ?", account.ID, groupId).First(&group).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { ErrResponse(w, http.StatusNotFound, err) } else { @@ -29,16 +29,9 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) { } err = store.DB.Transaction(func(tx *gorm.DB) error { - if res := tx.Delete(&group.Label).Error; res != nil { - return res - } if res := tx.Delete(&group).Error; res != nil { return res } - if res := tx.Model(&group.Label).Association("Groups").Delete(&group); res != nil { - return res - } - if res := tx.Model(&account).Updates(store.Account{ViewRevision: account.ViewRevision + 1, GroupRevision: account.GroupRevision + 1}).Error; res != nil { return res } diff --git a/net/server/internal/modelUtil.go b/net/server/internal/modelUtil.go index cf566f9b..7c4b82bb 100644 --- a/net/server/internal/modelUtil.go +++ b/net/server/internal/modelUtil.go @@ -58,17 +58,16 @@ func getArticleModel(article *store.Article, contact bool, shared bool) *Article } } else { - // populate id list var groups []string; + if !contact { + for _, group := range article.ArticleData.Groups { + groups = append(groups, group.GroupId) + } + } + var labels []string; for _, label := range article.ArticleData.Labels { - if label.Direct { - if !contact && len(label.Groups) > 0 { - groups = append(groups, label.Groups[0].GroupId) - } - } else { - labels = append(labels, label.LabelId) - } + labels = append(labels, label.LabelId) } return &Article{ diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 9f351703..1ac64b72 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -107,7 +107,6 @@ type Group struct { Created int64 `gorm:"autoCreateTime"` Updated int64 `gorm:"autoUpdateTime"` Cards []Card `gorm:"many2many:card_groups"` - Label Label //reference to label for direct assignment to articles Account Account } @@ -120,7 +119,6 @@ type Label struct { Data string Created int64 `gorm:"autoCreateTime"` Updated int64 `gorm:"autoUpdateTime"` - Direct bool //special label indicating direct assignment of a group Groups []Group `gorm:"many2many:label_groups;"` Account Account } @@ -190,6 +188,7 @@ type ArticleData struct { TagUpdated int64 `gorm:"not null"` TagCount int32 `gorm:"not null"` TagRevision int64 `gorm:"not null"` + Groups []Group `gorm:"many2many:article_groups;"` Labels []Label `gorm:"many2many:article_labels;"` }