mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
removed direct label assignment
This commit is contained in:
parent
a6ea2a7be1
commit
f94f7a2e7d
@ -23,8 +23,8 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var groups []store.Label
|
var groups []store.Group
|
||||||
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 {
|
if err := store.DB.Where("group_id IN ?", articleAccess.Groups).Find(&groups).Error; err != nil {
|
||||||
ErrResponse(w, http.StatusInternalServerError, err)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -35,7 +35,6 @@ func AddArticle(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// save data and apply transaction
|
// save data and apply transaction
|
||||||
var article *store.Article
|
var article *store.Article
|
||||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
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(),
|
TagUpdated: time.Now().Unix(),
|
||||||
TagRevision: 1,
|
TagRevision: 1,
|
||||||
TagCount: 0,
|
TagCount: 0,
|
||||||
Labels: append(groups, labels...),
|
Groups: groups,
|
||||||
|
Labels: labels,
|
||||||
};
|
};
|
||||||
if res := store.DB.Save(articleData).Error; res != nil {
|
if res := store.DB.Save(articleData).Error; res != nil {
|
||||||
return res;
|
return res;
|
||||||
|
@ -24,20 +24,9 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
var group *store.Group
|
var group *store.Group
|
||||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
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{
|
group = &store.Group{
|
||||||
GroupId: uuid.New().String(),
|
GroupId: uuid.New().String(),
|
||||||
AccountID: account.ID,
|
AccountID: account.ID,
|
||||||
LabelID: label.ID,
|
|
||||||
Revision: 1,
|
Revision: 1,
|
||||||
DataType: subject.DataType,
|
DataType: subject.DataType,
|
||||||
Data: subject.Data,
|
Data: subject.Data,
|
||||||
@ -46,11 +35,6 @@ func AddGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
return res
|
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 {
|
if res := tx.Model(&account).Update("group_revision", account.GroupRevision + 1).Error; res != nil {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,13 @@ func isShared(article *store.Article, guid string) bool {
|
|||||||
if article.ArticleData == nil {
|
if article.ArticleData == nil {
|
||||||
return false
|
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 _, label := range article.ArticleData.Labels {
|
||||||
for _, group := range label.Groups {
|
for _, group := range label.Groups {
|
||||||
for _, card := range group.Cards {
|
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 {
|
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 {
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
groupId := params["groupId"]
|
groupId := params["groupId"]
|
||||||
|
|
||||||
var group store.Group
|
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) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
ErrResponse(w, http.StatusNotFound, err)
|
ErrResponse(w, http.StatusNotFound, err)
|
||||||
} else {
|
} else {
|
||||||
@ -29,16 +29,9 @@ func RemoveGroup(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
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 {
|
if res := tx.Delete(&group).Error; res != nil {
|
||||||
return res
|
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 {
|
if res := tx.Model(&account).Updates(store.Account{ViewRevision: account.ViewRevision + 1, GroupRevision: account.GroupRevision + 1}).Error; res != nil {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
@ -58,17 +58,16 @@ func getArticleModel(article *store.Article, contact bool, shared bool) *Article
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
// populate id list
|
|
||||||
var groups []string;
|
var groups []string;
|
||||||
|
if !contact {
|
||||||
|
for _, group := range article.ArticleData.Groups {
|
||||||
|
groups = append(groups, group.GroupId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var labels []string;
|
var labels []string;
|
||||||
for _, label := range article.ArticleData.Labels {
|
for _, label := range article.ArticleData.Labels {
|
||||||
if label.Direct {
|
labels = append(labels, label.LabelId)
|
||||||
if !contact && len(label.Groups) > 0 {
|
|
||||||
groups = append(groups, label.Groups[0].GroupId)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
labels = append(labels, label.LabelId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Article{
|
return &Article{
|
||||||
|
@ -107,7 +107,6 @@ type Group struct {
|
|||||||
Created int64 `gorm:"autoCreateTime"`
|
Created int64 `gorm:"autoCreateTime"`
|
||||||
Updated int64 `gorm:"autoUpdateTime"`
|
Updated int64 `gorm:"autoUpdateTime"`
|
||||||
Cards []Card `gorm:"many2many:card_groups"`
|
Cards []Card `gorm:"many2many:card_groups"`
|
||||||
Label Label //reference to label for direct assignment to articles
|
|
||||||
Account Account
|
Account Account
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,7 +119,6 @@ type Label struct {
|
|||||||
Data string
|
Data string
|
||||||
Created int64 `gorm:"autoCreateTime"`
|
Created int64 `gorm:"autoCreateTime"`
|
||||||
Updated int64 `gorm:"autoUpdateTime"`
|
Updated int64 `gorm:"autoUpdateTime"`
|
||||||
Direct bool //special label indicating direct assignment of a group
|
|
||||||
Groups []Group `gorm:"many2many:label_groups;"`
|
Groups []Group `gorm:"many2many:label_groups;"`
|
||||||
Account Account
|
Account Account
|
||||||
}
|
}
|
||||||
@ -190,6 +188,7 @@ type ArticleData struct {
|
|||||||
TagUpdated int64 `gorm:"not null"`
|
TagUpdated int64 `gorm:"not null"`
|
||||||
TagCount int32 `gorm:"not null"`
|
TagCount int32 `gorm:"not null"`
|
||||||
TagRevision int64 `gorm:"not null"`
|
TagRevision int64 `gorm:"not null"`
|
||||||
|
Groups []Group `gorm:"many2many:article_groups;"`
|
||||||
Labels []Label `gorm:"many2many:article_labels;"`
|
Labels []Label `gorm:"many2many:article_labels;"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user