updating foreign key to allow for batch delete

This commit is contained in:
Roland Osborne 2022-03-03 00:03:32 -08:00
parent d84425df09
commit 1355d51dfa
4 changed files with 15 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -130,7 +130,6 @@ func TestTopicShare(t *testing.T) {
tags = &[]Tag{}
assert.NoError(t, ApiTestMsg(GetChannelTopicTags, "GET", "/content/channels/{channelId}/topics/{topicId}",
&params, nil, APP_TOKENCONTACT, set.C.A.Token, tags, nil))
PrintMsg(tags)
assert.Equal(t, 0, len(*tags))
// get list of assets