From 1355d51dfaf3a40a68ddf30fe562214419881bc4 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Thu, 3 Mar 2022 00:03:32 -0800 Subject: [PATCH] updating foreign key to allow for batch delete --- net/server/internal/api_addChannelTopic.go | 19 ++++++++++--------- net/server/internal/api_getChannelTopics.go | 6 ++++-- net/server/internal/store/schema.go | 2 +- net/server/internal/ucTopicShare_test.go | 1 - 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/net/server/internal/api_addChannelTopic.go b/net/server/internal/api_addChannelTopic.go index 375c756f..199d323c 100644 --- a/net/server/internal/api_addChannelTopic.go +++ b/net/server/internal/api_addChannelTopic.go @@ -27,9 +27,17 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { topicSlot := &store.TopicSlot{} err = store.DB.Transaction(func(tx *gorm.DB) error { - // add new record + topicSlot.TopicSlotId = uuid.New().String() + topicSlot.AccountID = act.ID + topicSlot.ChannelID = channelSlot.Channel.ID + topicSlot.Revision = act.ChannelRevision + 1 + if res := tx.Save(topicSlot).Error; res != nil { + return res + } + topic := &store.Topic{} topic.ChannelID = channelSlot.Channel.ID + topic.TopicSlotID = topicSlot.ID topic.Data = subject.Data topic.DataType = subject.DataType topic.TagCount = 0 @@ -44,15 +52,8 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { if res := tx.Save(topic).Error; res != nil { return res } - topicSlot.TopicSlotId = uuid.New().String() - topicSlot.AccountID = act.ID - topicSlot.ChannelID = channelSlot.Channel.ID - topicSlot.TopicID = topic.ID - topicSlot.Revision = act.ChannelRevision + 1 + topicSlot.Topic = topic - if res := tx.Save(topicSlot).Error; res != nil { - return res - } // update parent revision if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { diff --git a/net/server/internal/api_getChannelTopics.go b/net/server/internal/api_getChannelTopics.go index c9f286ae..d3ccd920 100644 --- a/net/server/internal/api_getChannelTopics.go +++ b/net/server/internal/api_getChannelTopics.go @@ -37,12 +37,14 @@ func GetChannelTopics(w http.ResponseWriter, r *http.Request) { } } else { var slots []store.TopicSlot - if err := store.DB.Preload("Topic").Where("channel_id = ? AND topic_id != 0", channelSlot.Channel.ID).Find(&slots).Error; err != nil { + if err := store.DB.Preload("Topic").Where("channel_id = ?", channelSlot.Channel.ID).Find(&slots).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return } for _, slot := range slots { - response = append(response, getTopicModel(&slot)) + if slot.Topic != nil { + response = append(response, getTopicModel(&slot)) + } } } diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 12547bdc..255fd551 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -212,7 +212,6 @@ type TopicSlot struct { AccountID uint `gorm:"not null;index:topicaccount,unique"` ChannelID uint `gorm:"not null;index:topicchannel,unique"` Revision int64 `gorm:"not null"` - TopicID uint `gorm:"not null;default:0"` Topic *Topic Channel *Channel Account Account @@ -222,6 +221,7 @@ type Topic struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` DetailRevision int64 `gorm:"not null"` ChannelID uint + TopicSlotID uint `gorm:"not null;index:topictopicslot,unique"` Guid string DataType string `gorm:"index"` Data string diff --git a/net/server/internal/ucTopicShare_test.go b/net/server/internal/ucTopicShare_test.go index 5d2c876a..269fa4e4 100644 --- a/net/server/internal/ucTopicShare_test.go +++ b/net/server/internal/ucTopicShare_test.go @@ -130,7 +130,6 @@ func TestTopicShare(t *testing.T) { tags = &[]Tag{} assert.NoError(t, ApiTestMsg(GetChannelTopicTags, "GET", "/content/channels/{channelId}/topics/{topicId}", ¶ms, nil, APP_TOKENCONTACT, set.C.A.Token, tags, nil)) -PrintMsg(tags) assert.Equal(t, 0, len(*tags)) // get list of assets