removed direct label assignment

This commit is contained in:
Roland Osborne 2022-02-01 23:55:16 -08:00
parent a6ea2a7be1
commit f94f7a2e7d
6 changed files with 22 additions and 40 deletions

View File

@ -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;

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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{

View File

@ -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;"`
}