adding tag subject update

This commit is contained in:
Roland Osborne 2022-03-02 21:46:21 -08:00
parent 381cde4108
commit 11483178a1
4 changed files with 115 additions and 5 deletions

View File

@ -29,6 +29,7 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) {
// add new record // add new record
topic := &store.Topic{} topic := &store.Topic{}
topic.ChannelID = channelSlot.Channel.ID
topic.Data = subject.Data topic.Data = subject.Data
topic.DataType = subject.DataType topic.DataType = subject.DataType
topic.TagCount = 0 topic.TagCount = 0

View File

@ -33,8 +33,3 @@ func RemoveChannelTopicTag(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
func SetChannelTopicTagSubject(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
}

View File

@ -0,0 +1,111 @@
package databag
import (
"time"
"errors"
"net/http"
"gorm.io/gorm"
"github.com/gorilla/mux"
"databag/internal/store"
)
func SetChannelTopicTagSubject(w http.ResponseWriter, r *http.Request) {
// scan parameters
params := mux.Vars(r)
channelId := params["channelId"]
topicId := params["topicId"]
tagId := params["tagId"]
var subject Subject
if err := ParseRequest(r, w, &subject); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
channelSlot, guid, err, code := getChannelSlot(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
// load topic
var tagSlot store.TagSlot
if err = store.DB.Preload("Tag.Channel.ChannelSlot").Preload("Tag.Topic.TopicSlot").Where("account_id = ? AND tag_slot_id = ?", act.ID, tagId).First(&tagSlot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if tagSlot.Tag == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty tag"))
return
}
if tagSlot.Tag.Channel.ChannelSlot.ChannelSlotId != channelId {
ErrResponse(w, http.StatusNotFound, errors.New("channel tag not found"))
return
}
if tagSlot.Tag.Topic.TopicSlot.TopicSlotId != topicId {
ErrResponse(w, http.StatusNotFound, errors.New("topic tag not found"))
return
}
if tagSlot.Tag.Guid != guid {
ErrResponse(w, http.StatusUnauthorized, errors.New("not creator of tag"))
return
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(tagSlot.Tag).Update("data", subject.Data).Error; res != nil {
return res
}
if res := tx.Model(tagSlot.Tag).Update("data_type", subject.DataType).Error; res != nil {
return res
}
if res := tx.Model(&tagSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&tagSlot.Tag.Topic).Update("tag_revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&tagSlot.Tag.Topic).Update("tag_updated", time.Now().Unix()).Error; res != nil {
return res
}
if res := tx.Model(&tagSlot.Tag.Topic.TopicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
return res
}
if res := tx.Model(act).Update("channel_revision", act.ChannelRevision + 1).Error; res != nil {
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)
}
WriteResponse(w, getTagModel(&tagSlot))
}

View File

@ -202,6 +202,7 @@ type Channel struct {
Updated int64 `gorm:"autoUpdateTime"` Updated int64 `gorm:"autoUpdateTime"`
Groups []Group `gorm:"many2many:channel_groups;"` Groups []Group `gorm:"many2many:channel_groups;"`
Cards []Card `gorm:"many2many:channel_cards;"` Cards []Card `gorm:"many2many:channel_cards;"`
Topics []Topic
ChannelSlot ChannelSlot ChannelSlot ChannelSlot
} }
@ -220,6 +221,7 @@ type TopicSlot struct {
type Topic struct { type Topic struct {
ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` ID uint `gorm:"primaryKey;not null;unique;autoIncrement"`
DetailRevision int64 `gorm:"not null"` DetailRevision int64 `gorm:"not null"`
ChannelID uint
Guid string Guid string
DataType string `gorm:"index"` DataType string `gorm:"index"`
Data string Data string
@ -229,6 +231,7 @@ type Topic struct {
TagCount int32 `gorm:"not null"` TagCount int32 `gorm:"not null"`
TagUpdated int64 TagUpdated int64
TagRevision int64 `gorm:"not null"` TagRevision int64 `gorm:"not null"`
Channel *Channel
Assets []Asset Assets []Asset
Tags []Tag Tags []Tag
TopicSlot TopicSlot TopicSlot TopicSlot