2022-02-18 07:56:30 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AddChannelTopic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
var subject Subject
|
|
|
|
if err := ParseRequest(r, w, &subject); err != nil {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-19 06:33:13 +00:00
|
|
|
channelSlot, guid, err, code := getChannelSlot(r, true)
|
2022-02-18 20:21:15 +00:00
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
2022-02-18 07:56:30 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-18 20:21:15 +00:00
|
|
|
act := &channelSlot.Account
|
2022-02-18 07:56:30 +00:00
|
|
|
|
|
|
|
topicSlot := &store.TopicSlot{}
|
2022-02-18 20:21:15 +00:00
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-02-18 07:56:30 +00:00
|
|
|
|
|
|
|
// add new record
|
|
|
|
topic := &store.Topic{}
|
|
|
|
topic.Data = subject.Data
|
|
|
|
topic.DataType = subject.DataType
|
|
|
|
topic.TagCount = 0
|
|
|
|
topic.Guid = guid
|
|
|
|
topic.DetailRevision = act.ChannelRevision + 1
|
|
|
|
topic.TagRevision = act.ChannelRevision + 1
|
|
|
|
topic.Status = APP_TOPICUNCONFIRMED
|
2022-03-01 08:28:36 +00:00
|
|
|
topic.Transform = APP_TRANSFORMCOMPLETE
|
2022-02-18 07:56:30 +00:00
|
|
|
if res := tx.Save(topic).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
topicSlot.TopicSlotId = uuid.New().String()
|
|
|
|
topicSlot.AccountID = act.ID
|
2022-02-18 20:21:15 +00:00
|
|
|
topicSlot.ChannelID = channelSlot.Channel.ID
|
2022-02-18 07:56:30 +00:00
|
|
|
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 {
|
|
|
|
return res
|
|
|
|
}
|
2022-02-18 20:21:15 +00:00
|
|
|
if res := tx.Model(act).Update("channel_revision", act.ChannelRevision + 1).Error; res != nil {
|
2022-02-18 07:56:30 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine affected contact list
|
|
|
|
cards := make(map[string]store.Card)
|
|
|
|
for _, card := range channelSlot.Channel.Cards {
|
|
|
|
cards[card.Guid] = card
|
|
|
|
}
|
|
|
|
for _, group := range channelSlot.Channel.Groups {
|
|
|
|
for _, card := range group.Cards {
|
|
|
|
cards[card.Guid] = card
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatus(act)
|
|
|
|
for _, card := range cards {
|
|
|
|
SetContactChannelNotification(act, &card)
|
|
|
|
}
|
2022-02-18 20:21:15 +00:00
|
|
|
WriteResponse(w, getTopicModel(topicSlot))
|
2022-02-18 07:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|