From c6653f126b446880ff77053513bb2df2be97ee82 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Fri, 4 Mar 2022 12:03:33 -0800 Subject: [PATCH] updating channel model for consistency with topics --- doc/api.oa3 | 57 ++----------------- net/server/internal/api_addChannel.go | 2 + net/server/internal/api_addChannelTopic.go | 4 +- .../internal/api_addChannelTopicAsset.go | 3 + net/server/internal/api_addChannelTopicTag.go | 10 +--- .../internal/api_getChannelTopicCount.go | 42 -------------- net/server/internal/api_removeChannelTopic.go | 3 + .../internal/api_removeChannelTopicAsset.go | 3 + .../internal/api_removeChannelTopicTag.go | 8 +-- .../internal/api_setChannelTopicConfirmed.go | 3 + .../internal/api_setChannelTopicSubject.go | 3 + .../internal/api_setChannelTopicTagSubject.go | 5 +- net/server/internal/modelUtil.go | 13 ----- net/server/internal/models.go | 11 +--- net/server/internal/routers.go | 7 --- net/server/internal/store/schema.go | 3 +- net/server/internal/testApp.go | 2 - 17 files changed, 35 insertions(+), 144 deletions(-) delete mode 100644 net/server/internal/api_getChannelTopicCount.go diff --git a/doc/api.oa3 b/doc/api.oa3 index ccff2532..96c0b99d 100644 --- a/doc/api.oa3 +++ b/doc/api.oa3 @@ -2395,43 +2395,6 @@ paths: '500': description: internal server error - /content/channels/{channelId}/topics/{topicId}/count: - get: - tags: - - content - description: Get count of associated taghs. - operationId: get-channel-topic-count - security: - - bearerAuth: [] - parameters: - - name: channelId - in: path - description: specified channel id - required: true - schema: - type: string - - name: topicId - in: path - description: specified topic id - required: true - schema: - type: string - responses: - '200': - description: success - content: - application/json: - schema: - $ref: '#/components/schemas/TagCount' - '401': - description: invalid password - '404': - description: channel not found - '410': - description: account disabled - '500': - description: internal server error - /content/channels/{channelId}/topics/{topicId}/subject/{field}: get: tags: @@ -3253,10 +3216,14 @@ components: type: object required: - detailRevision + - topicRevision properties: detailRevision: type: integer format: int64 + topicRevision: + type: integer + format: int64 channelDetail: $ref: '#/components/schemas/ChannelDetail' @@ -3324,8 +3291,6 @@ components: format: int64 topicDetail: $ref: '#/components/schemas/TopicDetail' - topicTags:: - $ref: '#/components/schemas/TagCount' TopicDetail: type: object @@ -3356,19 +3321,6 @@ components: type: string enum: [ complete, incomplete ] - TagCount: - type: object - required: - - count - - updated - properties: - count: - type: integer - format: int32 - updated: - type: integer - format: int64 - Tag: type: object required: @@ -3642,3 +3594,4 @@ components: + diff --git a/net/server/internal/api_addChannel.go b/net/server/internal/api_addChannel.go index 9b935d13..655bd2d0 100644 --- a/net/server/internal/api_addChannel.go +++ b/net/server/internal/api_addChannel.go @@ -27,6 +27,8 @@ func AddChannel(w http.ResponseWriter, r *http.Request) { channel := &store.Channel{} channel.Data = subject.Data channel.DataType = subject.DataType + channel.DetailRevision = account.ChannelRevision + 1 + channel.TopicRevision = account.ChannelRevision + 1 if res := tx.Save(channel).Error; res != nil { return res } diff --git a/net/server/internal/api_addChannelTopic.go b/net/server/internal/api_addChannelTopic.go index 199d323c..fe828470 100644 --- a/net/server/internal/api_addChannelTopic.go +++ b/net/server/internal/api_addChannelTopic.go @@ -40,7 +40,6 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { topic.TopicSlotID = topicSlot.ID topic.Data = subject.Data topic.DataType = subject.DataType - topic.TagCount = 0 topic.Guid = guid topic.DetailRevision = act.ChannelRevision + 1 topic.TagRevision = act.ChannelRevision + 1 @@ -56,6 +55,9 @@ func AddChannelTopic(w http.ResponseWriter, r *http.Request) { topicSlot.Topic = topic // update parent revision + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_addChannelTopicAsset.go b/net/server/internal/api_addChannelTopicAsset.go index 399f40c5..e6b08289 100644 --- a/net/server/internal/api_addChannelTopicAsset.go +++ b/net/server/internal/api_addChannelTopicAsset.go @@ -130,6 +130,9 @@ func AddChannelTopicAsset(w http.ResponseWriter, r *http.Request) { if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_addChannelTopicTag.go b/net/server/internal/api_addChannelTopicTag.go index 8de28081..dee36b59 100644 --- a/net/server/internal/api_addChannelTopicTag.go +++ b/net/server/internal/api_addChannelTopicTag.go @@ -1,7 +1,6 @@ package databag import ( - "time" "errors" "net/http" "github.com/gorilla/mux" @@ -68,18 +67,15 @@ func AddChannelTopicTag(w http.ResponseWriter, r *http.Request) { return res } - if res := tx.Model(&topicSlot.Topic).Update("tag_count", len(topicSlot.Topic.Tags) + 1).Error; res != nil { - return res - } - if res := tx.Model(&topicSlot.Topic).Update("tag_updated", time.Now().Unix()).Error; res != nil { - return res - } if res := tx.Model(&topicSlot.Topic).Update("tag_revision", act.ChannelRevision + 1).Error; res != nil { return res } if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_getChannelTopicCount.go b/net/server/internal/api_getChannelTopicCount.go deleted file mode 100644 index 0b2861a3..00000000 --- a/net/server/internal/api_getChannelTopicCount.go +++ /dev/null @@ -1,42 +0,0 @@ -package databag - -import ( - "errors" - "net/http" - "gorm.io/gorm" - "github.com/gorilla/mux" - "databag/internal/store" -) - -func GetChannelTopicCount(w http.ResponseWriter, r *http.Request) { - - // scan parameters - params := mux.Vars(r) - topicId := params["topicId"] - - var subject Subject - if err := ParseRequest(r, w, &subject); err != nil { - ErrResponse(w, http.StatusBadRequest, err) - return - } - - channelSlot, _, err, code := getChannelSlot(r, false) - if err != nil { - ErrResponse(w, code, err) - return - } - - // load topic - var topicSlot store.TopicSlot - if err = store.DB.Where("channel_id = ? AND topic_slot_id = ?", channelSlot.Channel.ID, topicId).First(&topicSlot).Error; err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - code = http.StatusNotFound - } else { - code = http.StatusInternalServerError - } - return - } - - WriteResponse(w, getTopicCountModel(&topicSlot)) -} - diff --git a/net/server/internal/api_removeChannelTopic.go b/net/server/internal/api_removeChannelTopic.go index 1942a401..bc26d3fb 100644 --- a/net/server/internal/api_removeChannelTopic.go +++ b/net/server/internal/api_removeChannelTopic.go @@ -64,6 +64,9 @@ func RemoveChannelTopic(w http.ResponseWriter, r *http.Request) { if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_removeChannelTopicAsset.go b/net/server/internal/api_removeChannelTopicAsset.go index 867df7a3..077976e5 100644 --- a/net/server/internal/api_removeChannelTopicAsset.go +++ b/net/server/internal/api_removeChannelTopicAsset.go @@ -54,6 +54,9 @@ func RemoveChannelTopicAsset(w http.ResponseWriter, r *http.Request) { if res := tx.Model(&asset.Topic.TopicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_removeChannelTopicTag.go b/net/server/internal/api_removeChannelTopicTag.go index df3b5096..48bfc6bc 100644 --- a/net/server/internal/api_removeChannelTopicTag.go +++ b/net/server/internal/api_removeChannelTopicTag.go @@ -1,7 +1,6 @@ package databag import ( - "time" "errors" "net/http" "gorm.io/gorm" @@ -74,16 +73,13 @@ func RemoveChannelTopicTag(w http.ResponseWriter, r *http.Request) { if res := tx.Model(&tag.TagSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } - if res := tx.Model(&topicSlot.Topic).Update("tag_count", len(topicSlot.Topic.Tags)).Error; res != nil { - return res - } if res := tx.Model(&topicSlot.Topic).Update("tag_revision", act.ChannelRevision + 1).Error; res != nil { return res } - if res := tx.Model(&topicSlot.Topic).Update("tag_updated", time.Now().Unix()).Error; res != nil { + if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } - if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { return res } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { diff --git a/net/server/internal/api_setChannelTopicConfirmed.go b/net/server/internal/api_setChannelTopicConfirmed.go index 1e4222fe..0a0ea756 100644 --- a/net/server/internal/api_setChannelTopicConfirmed.go +++ b/net/server/internal/api_setChannelTopicConfirmed.go @@ -53,6 +53,9 @@ func SetChannelTopicConfirmed(w http.ResponseWriter, r *http.Request) { if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_setChannelTopicSubject.go b/net/server/internal/api_setChannelTopicSubject.go index 421f18b3..2dde18b6 100644 --- a/net/server/internal/api_setChannelTopicSubject.go +++ b/net/server/internal/api_setChannelTopicSubject.go @@ -55,6 +55,9 @@ func SetChannelTopicSubject(w http.ResponseWriter, r *http.Request) { if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { + return res + } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } diff --git a/net/server/internal/api_setChannelTopicTagSubject.go b/net/server/internal/api_setChannelTopicTagSubject.go index d9400702..d7f94a19 100644 --- a/net/server/internal/api_setChannelTopicTagSubject.go +++ b/net/server/internal/api_setChannelTopicTagSubject.go @@ -1,7 +1,6 @@ package databag import ( - "time" "errors" "net/http" "gorm.io/gorm" @@ -71,10 +70,10 @@ func SetChannelTopicTagSubject(w http.ResponseWriter, r *http.Request) { 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 { + if res := tx.Model(&tagSlot.Tag.Topic.TopicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { return res } - if res := tx.Model(&tagSlot.Tag.Topic.TopicSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { + if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil { return res } if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil { diff --git a/net/server/internal/modelUtil.go b/net/server/internal/modelUtil.go index 72f9f472..d9b68131 100644 --- a/net/server/internal/modelUtil.go +++ b/net/server/internal/modelUtil.go @@ -258,18 +258,6 @@ func getTopicDetailModel(slot *store.TopicSlot) *TopicDetail { } } -func getTopicCountModel(slot *store.TopicSlot) *TagCount { - - if slot.Topic == nil { - return nil - } - - return &TagCount{ - Count: slot.Topic.TagCount, - Updated: slot.Topic.TagUpdated, - } -} - func getTopicModel(slot *store.TopicSlot) *Topic { if slot.Topic == nil { @@ -286,7 +274,6 @@ func getTopicModel(slot *store.TopicSlot) *Topic { DetailRevision: slot.Topic.DetailRevision, TopicDetail: getTopicDetailModel(slot), TagRevision: slot.Topic.TagRevision, - TopicTags: getTopicCountModel(slot), }, } } diff --git a/net/server/internal/models.go b/net/server/internal/models.go index 6d208f72..0983b543 100644 --- a/net/server/internal/models.go +++ b/net/server/internal/models.go @@ -158,6 +158,8 @@ type ChannelData struct { DetailRevision int64 `json:"detailRevision"` + TopicRevision int64 `json:"topicRevision"` + ChannelDetail *ChannelDetail `json:"channelDetail,omitempty"` } @@ -368,13 +370,6 @@ type Tag struct { Data *TagData `json:"data"` } -type TagCount struct { - - Count int32 `json:"count"` - - Updated int64 `json:"updated"` -} - type TagData struct { Guid string `json:"guid"` @@ -404,8 +399,6 @@ type TopicData struct { TagRevision int64 `json:"tagRevision"` TopicDetail *TopicDetail `json:"topicDetail,omitempty"` - - TopicTags *TagCount `json:"topicTags:,omitempty"` } type TopicDetail struct { diff --git a/net/server/internal/routers.go b/net/server/internal/routers.go index c5890e9c..832479b4 100644 --- a/net/server/internal/routers.go +++ b/net/server/internal/routers.go @@ -579,13 +579,6 @@ var routes = Routes{ GetChannelTopicDetail, }, - Route{ - "GetChannelTopicCount", - strings.ToUpper("Get"), - "/content/channels/{channelId}/topics/{topicId}/count", - GetChannelTopicCount, - }, - Route{ "GetChannelTopicSubjectField", strings.ToUpper("Get"), diff --git a/net/server/internal/store/schema.go b/net/server/internal/store/schema.go index 76c1a216..309d23b4 100644 --- a/net/server/internal/store/schema.go +++ b/net/server/internal/store/schema.go @@ -195,6 +195,7 @@ type ChannelSlot struct { type Channel struct { ID uint `gorm:"primaryKey;not null;unique;autoIncrement"` + TopicRevision int64 `gorm:"not null"` DetailRevision int64 `gorm:"not null"` DataType string `gorm:"index"` Data string @@ -228,8 +229,6 @@ type Topic struct { Status string `gorm:"not null;index"` Created int64 `gorm:"autoCreateTime"` Updated int64 `gorm:"autoUpdateTime"` - TagCount int32 `gorm:"not null"` - TagUpdated int64 TagRevision int64 `gorm:"not null"` Channel *Channel Assets []Asset diff --git a/net/server/internal/testApp.go b/net/server/internal/testApp.go index 36743d44..162557ad 100644 --- a/net/server/internal/testApp.go +++ b/net/server/internal/testApp.go @@ -520,8 +520,6 @@ func (a *TestApp) UpdateApp(rev *Revision) { a.mutex.Lock() defer a.mutex.Unlock() -PrintMsg(rev) - if rev.Profile != a.revision.Profile { if err := a.UpdateProfile(); err != nil { PrintMsg(err)